I'm currently working on an e-commerce project and I am using a register form on the site where users can create a new account. When they click "Register" I would like to send them their username and password in an html email.
The code I am using for this is contained in the register.aspx.cs (code behind) file for the register form and is executed when users click the register button:
MailMessage mail = new MailMessage();
mail.To = fld_EMail.Text;
mail.From = "support@dotnet.itags.org.ttp.com";
mail.Subject = "Account Notification";
mail.BodyFormat = MailFormat.Html;
mail.Body = "<html><body>Sample text..."+
"some more sample text here..."+
"some more... etc etc etc...</body></html>";
SmtpMail.Send(mail);
This seems to work ok or at least i thought it did. Most email accounts will display the html message ok. But the problem I'm having is that on some email account the message gets truncated with the folloing message in the email header information:
"Sun-Java-System-SMTP-Warning: Lines longer than SMTP allows found and truncated."
It seems when the email is sent that all the html from the mail.Body is put on one line and some servers don't like it.
Does anyone know a way around this? Forcing line breaks would be an option but I don't know how to from within the .cs file. The html is on multiple lines in my .cs file joined together like this:
mail.Body = "some html code" +
variable + "some more html" + another variable +
"some more html" +
"yet more html";
Or maybe someone knows a better way to send html email containing variables such as username passwords etc from a web application? I am really lost as to why some email accounts get the whole message and some others simply chop the email and only display the first 1024 characters.
Help much appreciated!
Thanks, Toby :)SMTP doesn't like lines longer than 1024 characters. You'll need to make sure you enter enough line breaks so that you never have lines longer than that.
Well, that would make sense, as not all servers support HTML. That seems to be the limitation you are running into. Try using \n or \r for a new line using C#.
Brian
As a followup, you can add line breaks with Environment.Newline, like this:
"some html code" + Environment.Newline + "some more html code" + Environment.Newline
Thanks for your help bmains!!!
Seems to work fine by adding + "\r\n" at the end of each line in the .cs file!
I was hopig it was something as simple as that :)
Big up yourself!!
Thanks Darrel, that also seems to work...
What is the difference between "\r\n" (see above) and Environment.Newline?
Environment.Newline adds the "\n" character for you. The benefit is if .NET is ever ported (well, it has been simulated with MONO) the Newline character, which varies by platform, won't have a platform-specific problem.
It's better practice to use Environment.Newline instead of "magic strings" like "\r\n".
0 comments:
Post a Comment