Thursday, March 22, 2012

Strange issue with system.web.mail

Hello,

I have a strange issue when sending e-mails using system.web.mail. For an unknown reason every 990 character is a space in my message body. Not a big issue? Well, bigger then you can think. If the 990 character is in the middle of a HTML-tag, the message body is constructed wrong.

I tested this with the script you can find below. Try it yourself to see the result. I use Microsoft .NET Framework Version:1.1.4322.2300; ASP.NET Version:1.1.4322.2300. My provider doesn't have another version available for me.

<%@dotnet.itags.org. Import Namespace = "System.Text" %>
<%@dotnet.itags.org. Import Namespace = "System.Web.Mail" %>
<script runat="server">
Sub SubmitEmail(sender As Object, e As EventArgs)
Dim sMsg as String
sMsg ="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"

doEmail("name.to@dotnet.itags.org.yourhost.com", "name.from@dotnet.itags.org.yourhost.com", sMsg, "Test e-mail")
End Sub

Function doEmail(sTo as String, sFrom as String, sMsg as String, sSubject as String) 'Email component
Dim objEmail as New MailMessage
objEmail.To=sTo 'TO
objEmail.FROM=sFrom 'From

objEmail.SUBJECT=sSubject 'Subject
objEmail.BODY=sMsg 'Link Message body
objEmail.BodyFormat = MailFormat.HTML 'MailFormat.Text = RichText & MailFormat.HTML = HTML
SmtpMail.SmtpServer ="localhost" 'SMTP server
SmtpMail.Send(objEmail) 'Send message
End Function
</script>
<form runat="server">
<asp:Button id="Submit" text="Submit e-mail" OnClick="SubmitEmail" runat="server" />
</form>

Is this a known issue? What is the solution? I prefer not buying a e-mail component?

Thanks for your suggestions.

I don't know the answer - but have you tried stringbuilder. I have read about problems using string ( mostly performance related ) and that stringbuilder is the best choice ( for most string building situations)

This issue is solved. I found the solution here:

http://www.systemwebmail.com/faq/4.4.5.aspx

Usually you have a line in the body that is too long. The email rfc's state that a line should not contain more than 1000 characters. Try inserting a CrLf (vbCrLf for VB.NET or "\r\n" for C#) in your message body to break up long lines.

0 comments:

Post a Comment