Formatting is lost while sending mail in .Net 2.0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dskinibbyb
    New Member
    • Mar 2007
    • 8

    Formatting is lost while sending mail in .Net 2.0

    Hi Everybody,

    I am sending mail using the new class in .Net 2.0.
    Here while sending internal mails it is giving me problem.
    Carriage return, Line feed and Spaces are lost while sending mails.
    I am using msg.BodyEncodin g = System.Text.UTF 8Encoding.UTF8 to encode the body.
    But same code and same mail is going fine , when sent to external mails (Like gmail, yahoo, etc.)
    Please can anyone help me in solving my problem.
    I even tried replacing the carriage return and lifeed like this,
    If InStr(mStrBody, CChar(Microsoft .VisualBasic.Ch r(13)) + CChar(Microsoft .VisualBasic.Ch r(10)), CompareMethod.B inary) Then
    mStrBody.Replac e(CChar(Microso ft.VisualBasic. Chr(13)) + CChar(Microsoft .VisualBasic.Ch r(10)), vbCrLf)
    End If
    But too its not working.
    Thank you all in advance.
    Deepak
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You can use '\n' and '\r' instead of all that messy CHAR(13) business.
    The common windows scheme is to have a \r\n.
    If your messages are being sent as html (You didn't say) then you will need a <br/> wherever you want a new line to occur. Also, if it's html, a bunch of spaces will be condensed into a single space. You will need to use like &nbsp; to get extra spaces.

    But I would first ensure that your emails are being sent as plain text.

    Comment

    • dskinibbyb
      New Member
      • Mar 2007
      • 8

      #3
      Originally posted by Plater
      You can use '\n' and '\r' instead of all that messy CHAR(13) business.
      The common windows scheme is to have a \r\n.
      If your messages are being sent as html (You didn't say) then you will need a <br/> wherever you want a new line to occur. Also, if it's html, a bunch of spaces will be condensed into a single space. You will need to use like &nbsp; to get extra spaces.

      But I would first ensure that your emails are being sent as plain text.
      Hi Plater,
      I am Sending the mail as html only if the option IsHtml is ticked in my application. Can you plz tell me how to use (Or set ) the message.Body.Fo rmat to "Plain/Text". Actually the code is as follows, here i am replacing each "\r\n" i.e. "vbCrLf" with 2 vbCrLf's, then i was getting the mail in correct format.
      the code is as follows..
      Public Sub Send(ByVal strServerName As String, ByVal strUserName As String, ByVal strPassword As String)
      Dim msg As System.Net.Mail .MailMessage
      Dim objSMTP As New System.Net.Mail .SmtpClient(str ServerName)
      Dim oAttachments As Attachment
      Dim arrTmp As Array
      Try
      If mIntId <> 0 Then
      If mDtNextRetry.Ti meOfDay <= System.DateTime .Now.TimeOfDay Then
      Try
      msg = New System.Net.Mail .MailMessage(mS trFrom, CStr(mStrTo), CStr(mStrSub), mStrBody)
      If mStrCC <> "" Then
      arrTmp = Split(mStrCC, ";")
      For Each strCC As String In arrTmp
      If strCC <> "" Then
      msg.CC.Add(New MailAddress(str CC))
      End If
      Next
      End If
      If mStrBcc <> "" Then
      arrTmp = Split(mStrBcc, ";")
      For Each strBcc As String In arrTmp
      msg.Bcc.Add(New MailAddress(str Bcc))
      Next
      End If
      msg.IsBodyHtml = mBlnIsHtml
      msg.BodyEncodin g = System.Text.UTF 8Encoding.UTF8
      msg.DeliveryNot ificationOption s = Net.Mail.Delive ryNotificationO ptions.OnFailur e
      For Each strAttachment As String In mColAttachment
      oAttachments = New Attachment(strA ttachment)
      msg.Attachments .Add(oAttachmen ts)
      Next
      objSMTP.Credent ials = New System.Net.Netw orkCredential(s trUserName, strPassword)
      objSMTP.Deliver yMethod = SmtpDeliveryMet hod.Network
      msg.Body = msg.Body.Replac e(vbCrLf, vbCrLf + vbCrLf)
      objSMTP.Send(ms g)
      mIntStatus = MailStatus.Sent
      mDtSentOn = System.DateTime .Now
      Catch ex As Exception
      mIntAttempt = mIntAttempt + 1
      If mIntAttempt = 3 Then
      mIntStatus = MailStatus.UnSe ndable
      Else
      mIntStatus = MailStatus.Erro rInMail
      End If
      mDtNextRetry = System.DateTime .Now.Date.Add(S ystem.DateTime. Now.TimeOfDay). AddHours(2)
      mDtSentOn = CDate("1-1-1900")
      End Try
      UpdateMail()
      End If
      End If

      Catch ex As Exception
      Throw ex
      End Try
      End Sub
      Can u suggest me is anything is wrong inthis code?
      Thanks
      Deepak

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        For HTML, "\r\n" (vbCRLF) is not an html line termination string. I would make a check to an html quickguide to do some formating.

        For Plain Text, how it looks in a textbox is how it should look when it's sent, without any changes required.

        Comment

        Working...