Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

MailMessage class: E-Mail body ignores vbCrLf

MailMessage class: E-Mail body ignores vbCrLf

MailMessage class: E-Mail body ignores vbCrLf


Here's the function I've put together:


Public Function SendEMail(ByVal tcSubjectLn As String, ByVal tcMsgText As String, ByVal tcSender As String, _
                      ByVal tcAddressee As String, ByVal tcSMTPSrvr As String) As Int16
' Purpose       : Sends E-Mail with the given subject line and text from the given sender to the given addressee(s).
' Description   : Uses System.Net.Mail.MailMessage class.
' Parameters    : Subject line, message text, Sender's E-Mail address, adressees E-Mail adresses list, SMTP Server name,
'                 All Mandatory, all Strings.
' Side effects  : None.
' Notes         : 1. Verbose on error if run in VS's IDE, silent otherwise.
'                 2. No parameters' verification, presumed valid (errs if not, and then see p. 1).
Dim loMail As New MailMessage(), loMailTo As New MailAddress(tcAddressee), loMailClient As New SmtpClient(tcSMTPSrvr, 25)
Dim lcLogStr As String = "", lnRet As Int16 = 0

loMail.To.Clear()                                                                         'Clear/Reset to: settings...
loMail.CC.Clear()                                                                         'Clear/Reset cc: settings...
loMail.Bcc.Clear()                                                                        'Clear/Reset bc: settings...
loMail.To.Add(loMailTo)                                                                   'Add override email value...
loMail.From = New MailAddress(tcSender)
loMail.Subject = tcSubjectLn                                                              'Set the subject...
loMail.Body = tcMsgText                                                                   'Set the body...
loMail.IsBodyHtml = False  ' True                                                                  'Set type to HTML...

'Send email...
   loMailClient.Send(loMail)                                                               'Send the email...
Catch loErr As Exception
   lnRet = -1
   lcLogStr = lcLogStr & Now.ToString("yyyy-MM-dd HH:mm:ss") & ": ERROR OCCURED while sending E-Mail to " & tcAddressee & "!" & _
              vbCrLf & Space(21) & loErr.Message & vbCrLf & Space(21) & "Occured in " & vbCrLf & Space(21) & loErr.Source & _
              vbCrLf & Space(21) & loErr.StackTrace & vbCrLf & Space(21) & "E-Mail '" & tcSubjectLn & "' wasn't sent." & vbCrLf
   Write2Log(gcLogFile, lcLogStr, True)

   If glDevelop Then
      MsgBox("ERROR OCCURED while sending E-Mail to" & tcAddressee & "!" & vbCrLf & loErr.Message & vbCrLf & "Occured in " & _
            loErr.Source & vbCrLf & loErr.StackTrace, MsgBoxStyle.Critical, gcAppName)
   End If

End Try

Return lnRet

End Function

Here's the (error) message passed as parameter tcMsgText:


lcEMailMsg = "ERROR OCCURED!" & vbCrLf & loErr.Message & vbCrLf & "Occured in " & vbCrLf & loErr.Source & vbCrLf & _
                loErr.StackTrace & vbCrLf & "Program quits." & vbCrLf 

Note that there are vbCrLf's, and IsBodyHtml property's set to False.
This is what I get:

Could not find file 'FileMover_Manager'.
Occured in
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
at FileMover_Manager.FileMover_Manager.MakeWorkersSubdirs(String pcTemplateDirName, String pcRootDir) in [Full path to]FileMover_Manager.vb:line 368 Program quits

IOW, that last string, "Program quits." (in bold type), must be on the next line - but it is not: it's on the same line.
(BTW, the same happens when IsBodyHtml = True: CRLF's are ignored.)

What am I doing wrong?
Alternatively - what shall I do to force MailMessage to "obey" CrLf in the E-Mail message's body?



RE: MailMessage class: E-Mail body ignores vbCrLf

I think I ran across this a while ago.
Not sure how to make it recognize the vbcrlf.
I use the string builder when building the body, seems to work just fine.


stbEmailBody.AppendLine("Folder: " & BackupFolder) 

Sylvania/Toledo Ohio

RE: MailMessage class: E-Mail body ignores vbCrLf

Can't replicate this

Quick question - when you say "This is what I get" can you tell us HOW you get it? Examining local variables in the debugger? Dispayed via Msgbox or similar? Actually in an email? If the latter, what email client?

RE: MailMessage class: E-Mail body ignores vbCrLf

To Auguy:



If loMail.IsBodyHtml Then
   loMail.Body = tcMsgText
Else ' ASCII or whatnot
   Dim laMsg() As String = Split(tcMsgText, vbCrLf), liMax As Int16 = UBound(laMsg, 1), liCnt As Int16 = 0
   Dim loStrBuilder As New Text.StringBuilder()

   For liCnt = 0 To liMax

   loMail.Body = loStrBuilder.ToString()
End If ' loMail.IsBodyHtml 
does work!
Thank you, colleague!

P.S. One thing, though (apparently, by knowing MS ways, "it's not-a-bug-but-a-feature"): it appears that StringBuilder's parsing function is kinda strictly follows the rule "String ends with the dot, next string starts with uppercase letter".
If I just split a string with CRLF w/o a dot (".") - it makes it into "uninterrupted" string. E.g.:
"Testing ASCII multi-string E-Mail body" & vbCrLf & "Str #1" & vbCrLf & "Str #2" & gcAlertEMailFtr - displayed properly in the E-Mail's body;


"In addition, please be aware that any message addressed to our domain is subject to archiving" & vbCrLf & _
"and reviewed by persons other than the intended recipient." - this CRLF (in bold type) is still omitted in the E-Mail message body.



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close