做asp.net的网站被要求要加入一个邮件自动发送的功能,同时还要加入附件网上搜索了很多,自己记录总结一下
废话说完先贴代码:
- public class MyMail
- {
- private MyMail(string title, string content, string from, string to)
- {
- this.Title = title;
- this.Content = content;
- this.SendFrom = from;
- this.SendTo = to;
- this.IsHTML = false;
- this.Priority = System.Net.Mail.MailPriority.Normal;
- this.Accessories = new List<string>();
- }
- /// <summary>
- /// 创建封邮件
- /// </summary>
- /// <param name="title">标题</param>
- /// <param name="content">正文</param>
- /// <param name="from">发件邮箱</param>
- /// <param name="to">收件邮箱</param>
- /// <returns></returns>
- public static MyMail CreateMail(
- string title, string content, string from, string to)
- {
- return new MyMail(title, content, from, to);
- }
- /// <summary>
- /// 添加附件
- /// </summary>
- /// <param name="path">附件存放路径</param>
- public void AddAccessory(string path)
- {
- this.Accessories.Add(path);
- }
- /// <summary>
- /// 移除添加的附件
- /// </summary>
- /// <param name="path">附件存放路径</param>
- public void RemoveAccessory(string path)
- {
- this.Accessories.Remove(path);
- }
- /// <summary>
- /// 邮件标题
- /// </summary>
- public string Title { get; set; }
- /// <summary>
- /// 正文内容
- /// </summary>
- public string Content { get; set; }
- /// <summary>
- /// 发件邮箱地址
- /// </summary>
- public string SendFrom { get; set; }
- /// <summary>
- /// 收件邮箱地址
- // </summary>
- public string SendTo { get; set; }
- /// <summary>
- /// 正文格式是否为Html
- /// </summary>
- public bool IsHTML { get; set; }
- /// <summary>
- /// 邮件优先级
- /// </summary>
- public System.Net.Mail.MailPriority Priority { get; set; }
- /// <summary>
- /// 携带附件存放路径
- /// </summary>
- public List<string> Accessories { get; set; }
- }
- public class MailServer
- {
- private MailServer(string server,string usr,string passwnd)
- {
- this.Port = 25;
- this.TimeOut=9999;
- this.ServerHost = server;
- this.UserName = usr;
- this.PassWnd = passwnd;
- }
- /// <summary>
- /// 创建一个邮件发送服务
- /// </summary>
- /// <param name="server"服务></param>
- /// <param name="usr">账户登录名</param>
- /// <param name="passwnd">密码</param>
- /// <returns></returns>
- public static MailServer CreateMailServer(
- string server, string usr, string passwnd)
- {
- return new MailServer(server, usr, passwnd);
- }
- /// <summary>
- /// smtp服务地址
- /// </summary>
- public string ServerHost { get; set; }
- /// <summary>
- /// 登录用户名
- /// </summary>
- public string UserName { get; set; }
- /// <summary>
- /// 登录密码
- /// </summary>
- public string PassWnd { get; set; }
- /// <summary>
- /// 端口号默认不用设置为25
- /// </summary>
- public int Port { get; set; }
- /// <summary>
- /// 超时设定
- /// </summary>
- public int TimeOut { get; set; }
- }
- public class MailSend
- {
- protected System.Net.Mail.SmtpClient _client;
- public MailSend(MailServer server)
- {
- _client= new System.Net.Mail.SmtpClient(server.ServerHost,server.Port);
- _client.Credentials = new System.Net.NetworkCredential(
- server.UserName,server.PassWnd);
- _client.Timeout = server.TimeOut;
- }
- public bool Send(MyMail mymail)
- {
- try
- {
- //邮件发送人地址
- System.Net.Mail.MailAddress from =
- new System.Net.Mail.MailAddress(mymail.SendFrom);
- //收件人地址
- System.Net.Mail.MailAddress to =
- new System.Net.Mail.MailAddress(mymail.SendTo);
- System.Net.Mail.MailMessage mail =
- new System.Net.Mail.MailMessage(mymail.SendFrom, mymail.SendTo);
- mail.Subject = mymail.Title;
- mail.Body = mymail.Content;
- mail.IsBodyHtml = mymail.IsHTML;
- //添加附件
- foreach (string accessory in mymail.Accessories)
- {
- System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(accessory);
- mail.Attachments.Add(data);
- }
- _client.Send(mail);
- mail.Dispose();
- return true;
- }
- catch
- {
- return false;
- }
- }
- }
附上三个类的类图:
再给个实际调用的例子:
- MailServer server =
- MailServer.CreateMailServer(
- "smtp.163.com",
- " sender@163.com ",
- "******");//此处填写stmp服务提供商、登录帐号、密码
- MyMail mail=MyMail.CreateMail(
- "测试邮件标题",
- "你好这是一封测试邮件",
- "sender@163.com",
- "receiver@sina.com"); //此处填写邮件标题、正文、发送者邮箱和接受者邮箱
- mail.AddAccessory(@"E:\Winter.jpg");//此处用于添加本地路径附件
- MailSend mailsender = new MailSend(server);
- if (mailsender.Send(mail))
- {
- //邮件发送成功
- }
- else
- {
- //失败处理
- }
注意:邮件发送者必须与服务提供商是同一个。例如:发送者邮箱为163的邮箱,登录名、密码和stmp服务提供商就必须是163的。
另外有些邮箱,例如qq邮箱必须先登录邮箱手动开启stmp服务!!!