首页 > 代码库 > C#实现简单的邮件发送功能

C#实现简单的邮件发送功能

     唉,最近要做一个项目,里面需要实现邮件发送功能。在网络上也找一些看,自己也随便写下。也当做是给自己复习下吧,如有不对之处还请大家指出。谢谢!

     首先我是创建一个实体对象Model (EmailParameterSet) :

 1  public  class EmailParameterSet 2     { 3         /// <summary> 4         /// 收件人的邮件地址  5         /// </summary> 6         public string ConsigneeAddress { get; set; } 7  8         /// <summary> 9         /// 收件人的名称10         /// </summary>11         public string ConsigneeName { get; set; }12 13         /// <summary>14         /// 收件人标题15         /// </summary>16         public string ConsigneeHand { get; set; }17 18         /// <summary>19         /// 收件人的主题20         /// </summary>21         public string ConsigneeTheme { get; set; }22 23         /// <summary>24         /// 发件邮件服务器的Smtp设置25         /// </summary>26         public string SendSetSmtp { get; set; }27 28         /// <summary>29         /// 发件人的邮件30         /// </summary>31         public string SendEmail { get; set; }32 33         /// <summary>34         /// 发件人的邮件密码35         /// </summary>36         public string SendPwd { get; set; }37         /// <summary>38         /// 发件内容39         /// </summary>40         public string SendContent { get; set; }41     }

方法也简单  如果发送成功就返回一个bool值 

 

 1  public bool MailSend(EmailParameterSet EPSModel )  2         { 3             try 4             { 5                 //确定smtp服务器端的地址,实列化一个客户端smtp  6                 System.Net.Mail.SmtpClient sendSmtpClient = new System.Net.Mail.SmtpClient(EPSModel.SendSetSmtp);//发件人的邮件服务器地址 7                 //构造一个发件的人的地址 8                 System.Net.Mail.MailAddress sendMailAddress = new MailAddress(EPSModel.SendEmail, EPSModel.ConsigneeHand, Encoding.UTF8);//发件人的邮件地址和收件人的标题、编码 9 10                 //构造一个收件的人的地址11                 System.Net.Mail.MailAddress consigneeMailAddress = new MailAddress(EPSModel.ConsigneeAddress, EPSModel.ConsigneeName, Encoding.UTF8);//收件人的邮件地址和收件人的名称 和编码12 13                 //构造一个Email对象14                 System.Net.Mail.MailMessage mailMessage = new MailMessage(sendMailAddress, consigneeMailAddress);//发件地址和收件地址15                 mailMessage.Subject = EPSModel.ConsigneeTheme;//邮件的主题16                 mailMessage.BodyEncoding = Encoding.UTF8;//编码17                 mailMessage.SubjectEncoding = Encoding.UTF8;//编码18                 mailMessage.Body = EPSModel.SendContent;//发件内容19                 mailMessage.IsBodyHtml = false;//获取或者设置指定邮件正文是否为html20 21                 //设置邮件信息 (指定如何处理待发的电子邮件)22                 sendSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定如何发邮件 是以网络来发23                 sendSmtpClient.EnableSsl = false;//服务器支持安全接连,安全则为true24 25                 sendSmtpClient.UseDefaultCredentials = false;//是否随着请求一起发26 27                 //用户登录信息28                 NetworkCredential myCredential = new NetworkCredential(EPSModel.SendEmail, EPSModel.SendPwd);29                 sendSmtpClient.Credentials = myCredential;//登录30                 sendSmtpClient.Send(mailMessage);//发邮件31                 return true;//发送成功32             }33             catch (Exception)34             {35                 return false;//发送失败36             }37         }

然后就调用就可以。

 1   private void button3_Click(object sender, EventArgs e) 2         { 3             EmailParameterSet model = new EmailParameterSet(); 4             model.SendEmail = "whisert3@163.com"; 5             model.SendPwd = "xxx";//密码 6             model.SendSetSmtp = "smtp.163.com";//发送的SMTP服务地址 ,每个邮箱的是不一样的。。根据发件人的邮箱来定 7             model.ConsigneeAddress = "likeneg@126.com"; 8             model.ConsigneeTheme = "主题"; 9             model.ConsigneeHand = "标题";10             model.ConsigneeName = "李可能";11             model.SendContent = "htpp://www.baidu.com";12             if (MailSend(model)== true)13             {14                 MessageBox.Show("邮件发送成功!");15             }16             else17             {18                 MessageBox.Show("邮件发送失败!");19             }20         }