利用C#邮件发送邮箱使用到两个类SmtpClient和MailMessage。可以把SmtpClient看做发送邮件信息的客户端,而把MailMessage看做需要发送的消息。
下面是我写的发送邮件的公共方法:
/// <summary>
///
/// </summary>
/// <param name="ServerIP">发送邮件服务器ip或者域名</param>
/// <param name="ServerPort">服务器端口</param>
/// <param name="UserName">发件人的账号</param>
/// <param name="Password">发件人的密码</param>
/// <param name="FromAddress">发件人的邮箱地址</param>
/// <param name="FromAddressName">发件人名称</param>
/// <param name="Subject">主题</param>
/// <param name="Body">正文</param>
/// <param name="ToAddressList">收件人集合</param>
/// <param name="CCAddressList">副本收件人集合</param>
/// <param name="BccAddressList">密件抄送收件人集合</param>
/// <param name="IsHtml">正文是否可以使用html标签</param>
/// <param name="AttachMentFileList">附件</param>
/// <param name="MailPriority">发送邮件的优先级 三个选项:低 中 高</param>
/// <returns></returns>
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)
{
try
{
if (string.IsNullOrEmpty(ServerIP))
{
return "ServerIP 不能为空!";
} SmtpClient mSmtpClient = new SmtpClient(ServerIP); //申明SmtpClient对象
System.Net.Mail.MailMessage mMailMessage = new System.Net.Mail.MailMessage(); //申明MailMessage对象 if (ServerPort != null)
{
mSmtpClient.Port = (int)ServerPort;
} if (String.IsNullOrEmpty(FromAddress))
{
return "发件人不能为空";
} if (string.IsNullOrEmpty(FromAddressName))
{
mMailMessage.From = new MailAddress(FromAddress);
}
else
{
mMailMessage.From = new MailAddress(FromAddress, FromAddressName);
} if (string.IsNullOrEmpty(Subject))
{
return "主旨不能为空!";
} if (ToAddressList == null || ToAddressList.Count() == )
{
return "收件人不能为空!";
} // mSmtpClient.UseDefaultCredentials = false;
mSmtpClient.Credentials = new NetworkCredential(UserName, Password); //设置发送人的邮箱账号和密码,163邮箱有设置授权码
mSmtpClient.EnableSsl = true; //启用ssl,也就是安全发送 foreach (var item in ToAddressList) //收件者
{
mMailMessage.To.Add(item);
}
if (CCAddressList != null && CCAddressList.Count() != )
{
foreach (var item in CCAddressList) //副本
{
mMailMessage.CC.Add(item);
}
} if (BccAddressList != null && BccAddressList.Count() != )
{
foreach (var item in BccAddressList) //密件抄送人员
{
mMailMessage.Bcc.Add(item);
}
} mMailMessage.Subject = Subject;
mMailMessage.IsBodyHtml = IsHtml; //正文是否使用html标签展示
mMailMessage.Body = Body; //正文
mMailMessage.BodyEncoding = System.Text.Encoding.UTF8; //正文的编码原则
mMailMessage.Priority = MailPriority; //优先级 if (AttachMentFileList != null && AttachMentFileList.Count() != ) //判断是否有附件
{
foreach (var item in AttachMentFileList)
{
Attachment mAttachment = new Attachment(item.FilePath); //添加文件的文职
mAttachment.Name = item.FileName;//附件显示的文件名称
mAttachment.NameEncoding = System.Text.Encoding.UTF8;
mAttachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
mAttachment.ContentDisposition.Inline = true;
mAttachment.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Attachment;
mMailMessage.Attachments.Add(mAttachment);
}
} mSmtpClient.Send(mMailMessage);
return "发送成功!"; }
catch (Exception ex)
{
return ex.Message;
}
} public string SendMail(String ServerIP, string UserName, string Password, string FromAddress, string FromAddressName, string Subject, string Body, List<string> ToAddressList)
{
return SendMail(ServerIP, null, UserName, Password, FromAddress, FromAddressName, Subject, Body, ToAddressList, null, null, false, null, MailPriority.Normal);
}
}
public class AttachmentFile
{
/// <summary>
/// 文件路径
/// </summary>
public string FilePath { get; set; }
/// <summary>
///附件显示的文件明
/// </summary>
public string FileName { get; set; }
}
上面的代码可以直接使用。基本的方法都有,在这儿需要说明下AttachmentFile类是我用于发送文件的时候可以重新命名文件名。
注意:大家在使用的时候注意一下参数UserName和Password,在这儿我以我163的邮箱为例,解释下这两个参数的作用。
在使用163邮箱服务器发送信息时需要按照图片上的操作开启客户端授权密码,上面的UserName和Password就是我们的邮箱地址和授权码。至于QQ邮箱我有试验过,但是就是无法发送邮件,至于你们说的开启POP3和SMTP的选项,我也已经开启了,如果知道一些原因的话,可以在下面留言告诉我!感激!
希望能帮到大家!