发送预先电子邮件的最佳方式
我想通过asp.net发送电子邮件。我使用这个代码,它工作正常。发送预先电子邮件的最佳方式
mail.From = new MailAddress("[email protected]", "sender", System.Text.Encoding.UTF8);
string to = Session["Umail"].ToString();
mail.To.Add(to);
mail.IsBodyHtml = true;
mail.SubjectEncoding = Encoding.UTF8;
mail.BodyEncoding = Encoding.GetEncoding("utf-8");
mail.Subject = "subject";
mail.Body = "body" ;
SmtpClient smtp = new SmtpClient("Smtp.gmail.Com", 587);
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("[email protected]", "pass"); smtp.Send(mail);
但我想要一个自定义和美丽的邮件。就像从Facebook,谷歌团队等发送的电子邮件一样。我知道可以在mail.Body
中使用html标签,但它是一种好方法吗?什么是最好的方法 ?
这是准备使用的代码片段,我用来发送包含文字内容以及基于HTML模板的内容的电子邮件:
// first we create a plain text version and set it to the AlternateView
// then we create the HTML version
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]", "From Name");
msg.Subject = "Subject";
msg.To.Add("[email protected]");
msg.BodyEncoding = Encoding.UTF8;
String plainBody = "Body of plain email";
//first we create the text version
AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainBody, Encoding.UTF8, "text/plain");
msg.AlternateViews.Add(plainView);
//now create the HTML version
MailDefinition message = new MailDefinition();
message.BodyFileName = "~/MailTemplates/template1.htm";
message.IsBodyHtml = true;
message.From = "[email protected]";
message.Subject = "Subject";
//Build replacement collection to replace fields in template1.htm file
ListDictionary replacements = new ListDictionary();
replacements.Add("<%USERNAME%>", "ToUsername");//example of dynamic content for Username
//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.
MailMessage msgHtml = message.CreateMailMessage("[email protected]", replacements, new LiteralControl());
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, Encoding.UTF8, "text/html");
//example of a linked image
LinkedResource imgRes = new LinkedResource(Server.MapPath("~/MailTemplates/images/imgA.jpg"), System.Net.Mime.MediaTypeNames.Image.Jpeg);
imgRes.ContentId = "imgA";
imgRes.ContentType.Name = "imgA.jpg";
imgRes.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imgRes);
msg.AlternateViews.Add(htmlView);
//sending prepared email
SmtpClient smtp = new SmtpClient();//It reads the SMPT params from Web.config
smtp.Send(msg);
,这些都是HTML模板的关键部分:
<p>Username: <%USERNAME%></p>
<img src="cid:imgA"/>
感谢您的回答,我想要的这段代码... 我测试了它,它运行良好,但是当我想在邮件的顶部使用图片时,在gmail中它只附加它并在yahoo邮件中附加并在底部添加图片的邮件。 为什么我的邮件没有在正确的位置添加此图片? – NASRIN 2013-05-05 06:05:23
我认为这是你的模板的问题。我刚刚测试过这段代码,发送到Gmail账户,结果是图像被放置在正确的位置(我改变了它的位置)。在我的模板中,img标签被放到html表格中,以便定位它(旧的方式,而不是css)。我会提到LinkedResource将图像嵌入到电子邮件中(而不是通过外部URL)。 – Bronek 2013-05-05 10:36:31
我想要一个自定义和漂亮的邮件。
我知道可以在
mail.Body
中使用html标签,但它是好方法吗?什么是 最好的方法?
我不知道究竟那是什么意思,但通常有两种方法来做到这一点。 (如果我们谈论电子邮件中的图像或源代码等)。
您可以使用.NET Framework的类LinkedResource
。
表示电子邮件附件中的嵌入式外部资源,如 作为HTML附件中的图像。
或者,更简单地在我看来,如果你想在你的电子邮件使用一些图片,把图像中的公共位置,然后只是引用电子邮件的HTML该位置。
是的,您应该使用HTML描述。我不确定你还期待什么其他答案? – 2013-05-04 11:21:55
我想用这样的风格和形象。我认为在c#代码中使用大量的html和css标签并不好。我可以创建单独的html文件并附加它? – NASRIN 2013-05-04 11:27:25
不,您必须将这些样式包含在电子邮件本身中。有一个这样的阅读:http://css-tricks.com/using-css-in-html-emails-the-real-story/ – 2013-05-04 11:37:31