Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MailMessage class: E-Mail body ignores vbCrLf 1

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
571
US
Colleagues,

Here's the function I've put together:
Code:
'===================================================================================================================================
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...
Try
   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:

Code:
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:

"ERROR OCCURED!
Could not find file 'FileMover_Manager'.
Occured in
mscorlib
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?
TIA!

Regards,

Ilya
 
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.
Code:
stbEmailBody.AppendLine("Folder: " & BackupFolder)

Auguy
Sylvania/Toledo Ohio
 
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?
 
To Auguy:

This
Code:
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
      loStrBuilder.AppendLine(laMsg(liCnt))
   Next

   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;

however

"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.

Regards,

Ilya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top