Solved

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

Posted on 2004-10-08
10
580 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
Comment Utility

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
Comment Utility
0
 
LVL 14

Expert Comment

by:AvonWyss
Comment Utility
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
 
LVL 14

Expert Comment

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

Author Comment

by:Jinghui Li
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 14

Expert Comment

by:AvonWyss
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now