Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-08
10
Medium Priority
?
595 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 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
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 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

636 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