C#.NET使用HTML模板发送电子邮件
来源:互联网 发布:java sql解析器 开源 编辑:程序博客网 时间:2024/06/06 00:24
要使用html模板进行发送邮件,需要准备以下几项工作:
1)HTML模板
2)替换函数(替换模板中绑定的变量)
3)邮件函数(发送邮件)
一、HTML模板
01.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 02.<html xmlns="http://www.w3.org/1999/xhtml" > 03.<head> 04. <title>HTML Report</title> 05.</head> 06.<body> 07.<p >$USER_NAME$:</p> 08. 09. 10.<p>My name is $NAME$</p> 11.<p >This is a Test Email,<br /> 12. $MY_NAME$</p> 13.</body> 14.</html>
其中USER_NAME、NAME、MY_NAME这三个变量用$符号包裹进行标识,是需要被替换的字符串,它会在下面的替换函数中被动态替换。
二、替换函数
01./// <summary> 02.///替换模板中的字段值 03./// </summary> 04.public string ReplaceText(String userName,string name,string myName) 05.{ 06. 07. string path = string.Empty; 08. 09. path = HttpContext.Current.Server.MapPath("EmailTemplate\\emailTemplate.html"); 10. 11. if (path == string.Empty) 12. { 13. return string.Empty; 14. } 15. System.IO.StreamReader sr = new System.IO.StreamReader(path); 16. string str = string.Empty; 17. str = sr.ReadToEnd(); 18. str = str.Replace("$USER_NAME$", userName); 19. str = str.Replace("$NAME$", name); 20. str = str.Replace("$MY_NAME$",myName); 21. 22. return str; 23.}
三、邮件发送
01. /// <summary> 02. /// 发送邮件 03. /// </summary> 04. public void SendEmail(string email_from,string email_to, string email_cc, string userName, string name, string myName) 05. { 06. 07. try 08. { 09. // 建立一个邮件实体 10. MailAddress from = new MailAddress(email_from); 11. 12. 13. MailAddress to = new MailAddress(email_to); 14. MailMessage message = new MailMessage(from, to); 15. 16. string strbody = ReplaceText(userName, name, myName); 17. 18. if (email_cc.ToString() != string.Empty) 19. { 20. foreach (string ccs in email_cc.Split(';')) 21. { 22. MailAddress cc = new MailAddress(ccs); 23. message.CC.Add(cc); 24. } 25. } 26. 27. message.IsBodyHtml = true; 28. message.BodyEncoding = System.Text.Encoding.UTF8; 29. message.Priority = MailPriority.High; 30. message.Body = strbody; //邮件BODY内容 31. message.Subject = "Subject"; 32. 33. SmtpClient smtp = new SmtpClient(); 34. smtp.Host = Configuration.MailHost; 35. smtp.Port = Configuration.MailHostPort; 36. smtp.Credentials = new System.Net.NetworkCredential(email_from, "emailpassword"); 37. 38. smtp.Send(message); //发送邮件 39. 40. } catch (Exception ex) 41. { 42. throw ex; 43. } 44. 45. }
其实无论采取什么方式或组件进行邮件发送,要替换HTML模板中的内容,只需一个Replace函数即可。
- C#.NET使用HTML模板发送电子邮件
- asp.net 使用html模板发送电子邮件
- asp.net 使用html模板发送电子邮件
- Asp.net(c#)发送电子邮件
- Asp.net(c#)发送电子邮件
- Asp.net(c#)发送电子邮件
- Asp.net(c#)发送电子邮件
- ASP.NET(C#)发送电子邮件的方法
- 使用asp.net通过gmail发送电子邮件
- 使用 System.Net.Mail.MailMessage 发送电子邮件
- 使用 System.Net.Mail.MailMessage 发送电子邮件
- 使用System.Net.Mail发送电子邮件
- 使用 System.Net.Mail.MailMessage 发送电子邮件
- 使用 System.Net.Mail.MailMessage 发送电子邮件
- 使用jmail组件发送电子邮件(C#)
- 使用jmail组件发送电子邮件(C#)
- ASP.NET发送电子邮件
- .net 2.0发送电子邮件
- 细说Sybase数据库日志
- C++学习笔记之操作符使用注意。
- 使用Ctrl + F5清除varnish缓存
- Log4j学习笔记(1)_log4j 基础架构&配置项解析
- oracle远程导入导出
- C#.NET使用HTML模板发送电子邮件
- Linux的中断处理:work_struct的使用实例
- linux下进程容纳多少线程数
- windows下oracle修改参数ORA-02095错误
- 用SurfaceView,Timer以及Handler实现的一个带有音乐和文字的电子相册,可以自动播放~
- 给编程开发者建议的书籍
- ARM7,ARM9,cortex-m3,cortex-m4,cortex-a8的区别
- Objective-C语法小总结
- 常见的MySQL性能测试工具