Starting new process with very long file name

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
15,033
Location
Sydney, Australia
Programming Experience
10+
I am developing an application that uses the default e-mail client to send an e-mail message to a list of selected contacts. The user selects the desired contacts and the application extracts their e-mail addresses, puts them in a String variable named "emailAddresses" separated by semi-colons and then uses the following code to create the e-mail message:

VB.NET:
Process.Start("mailto:?bcc=" + emailAddresses)

This method works fine until the length of the argument to Process.Start() exceeds some limit that seems to be a liitle over 2000 characters. When this occurs I get a Win32Exception that says "Access is denied". I've tried using Shell() but it doesn't seem to work with a URL as an argument. Does anyone know of a way around this, seemingly undocumented, limitation of Process.Start(). It is not different in .NET 2.0, so I can't just wait until that reaches release either.
 
I've already considered a couple of options, like breaking the list of e-mail addresses into substrings, each within the character limit, and creating a new message for each, or copying the entire string to the clipboard and creating a single new message, whereupon the user can paste the address list into the desired recipient field. Both of these options are, I'm sure you'll agree, a tad kludgy. I am looking for a way to create a single e-mail message addressed to all recipients, regardless of the length of the list.
 
Last edited:
Instead of a String variable named "email addresses", why not declare it as an array. Then, virtually, there will be no limit, and the program should perform as desired.

I am currently building an e-mail component, and I am using an array for my To, Cc, and Bcc recipients. It works for me, and I have had no problems with length


Post and let me know if declaring the array works.


B Rad
 
Thank you for your reply B Rad.

I'm not sure, however, how you are intending that the array of e-mail addresses be used. I am currently using Process.Start("mailto:") to create the new e-mail message because it doesn't require my application to do any of the work where the e-mail is concerned or to know anything about the e-mail client being used, and also the user will end up with a copy of the message in their Sent Items folder, as would normally be desired. The problem I am experiencing seems to be as a result of the fact that if the FileName property (As String) of the StartInfo property (As ProcessStartInfo) of the new Process is longer than a certain number of characters (I haven't determined the exact number but it seems to be a little over 2000) an exception is thrown in the private method Process.StartWithShellExecuteEx(). The overload of Process.Start() that I am using takes a single String argument that respresents the name of the file to open. None of the other overloads take an array as an argument either, so I assume you are intending that some other method of creating the message be used. Keeping in mind that I am not developing an e-mail client, exactly how would you suggest the message be created?
 
Last edited:
I'll play around with some code and see what I can come up with. Do you by chance have a code snippet to post. That way we have something to work off of.


B Rad
 
I don't know if this will make things any clearer. My form contains a ListView, called studentListView, that contains a student record in each row. The column at index 28 contains all e-mail addresses for the student, concatenated and separated by "; ". The ListView has CheckBoxes enabled and the user must check a row to include that student in the new e-mail message. Here is the entire onClick event handler for a Button call emailButton.
VB.NET:
	Private Sub emailButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles emailButton.Click
		Dim uniqueAddressList As New ArrayList
		Dim studentAddresses As String()
		Dim recipients As String

		For Each studentItem As ListViewItem In Me.studentListView.CheckedItems
			If studentItem.SubItems(28).Text <> Nothing Then
			 studentAddresses = studentItem.SubItems(28).Text.Replace(" ", Nothing).Split(";")

			    For Each emailAddress As String In studentAddresses
				 If Not uniqueAddressList.Contains(emailAddress) Then
					 If recipients <> Nothing Then
						 recipients += ";"
					 End If

					 recipients += emailAddress
					 uniqueAddressList.Add(emailAddress)
					End If
				Next
			End If
		Next

		If recipients.Length > 2000 Then
		 MessageBox.Show("The list of student e-mail addresses is too long to send automatically to your e-mail client." + vbNewLine + _
						 "The list of addresses has been copied to the clipboard." + vbNewLine + _
						 "When the new e-mail message opens you must paste the address list into the desired recipient field.", _
						 "Recipient List Too Large", _
						 MessageBoxButtons.OK, _
						 MessageBoxIcon.Information)
			Clipboard.SetDataObject(recipients)
			Process.Start("mailto:")
		Else
			Process.Start("mailto:?bcc=" + recipients)
		End If
	End Sub
As you can see, I've opted for the clipboard option as a workaround for the moment, but I would like to remove that user intervention if possible.
 
Last edited:
I haven't forgotten. I have some other programmers I work with from time to time looking at the problem. They have worked more often than I have with
e-mail in applications.

I'll get back as soon as I can with the answer.....if we can come up with one. In the meantime, help from others would be appreciated by all.


B Rad
 
So far, we can't figure it out. My fellow programmers are like me and have always implemented their own interface by using the SMTPMail class and referencing the System.Web.Mail namespace.

Currently, they don't have the time to figure it out, and I'm stumped. Nevertheless, I will keep working on it, because personally I want to know what was happening and why.


B Rad
 
Last edited:
I know I'm only about a year late on this topic, but I came across it on google and though somebody else may do the same.

I've had the same problem with the character limit on starting a process for creating a new email. To get around this I used the following bit of code:

VB.NET:
[FONT=Courier New]Dim proc As Process = New Process [/FONT]
[FONT=Courier New]Dim startInfo As ProcessStartInfo = New ProcessStartInfo("C:\Program Files\Microsoft Office\Office12\Outlook.exe", _[/FONT]
[FONT=Courier New]  "/c ipm.note /m " + emailList) [/FONT]
[FONT=Courier New]startInfo.UseShellExecute = False [/FONT]
[FONT=Courier New]proc.StartInfo = startInfo [/FONT]
[FONT=Courier New]proc.Start[/FONT]
That calls the outlook exe directly, telling it to create a new email (note) with emails from the emailList string (just semi-colon separated email addresses). When not using shell execute, you don't have a limit on the number of characters.

This works for me, but if you don't use outlook as your email client then you're out of luck! My example relies on Outlook 2007, so you should change the path if you don't have that.
 
Last edited by a moderator:
Back
Top