/// <summary>
/// 发送邮件
/// </summary>
/// <param name="fromAddr">发件人邮箱</param>
/// <param name="displayName_from">发件人名称</param>
/// <param name="toAddr">收件人邮箱</param>
/// <param name="displayName_to">收件人名称</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件正文</param>
/// <param name="file">附件</param>
/// <param name="SMTPHost">发送送邮件服务器</param>
/// <param name="pwd">发件人密码</param>
/// <returns>发送成功返回true,否则返回false</returns>
public static bool send(string fromAddr,string displayName_from, string toAddr,string displayName_to, string subject, string body, string file, string SMTPHost, string pwd)
{
MailAddress from = new MailAddress(fromAddr, displayName_from);
MailAddress to = new MailAddress(toAddr, displayName_to);
MailMessage oMail = new MailMessage(from, to);
添加附件
if (file != null && file.Trim().Length != 0)
{
oMail.Attachments.Add(new Attachment(file));
}
oMail.Subject = subject;
oMail.Body = body;
邮件格式
oMail.IsBodyHtml = false;
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8");
设置邮件的优先级为高
oMail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Host = SMTPHost;
client.Credentials = new NetworkCredential(fromAddr, pwd);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(oMail);
return true;
}
catch
{
return false;
}
finally
{
oMail.Dispose();
}
}
C#发送邮件
最新推荐文章于 2018-04-17 09:34:33 发布