How do I send emails with alternative views, either as plain text or as HTML templates.?

I want to send an email with alternative views with both plain text and an html template, I used the code from the following link aspnet-sending-email-both-in-html-and-plain-text , but I don't know why it does not work for me .. my code :.

public void EmailSendingWithBothHTMLAndPlainText() { // CREATE EMAIL // first we create a plain text version and set it to the AlternateView // then we create the HTML version System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); msg.From = new MailAddress(" visionstask@gmail.com "); msg.Subject = "Event: testing"; msg.To.Add(" ram@tsgdev.net "); // create a string to hold all email addresses StringBuilder sbEmailTo = new StringBuilder(); sbEmailTo.Append(" visionstask@gmail.com "); //if (!String.IsNullOrEmpty(TextBoxEmail2.Text)) { msg.To.Add(TextBoxEmail2.Text); sbEmailTo.Append(", " + TextBoxEmail2.Text); } //if (!String.IsNullOrEmpty(TextBoxEmail3.Text)) { msg.To.Add(TextBoxEmail3.Text); sbEmailTo.Append(", " + TextBoxEmail3.Text); } //if (!String.IsNullOrEmpty(TextBoxEmail4.Text)) { msg.To.Add(TextBoxEmail4.Text); sbEmailTo.Append(", " + TextBoxEmail4.Text); } //if (!String.IsNullOrEmpty(TextBoxEmail5.Text)) { msg.To.Add(TextBoxEmail5.Text); sbEmailTo.Append(", " + TextBoxEmail5.Text); } //now create the HTML version MailDefinition message = new MailDefinition(); message.BodyFileName = "RegisterUser.html"; message.IsBodyHtml = true; message.From = " visionstask@gmail.com "; message.Subject = "Zoo Event: " + "testing for HTML"; //embed images for the email //EmbeddedMailObject emo = new EmbeddedMailObject(); //emo.Path = @"~\Images\email\hdr_roar.gif"; //emo.Name = "hdr"; //EmbeddedMailObject emo2 = new EmbeddedMailObject(); ////emo2.Path = @"~\Images\email\box_top.gif"; //emo2.Name = "box_top"; //message.EmbeddedObjects.Add(emo); //message.EmbeddedObjects.Add(emo2); //Build replacement collection to replace fields in email.htm file ListDictionary replacements = new ListDictionary(); replacements.Add("@Name", "Ram Singh"); //now create mail message using the mail definition object //the CreateMailMessage object takes a source control object as the last parameter, //if the object you are working with is webcontrol then you can just pass "this", //otherwise create a dummy control as below. //System.Net.Mail.MailMessage msgHtml = message.CreateMailMessage(sbEmailTo.ToString(), replacements, new LiteralControl()); //AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, null, "text/html"); // msg.AlternateViews.Add(htmlView); String plainEmail = "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td align='center'><table width='600' bgcolor='#f3f3f3' border='0' cellpadding='0' cellspacing='0'><tr><td><img src='http://localhost:4823/WishIsDone/Images/emaillogo.png' style='margin-left: 50px;'alt='' /></td></tr><tr><td><table bgcolor='#FFFFFF' border='0' width='500' align='center' style='margin: auto; border: 1px solid #999; padding: 5px;' cellpadding='0' cellpadding='0'><tr><td style='font-size: 18px; font-family: Verdana, Geneva, sans-serif; color: #008fbb;'>Registration</td></tr></table></td></tr><tr><td><table bgcolor='#FFFFFF' border='0' width='500' align='center' style='margin: auto; border: 1px solid #999; padding: 20px;'><tr><td>Hello @Name!</td></tr><tr><td style='font-size: 11px; font-family: Verdana, Geneva, sans-serif;'>Welcome to WishIsDone Website </td></tr><tr><td style='font-size: 12px; color: #008fbb; font-family: Verdana, Geneva, sans-serif;'> <p>You have successfully registrated.</p> </td> </tr><tr><td style='font-size: 11px; font-family: Verdana, Geneva, sans-serif; color: #999;'><p>Verify your e-mail address to complete your registration by clicking the link:</p> <p><a href='#'>@link</a></p><p><strong style='color: #000; font-size: 10px; font-weight: bold;'>Note:</strong><span style='color: #333;'>please change your password after login.</span></p><table style='border-top: 1px dotted #999; margin: auto;' width='450'><tr><td style='font-size: 10px; color: #333;'><p>Regards,<br /> Wish Is Done Team</p></td></tr></table></td></tr> </table> </td></tr> <tr> <td style='font-size: 10px; color: #333; padding: 10px 50px; font-family: Verdana, Geneva, sans-serif;'> <p> For any queries or difficulties Contact:<br /> Email:<a href='#'> info@wishisdone.com </a><br /> Toll free: XXX-XXX-XXXX<br /> <a href='#'>www.wishisdone.com</a></p> </td></tr><tr><td><table style='border-top: 1px dotted #999; width: 500px; color: #333; margin: auto; font-size: 9px; font-family: Verdana, Geneva, sans-serif;'> <tr><td>Copyright &copy; 2012 wishisdone</td></tr></table></td></tr></table></td> </tr></table>"; //first we create the Plain Text part AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainEmail, null, "text/plain"); msg.AlternateViews.Add(plainView); msg.IsBodyHtml = true; SmtpClient emailClient = new SmtpClient("smtp.gmail.com"); System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(" test@gmail.com ", "test"); emailClient.UseDefaultCredentials = false; emailClient.Credentials = SMTPUserInfo; emailClient.Port = 587; emailClient.EnableSsl = true; //SmtpClient emailClient = new SmtpClient(ConfigurationManager.AppSettings["SMTP"].ToString()); //emailClient.UseDefaultCredentials = false; //emailClient.EnableSsl = true; emailClient.Send(msg); } 

and I get the following output in the mail:

 <table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td align='center'><table width='600' bgcolor='#f3f3f3' border='0' cellpadding='0' cellspacing='0'><tr><td><img src='http://localhost:4823/WishIsDone/Images/emaillogo.png' style='margin-left: 50px;'alt='' /></td></tr><tr><td><table bgcolor='#FFFFFF' border='0' width='500' align='center' style='margin: auto; border: 1px solid #999; padding: 5px;' cellpadding='0' cellpadding='0'><tr><td style='font-size: 18px; font-family: Verdana, Geneva, sans-serif; color: #008fbb;'>Registration</td></tr></table></td></tr><tr><td><table bgcolor='#FFFFFF' border='0' width='500' align='center' style='margin: auto; border: 1px solid #999; padding: 20px;'><tr><td>Hello @Name!</td></tr><tr><td style='font-size: 11px; font-family: Verdana, Geneva, sans-serif;'>Welcome to WishIsDone Website </td></tr><tr><td style='font-size: 12px; color: #008fbb; font-family: Verdana, Geneva, sans-serif;'> <p>You have successfully registrated.</p> </td> </tr><tr><td style='font-size: 11px; font-family: Verdana, Geneva, sans-serif; color: #999;'><p>Verify your e-mail address to complete your registration by clicking the link:</p> <p><a href='#'>@link</a></p><p><strong style='color: #000; font-size: 10px; font-weight: bold;'>Note:</strong><span style='color: #333;'>please change your password after login.</span></p><table style='border-top: 1px dotted #999; margin: auto;' width='450'><tr><td style='font-size: 10px; color: #333;'><p>Regards,<br /> Wish Is Done Team</p></td></tr></table></td></tr> </table> </td></tr> <tr> <td style='font-size: 10px; color: #333; padding: 10px 50px; font-family: Verdana, Geneva, sans-serif;'> <p> For any queries or difficulties Contact:<br /> Email:<a href='#'> info@wishisdone.com </a><br /> Toll free: XXX-XXX-XXXX<br /> <a href='#'>www.wishisdone.com</a></p> </td></tr><tr><td><table style='border-top: 1px dotted #999; width: 500px; color: #333; margin: auto; font-size: 9px; font-family: Verdana, Geneva, sans-serif;'> <tr><td>Copyright &copy; 2012 wishisdone</td></tr></table></td></tr></table></td> </tr></table> 

Please help me .. I want to display it in a well formatted form, because of this I want to use alternative views.

+4
source share
2 answers

Try using text / html as the mime type when creating an alternative view:

 AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainEmail, null, "text/html"); 

