Solved

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

Posted on 2004-10-08
10
591 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
[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
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 20 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 480 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 480 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 480 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Service FileSystemwatcher isse 9 64
Read top line from CSV file 14 57
C# LINQ query question 11 30
What is GIS method of Geometry data type? 6 33
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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