By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,435 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

Formatting is lost while sending mail in .Net 2.0

P: 8
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.BodyEncoding = System.Text.UTF8Encoding.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.Chr(13)) + CChar(Microsoft.VisualBasic.Chr(10)), CompareMethod.Binary) Then
mStrBody.Replace(CChar(Microsoft.VisualBasic.Chr(1 3)) + CChar(Microsoft.VisualBasic.Chr(10)), vbCrLf)
End If
But too its not working.
Thank you all in advance.
Deepak
May 31 '07 #1
Share this Question
Share on Google+
3 Replies


Plater
Expert 5K+
P: 7,872
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.
May 31 '07 #2

P: 8
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.Format 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(strServerName)
Dim oAttachments As Attachment
Dim arrTmp As Array
Try
If mIntId <> 0 Then
If mDtNextRetry.TimeOfDay <= System.DateTime.Now.TimeOfDay Then
Try
msg = New System.Net.Mail.MailMessage(mStrFrom, 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(strCC))
End If
Next
End If
If mStrBcc <> "" Then
arrTmp = Split(mStrBcc, ";")
For Each strBcc As String In arrTmp
msg.Bcc.Add(New MailAddress(strBcc))
Next
End If
msg.IsBodyHtml = mBlnIsHtml
msg.BodyEncoding = System.Text.UTF8Encoding.UTF8
msg.DeliveryNotificationOptions = Net.Mail.DeliveryNotificationOptions.OnFailure
For Each strAttachment As String In mColAttachment
oAttachments = New Attachment(strAttachment)
msg.Attachments.Add(oAttachments)
Next
objSMTP.Credentials = New System.Net.NetworkCredential(strUserName, strPassword)
objSMTP.DeliveryMethod = SmtpDeliveryMethod.Network
msg.Body = msg.Body.Replace(vbCrLf, vbCrLf + vbCrLf)
objSMTP.Send(msg)
mIntStatus = MailStatus.Sent
mDtSentOn = System.DateTime.Now
Catch ex As Exception
mIntAttempt = mIntAttempt + 1
If mIntAttempt = 3 Then
mIntStatus = MailStatus.UnSendable
Else
mIntStatus = MailStatus.ErrorInMail
End If
mDtNextRetry = System.DateTime.Now.Date.Add(System.DateTime.Now.T imeOfDay).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
Jun 1 '07 #3

Plater
Expert 5K+
P: 7,872
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.
Jun 1 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.