As in this example: Alternative Views

+7
source

try it. because the html body was not used in your document

 protected void btnSubmit_Click(object sender, EventArgs e) { MailMessage mail = new MailMessage(); mail.To.Add(" you@gmail.com "); mail.From = new MailAddress(" you@gmail.com "); mail.Subject = "PHR WORLD User Contacting You"; string body = "<h1>Message from </h1><p>Simply awesome!</p>"; body += "<HTML><HEAD></HEAD>"; body += "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td align='center'><table width='600' bgcolor='#f3f3f3' border='0' cellpadding='0' cellspacing='0'><tr><td><img src='http://localhost:4823/WishIsDone/Images/emaillogo.png' style='margin-left: 50px;'alt='' /></td></tr><tr><td><table bgcolor='#FFFFFF' border='0' width='500' align='center' style='margin: auto; border: 1px solid #999; padding: 5px;' cellpadding='0' cellpadding='0'><tr><td style='font-size: 18px; font-family: Verdana, Geneva, sans-serif; color: #008fbb;'>Registration</td></tr></table></td></tr><tr><td><table bgcolor='#FFFFFF' border='0' width='500' align='center' style='margin: auto; border: 1px solid #999; padding: 20px;'><tr><td>Hello @Name!</td></tr><tr><td style='font-size: 11px; font-family: Verdana, Geneva, sans-serif;'>Welcome to WishIsDone Website </td></tr><tr><td style='font-size: 12px; color: #008fbb; font-family: Verdana, Geneva, sans-serif;'> <p>You have successfully registrated.</p> </td> </tr><tr><td style='font-size: 11px; font-family: Verdana, Geneva, sans-serif; color: #999;'><p>Verify your e-mail address to complete your registration by clicking the link:</p> <p><a href='#'>@link</a></p><p><strong style='color: #000; font-size: 10px; font-weight: bold;'>Note:</strong><span style='color: #333;'>please change your password after login.</span></p><table style='border-top: 1px dotted #999; margin: auto;' width='450'><tr><td style='font-size: 10px; color: #333;'><p>Regards,<br /> Wish Is Done Team</p></td></tr></table></td></tr> </table> </td></tr> <tr> <td style='font-size: 10px; color: #333; padding: 10px 50px; font-family: Verdana, Geneva, sans-serif;'> <p> For any queries or difficulties Contact:<br /> Email:<a href='#'> info@wishisdone.com </a><br /> Toll free: XXX-XXX-XXXX<br /> <a href='#'>www.wishisdone.com</a></p> </td></tr><tr><td><table style='border-top: 1px dotted #999; width: 500px; color: #333; margin: auto; font-size: 9px; font-family: Verdana, Geneva, sans-serif;'> <tr><td>Copyright &copy; 2012 wishisdone</td></tr></table></td></tr></table></td> </tr></table>"; body += "</FONT></DIV></BODY></HTML>"; body += " Sender Name: " + txtName.Text + "\n Sender Email: " + txtMail.Text + "\n Sender mobile: " + txtmobile.Text + "\n Subject:" + txtMessage.Text; mail.Body = body; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address smtp.Port = 587; smtp.EnableSsl = true; smtp.Credentials = new System.Net.NetworkCredential(" you@gmail.com ", "password"); //Or your Smtp Email ID and Password smtp.Send(mail); ClearTextBoxes(Page); Label1.Text = "Thanks for contacting us"; } 
-4
source

Source: https://habr.com/ru/post/1436065/


All Articles