?
Solved

"! " is added to my long email message body ramdomly.

Posted on 2004-10-08
10
Medium Priority
?
596 Views
Last Modified: 2010-04-15
My application (both asp and asp.net) sends email notice.  An exclamation mark is randomly added to my email message body.  I search Internet and found similar bugger in PHP email.  If I cut the message very short, the mark goes away.

http://www.tek-tips.com/viewthread.cfm?qid=913854

As one expert suggests, this could be caused by character encoding that is triggered by use of high-hex-value characters or long lines in the body of the email.

Please help.

Here is the problematic codes, which is in a loop.

StringBuilder bodyMsg = new StringBuilder();
bodyMsg.Append("<p><b>Dear ");
bodyMsg.Append(fName);

..... long message

MailMessage Message = new MailMessage();
Message.To = "sss@here.com";
Message.From = "ddd@there.com";
Message.Subject = "a subject of length like jasdlfkjsadfjsd flsdfjasdjf fdjasldfjasldfjlsdfjsdaj";
Message.Body = bodyMsg.ToString();
Message.BodyFormat = MailFormat.Html;

SmtpMail.SmtpServer = "there.com";
SmtpMail.Send(Message);
0
Comment
Question by:Jinghui Li
10 Comments
 
LVL 3

Expert Comment

by:yellowjetski
ID: 12263258

When you are debugging, try to copy and paste the bodyMsg.ToString() to a html file and see if you are missing or have any extra html tags

Message.Priority = MailPriority.Normal;//Add this line to just make sure.
0
 
LVL 8

Assisted Solution

by:Razzie_
Razzie_ earned 80 total points
ID: 12263936
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12265376
Beginning with V1.1 of the framework, System.Web.Mail does no longer use Quoted-Printable encoding on HTML mails. Therefore, you should make sure yourself that no line exceeds 76 characters, which is the limit one should use for trasnport over SMTP. Adding line breaks may solve your problem.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 14

Expert Comment

by:AvonWyss
ID: 12265380
Also, make sure that no line starts with a "." since this is not well handled by some servers.
0
 

Author Comment

by:Jinghui Li
ID: 12266425
AvonWyss,

Thanks a million for your input.  Does "no line exceeds 76 characters" mean I need to use html code to control the line length, instead of letting recipient's email client to wrap the lines up?  If I have to force wrapup using html tags, then I have to.  Otherwise, I may get somethings like the following if automatic wrapup happens before 76 characters.

12345678902234567890323456789042345678905234567890623456789
0723456(foreced wrapup)
1234567890223456789032345678904234567890523456789062345678907
23456(foreced wrapup)
1234567890223456789032345678904234567890523456789062345678907
23456(foreced wrapup)

Biang
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12267851
No, I think there is a misunderstanding. The thing is that the lines of the HTML source (!) must not exceed 76 charachters in length to be fully compatile with the SMTP specifications. Since HTML allows you to use line breaks which are not transferred to the output (just a whitespace, except for <pre>-sections), this usually does not change the visible result of your mail. Basically, it's goot enough to write some code that will split up the HTML source code into lines with a length shorter or equal to 76 characters. You usually can add line breaks at any whitespace or tag start/end without changing the finale HTML result.
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 1920 total points
ID: 12267889
Assuming that msg is the message you prepared to be sent, try using something like this:

      [....] // prepare the msg here
            StringBuilder sb=new StringBuilder(msg.Body);
            if (msg.BodyFormat == MailFormat.Html) {
                  // normalize whitespace for HTML
                  sb.Replace('\r', ' ');
                  sb.Replace('\n', ' ');
                  sb.Replace('\t', ' ');
                  sb.Replace(">", " >");
                  while (sb.ToString().IndexOf("  ") != -1)
                        sb.Replace("  ", " ");
            }
            Wrap(sb, 76);
            msg.Body = sb.ToString();
      [....] // send here using SmtpMail.Send(msg);


      public static void Wrap(StringBuilder sb, int lineLength) {
            int lastWS=-1, lastNL=0;
            for (int pos=0; pos<sb.Length; pos++) {
                  switch (sb[pos]) {
                  case '\r':
                  case '\n':
                        lastWS=-1;
                        lastNL=pos+1;
                        break;
                  case '\t':
                  case ' ':
                        lastWS=pos;
                        break;
                  }
                  if (pos-lastNL>lineLength) {
                        if (lastWS>=0) {
                              sb.Remove(lastWS, 1);
                              sb.Insert(lastWS, "\r\n");
                              lastNL=lastWS+1;
                        }      else {
                              sb.Insert(pos, "\r\n");
                              lastNL=pos+1;
                        }
                        lastWS=-1;
                  }
            }
      }

This code should get you started and it will also add line breaks to plain text messages.
0
 

Author Comment

by:Jinghui Li
ID: 12277584
AvonWyss,

Great!!! It works.  Two more questions.

1) where I can find the SMTP specifications

2) wrap method wraps the line in the middle of link, like

"xxxxxxxx xxxx htt://ww
w.xxxxxxxxx"

When I look at the page, the link ends up with "http://ww w.xxxxxxx".  A space looks like added.  But in html code is not.  Any further suggestion?

Thanks a million for your advice, AvonWyss.

Bin

0
 
LVL 14

Assisted Solution

by:AvonWyss
AvonWyss earned 1920 total points
ID: 12278105
1) The current SMTP spec is this one:
http://www.zvon.org/tmRFC/RFC2821/Output/index.html

Note that the issue with long lines is not handled in this spec, since it's in relation with RFC 822 and 2822. Some details about this can be found here:
http://mailformat.dan.info/body/linelength.html

2) This should only happen when the link given is longer than 76 chars. In this case, try to use a shorter link; if not possible, you can change the Wrap() method to suit your needs. Replacing

               if (pos-lastNL>lineLength) {
                    if (lastWS>=0) {
                         sb.Remove(lastWS, 1);
                         sb.Insert(lastWS, "\r\n");
                         lastNL=lastWS+1;
                    }     else {
                         sb.Insert(pos, "\r\n");
                         lastNL=pos+1;
                    }
                    lastWS=-1;
               }

with

               if ((pos-lastNL>lineLength) && (lastWS>=0)) {
                    sb.Remove(lastWS, 1);
                    sb.Insert(lastWS, "\r\n");
                    lastNL=lastWS+1;
                    lastWS=-1;
               }

may do the trick (not tested).
0
 
LVL 14

Assisted Solution

by:AvonWyss
AvonWyss earned 1920 total points
ID: 12278176
Note that line lengths up to 998 chars should be handled OK, but aren't by some mail gateways (especially gateways which do mail exchange between two different systems, like SMTP to X400, are often not handling this well, but also some other servers and mail clients just cannot handle long lines). De facto, the suggested 78 chars is the max which you can be sure will be reliably transported. While this should be excluding the CRLF sequence, I have seen code which does not exclude it (for instance, allocating a 79 char buffer for a tero terminated string, which results in effective 76 chars available including CRLF), so that this value really gets you on the safe side...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

809 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