Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to insert line feeds in stringbuilder for SMTP email body

Posted on 2006-06-14
2
Medium Priority
?
3,306 Views
Last Modified: 2012-06-27
Hi folks,

My first post on EE and hopefully one that will help others (once solved) who have stumbled across this issue.  I'm new to the point system, so hopefully I have valued this appropriately. If your web app generates email and your email is often misformated with exclamations points, read on.

I have a web application that generates an HTML email back to the user. The email is created dynamically and is different for each user. The body of the email is generated by creating an VB.Net Stringbuilder object (sb) and basically sb.Appending various HTML elements together. My guess is this is a fairly common usage.

For example, if I know the first name of the user, things start out like this:

        If firstName.Trim <> "" Then
            sb.Append("<p style=""margin-top: 0; margin-bottom: 0""><font face=""Arial"" style=""font-size: 11pt"">Dear " & firstName.Trim & ",</font><br>&nbsp;</p>")
        End If

Various paragraphs and info are continuously sb.appended and then the entire SB is converted to a string and passed to an email function. This string can be quite long. 5000 - 10000 characters.

The email function is pretty straightforward. The string is assigned to the email.Body and off it goes (try catch block not shown). The key to this problem is that it goes to an SMTP email server. I've not hit this issue on other email servers.

        emailMessage.BodyFormat = Mail.MailFormat.Html
        emailMessage.Body = big string assigned here
        System.Web.Mail.SmtpMail.SmtpServer = System.Configuration.ConfigurationSettings.AppSettings("MailSvr")
        System.Web.Mail.SmtpMail.Send(emailMessage)

What I noticed immediately is that our host email server seemed to be inserting carriage returns into the body of the message. Most times this just appears as an exclamation point ! and space in the middle of text, but sometimes it really whacks the HTML (especially when it hits in the middle of a tag). After repeated denials from the host that the email server had anything to do with it (wrong!) and lots of searching, I finally discovered that the SMTP server does add carriage returns to email when a line is greater than 990 characters. Apparently the smtp_line_length_limit defaults to 990 and smtp then inserts "<CR><LF><SPACE>" into the long lines.

So that sounded reasonably easy to fix and I happily added "&#10" throughout my sb.Append lines. Only problem, that doesn't seem to solve the problem. Still got the breaks at 990 characters.  I also tried "<LF>" and that seemed to make things better - the exclamation points were gone (the CR part, I think), but still got the spaces every 990 chars.

So the question after all this is, "In VB.net, how do you add a line feed to a stringbuilder object that is recognized by SMTP?"  BTW, <br> doesn't seem to count as a line feed and I'm pretty sure vbcrlf is not the answer.

Thanks in advance and for reading this far.
Dave Hunt
davehunt00








0
Comment
Question by:davehunt00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
pradeepsudharsan earned 375 total points
ID: 16908728
Hai,
plz  use vbCrLf

--Pradeep
0
 
LVL 9

Expert Comment

by:bele04
ID: 16909104
SMTP has a limit of 76 characters per line...try adding a vbCrLf after the 76th character or if that doesn't work try adding two vbCrLf to the end of the string that you will be adding to the stringbuilder
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question