.NET发送邮件

首先导入using System.Net.Mail;
public void SendEmail(string sendToList, string attachment, string subject, string mailBody)
{


MailMessage mail = new MailMessage(); sendToList = sendToList.Trim().TrimEnd(';');
if (sendToList.Length == 0)
{ throw new Exception("程序没有获取到文件的收件人列表,请检查是否没有设置!"); }
mail.From = new MailAddress(" [email protected]");
foreach (string to in sendToList.Split(';'))
{
mail.To.Add(to); }
mail.IsBodyHtml = false;
mail.Subject = subject;
mail.Body = mailBody;
mail.SubjectEncoding = Encoding.GetEncoding("gb2312");
mail.BodyEncoding = Encoding.GetEncoding("gb2312");
if (attachment.Length > 0)
{ mail.Body = mailBody + "\n\r随邮件带附件:" + attachment;
if (File.Exists(attachment))
{
Attachment mailAttach = new Attachment(attachment);
mail.Attachments.Add(mailAttach);
}
}
SmtpClient smtp = new SmtpClient("smtp.163.com");
smtp.Credentials = new System.Net.NetworkCredential(" [email protected]", "密码");
smtp.Send(mail);
mail.Dispose();
}

你可能感兴趣的:(.net,exception,String)