c# SendMail发送邮件实例代码
本文导语: 代码如下:using System;using System.Collections.Generic;using System.Net;using System.Net.Mail;using System.Text; namespace Common{ /// /// 基于system.net.mail发送邮件,支持附件 /// public class NetSendMail { public static void MailSend...
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;
namespace Common
{
///
/// 基于system.net.mail发送邮件,支持附件
///
public class NetSendMail
{
public static void MailSend(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList mailTo, IList mailCC, IList mailBCC, string mailTitle, string mailContent, IList mailAttachments, System.Text.Encoding encoding, bool isBodyHtml)
{
MailMessage message = new MailMessage();
if (mailFrom.Trim() == "")
{
throw new Exception("发送邮件不可以为空");
}
message.From = new MailAddress(mailFrom);
if (mailTo.Count 0)
{
foreach (string s in mailCC)
{
message.CC.Add(new MailAddress(s));
}
}
if (mailBCC.Count > 0)
{
foreach (string s in mailBCC)
{
message.Bcc.Add(new MailAddress(s));
}
}
message.Subject = mailTitle;
message.Body = mailContent;
message.BodyEncoding = encoding; //邮件编码
message.IsBodyHtml = isBodyHtml; //内容格式是否是html
message.Priority = MailPriority.High; //设置发送的优先集
//附件
foreach (string att in mailAttachments)
{
message.Attachments.Add(new Attachment(att));
}
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = mailSmtpServer;
smtpClient.Credentials = new NetworkCredential(maiFromlAccount, mailFromPwd);
smtpClient.Timeout = 1000;
smtpClient.EnableSsl = false; //不使用ssl连接
smtpClient.Send(message);
}
public static void MailSendText(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList mailTo, IList mailCC, IList mailBCC, string mailTitle, string mailContent)
{
List attList = new List();
MailSend(mailFrom, maiFromlAccount, mailFromPwd, mailSmtpServer, mailTo, mailCC, mailBCC, mailTitle, mailContent, attList, Encoding.UTF8, false);
}
public static void MailSendHTML(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList mailTo, IList mailCC, IList mailBCC, string mailTitle, string mailContent)
{
List attList = new List();
MailSend(mailFrom, maiFromlAccount, mailFromPwd, mailSmtpServer, mailTo, mailCC, mailBCC, mailTitle, mailContent, attList, Encoding.UTF8, true);
}
}
}
您可能感兴趣的文章:
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。