Line Feeds

DavyEFC

Well-known member
Joined
Dec 17, 2010
Messages
51
Programming Experience
5-10
I have what is hopefully a simple, newbie issue to resolve. I have some code that opens a new email in the default client, if it exists. I don't need to actually send the email (hence no addressing) just open a new message and populate subject and body. The code below works fine in every way except in the text body the newlines are ignored so its all on one continuous line. I have tried splitting the text by line feed (which is successful) and then building up while appending vbCrLf to each, but still no joy.

(frmMain.txtBody is a RichTextBox, and DNFileName is a string that may or may not be present)

The only issue i need to resolve is the newlines. Help!

VB.NET:
    Sub OpenEmail()
        Dim ProcString$ = ""
        Dim S$()
        Dim myProcess As New Process
        '-----------------------------
        If Len(DNFileName) > 0 Then ProcString = "&subject=" & DNFileName
        ProcString = ProcString & "&body="

        S = Split(frmMain.txtBody.Text, ControlChars.Lf)
        For i = 0 To UBound(S)
            ProcString = ProcString & S(i) & vbCrLf
        Next

        Try
            With myProcess
                .StartInfo.FileName = "mailto:" & ProcString
                .StartInfo.UseShellExecute = True
                .StartInfo.RedirectStandardOutput = False
                .Start()
                .Dispose()
            End With
        Catch ex As Exception
            MsgBox("Error trying to generate email:" & vbCrLf & Err.Description, 48, c_AppName)
        End Try
    End Sub
 
You can encode body with HttpUtility.UrlEncode (System.Web assembly, full .Net 4 Framework), or url encode all special characters yourself. Cr is url encoded "%0D", and Lf is "%0A".
 
(Apologies for posting in wrong area)

Thanks, i changed the code to:
VB.NET:
        For i = 0 To UBound(S)
            ProcString = ProcString & S(i) & "%0A"
        Next
..and that works for me.
Problem now is that if the text body is too long it is cutting it short. Any ideas on that one?
 
It wouldn't surprise me if there was system limits both for length of commandline and mailto url. One option could be putting larger body content into Clipboard ready to paste into the generated email.
 
It wouldn't surprise me if there was system limits both for length of commandline and mailto url.

Yeah, i don't know for fact but i would guess you are quite right.

One option could be putting larger body content into Clipboard ready to paste into the generated email.

I did think of that and tried it but failed. The problem was (apart from my inexperience!) that i couldn't get to paste in the message window because once the process was disposed all links to teh message were lost. How would i do that?
 
The problem was (apart from my inexperience!) that i couldn't get to paste in the message window because once the process was disposed all links to teh message were lost. How would i do that?
That sounds weird, all Clipboard.Set methods preserves clipboard data when application exits, except SetDataObject method where copy parameter provides explicit control.
 
It wouldn't surprise me if there was system limits both for length of commandline and mailto url. One option could be putting larger body content into Clipboard ready to paste into the generated email.

My very first post on this forum was on this very topic. I was trying to use Process.Start to execute a mailto URL. As I recall, the limit was with Process.Start and it was just over 2000 characters. My guess would be that it is 2048 but I never tested to find the exact number.
 
I don't think John meant for you to programmatically paste the text into the mail window. I think he meant for you to leave it on the clipboard so the user can paste it
If this is a professional app you'd be better off putting a little bit of rime into rolling your own mail screen - itll be the same effort as spending hours messing with hacks to get the default client working and will give a better result
 
Thanks for all your replies, plenty of food for thought!

cjard, no - not a pro app, just comes under my 'see if i can do this' type coding, but i take your point exactly.
 
Back
Top