2012-09-25 30 views
4

我想用其他的意见纯文本或HTML模板发送电子邮件,我已经使用的代码从以下链接 aspnet-sending-email-both-in-html-and-plain-text但我不知道为什么它不工作对我来说..我的代码是:。如何使用纯文本或HTML模板的备用视图发送电子邮件。

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("[email protected]"); 
    msg.Subject = "Event: testing"; 
    msg.To.Add("[email protected]"); 

    // create a string to hold all email addresses 
    StringBuilder sbEmailTo = new StringBuilder(); 
    sbEmailTo.Append("[email protected]"); 

    //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 = "[email protected]"; 
    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='#'>[email protected]</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("[email protected]", "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); 
} 

和我收到邮件以下outout:

<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='#'>[email protected]</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> 

请帮me..i要显示它的格式正确的方式,因为我想用备用的看法。

+1

我想看看MSDN文档:http://msdn.microsoft.com/en-us/library/system.net.mail。 mailmessage.alternateviews.aspx看来你有你的HTML,纯文本交换周围 – Justin

+0

这里是告诉您如何(http://comfirm.se/send-email-with-csharp-dotnet [用C#发送电子邮件]的文章/)(以链接到[AlternateViews](http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.alternateviews.aspx))。 –

回答

7

尝试创建备用视图时使用text/html的为MIME类型:

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

如int这个例子做it's: Alternate Views

+2

你调用变量'plainView',但它的HTML。也许'htmlView'会更好? – bradlis7

+0

@ bradlis7,是很可能是更具可读性。 – Kamolas81

-4

请试试这个。 因为HTML身体didnt使用您记录

protected void btnSubmit_Click(object sender, EventArgs e) 
{ 

    MailMessage mail = new MailMessage(); 
    mail.To.Add("[email protected]"); 
    mail.From = new MailAddress("[email protected]"); 
    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='#'>[email protected]</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("[email protected]", "password"); 
    //Or your Smtp Email ID and Password 
    smtp.Send(mail); 
    ClearTextBoxes(Page); 
    Label1.Text = "Thanks for contacting us"; 
} 
相关问题