C# 发送email邮件!
利用C#邮件发送邮箱使用到两个类SmtpClient和MailMessage。可以把SmtpClient看做发送邮件信息的客户端,而把MailMessage看做需要发送的消息。
下面是我写的发送邮件的公共方法:
1 /// <summary> 2 /// 3 /// </summary> 4 /// <param name="ServerIP">发送邮件服务器ip或者域名</param> 5 /// <param name="ServerPort">服务器端口</param> 6 /// <param name="UserName">发件人的账号</param> 7 /// <param name="Password">发件人的密码</param> 8 /// <param name="FromAddress">发件人的邮箱地址</param> 9 /// <param name="FromAddressName">发件人名称</param> 10 /// <param name="Subject">主题</param> 11 /// <param name="Body">正文</param> 12 /// <param name="ToAddressList">收件人集合</param> 13 /// <param name="CCAddressList">副本收件人集合</param> 14 /// <param name="BccAddressList">密件抄送收件人集合</param> 15 /// <param name="IsHtml">正文是否可以使用html标签</param> 16 /// <param name="AttachMentFileList">附件</param> 17 /// <param name="MailPriority">发送邮件的优先级 三个选项:低 中 高</param> 18 /// <returns></returns> 19 public String SendMail(string ServerIP, int? ServerPort, string UserName, string Password, string FromAddress, string FromAddressName, string Subject, string Body, List<string> ToAddressList, List<string> CCAddressList, List<string> BccAddressList, bool IsHtml, List<AttachmentFile> AttachMentFileList, MailPriority MailPriority) 20 { 21 try 22 { 23 if (string.IsNullOrEmpty(ServerIP)) 24 { 25 return "ServerIP 不能为空!"; 26 } 27 28 SmtpClient mSmtpClient = new SmtpClient(ServerIP); //申明SmtpClient对象 29 System.Net.Mail.MailMessage mMailMessage = new System.Net.Mail.MailMessage(); //申明MailMessage对象 30 31 if (ServerPort != null) 32 { 33 mSmtpClient.Port = (int)ServerPort; 34 } 35 36 if (String.IsNullOrEmpty(FromAddress)) 37 { 38 return "发件人不能为空"; 39 } 40 41 if (string.IsNullOrEmpty(FromAddressName)) 42 { 43 mMailMessage.From = new MailAddress(FromAddress); 44 } 45 else 46 { 47 mMailMessage.From = new MailAddress(FromAddress, FromAddressName); 48 } 49 50 if (string.IsNullOrEmpty(Subject)) 51 { 52 return "主旨不能为空!"; 53 } 54 55 if (ToAddressList == null || ToAddressList.Count() == 0) 56 { 57 return "收件人不能为空!"; 58 } 59 60 // mSmtpClient.UseDefaultCredentials = false; 61 mSmtpClient.Credentials = new NetworkCredential(UserName, Password); //设置发送人的邮箱账号和密码,163邮箱有设置授权码 62 mSmtpClient.EnableSsl = true; //启用ssl,也就是安全发送 63 64 foreach (var item in ToAddressList) //收件者 65 { 66 mMailMessage.To.Add(item); 67 } 68 if (CCAddressList != null && CCAddressList.Count() != 0) 69 { 70 foreach (var item in CCAddressList) //副本 71 { 72 mMailMessage.CC.Add(item); 73 } 74 } 75 76 if (BccAddressList != null && BccAddressList.Count() != 0) 77 { 78 foreach (var item in BccAddressList) //密件抄送人员 79 { 80 mMailMessage.Bcc.Add(item); 81 } 82 } 83 84 mMailMessage.Subject = Subject; 85 mMailMessage.IsBodyHtml = IsHtml; //正文是否使用html标签展示 86 mMailMessage.Body = Body; //正文 87 mMailMessage.BodyEncoding = System.Text.Encoding.UTF8; //正文的编码原则 88 mMailMessage.Priority = MailPriority; //优先级 89 90 if (AttachMentFileList != null && AttachMentFileList.Count() != 0) //判断是否有附件 91 { 92 foreach (var item in AttachMentFileList) 93 { 94 Attachment mAttachment = new Attachment(item.FilePath); //添加文件的文职 95 mAttachment.Name = item.FileName;//附件显示的文件名称 96 mAttachment.NameEncoding = System.Text.Encoding.UTF8; 97 mAttachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; 98 mAttachment.ContentDisposition.Inline = true; 99 mAttachment.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Attachment; 100 mMailMessage.Attachments.Add(mAttachment); 101 } 102 } 103 104 mSmtpClient.Send(mMailMessage); 105 return "发送成功!"; 106 107 } 108 catch (Exception ex) 109 { 110 return ex.Message; 111 } 112 } 113 114 public string SendMail(String ServerIP, string UserName, string Password, string FromAddress, string FromAddressName, string Subject, string Body, List<string> ToAddressList) 115 { 116 return SendMail(ServerIP, null, UserName, Password, FromAddress, FromAddressName, Subject, Body, ToAddressList, null, null, false, null, MailPriority.Normal); 117 } 118 } 119 public class AttachmentFile 120 { 121 /// <summary> 122 /// 文件路径 123 /// </summary> 124 public string FilePath { get; set; } 125 /// <summary> 126 ///附件显示的文件明 127 /// </summary> 128 public string FileName { get; set; } 129 }
上面的代码可以直接使用。基本的方法都有,在这儿需要说明下AttachmentFile类是我用于发送文件的时候可以重新命名文件名。
注意:大家在使用的时候注意一下参数UserName和Password,在这儿我以我163的邮箱为例,解释下这两个参数的作用。
在使用163邮箱服务器发送信息时需要按照图片上的操作开启客户端授权密码,上面的UserName和Password就是我们的邮箱地址和授权码。至于QQ邮箱我有试验过,但是就是无法发送邮件,至于你们说的开启POP3和SMTP的选项,我也已经开启了,如果知道一些原因的话,可以在下面留言告诉我!感激!
希望能帮到大家!