2.1引入System.Web.Mail;命名空间
PS:但该命名空间已过期!!
//引入using System.Web.Mail; Web应用程序的命名空间
//发送邮件的方法
public static void Send(string subject, string body, string from, string to, string cc, string smtp, string accountName, string accountPwd)
{
MailMessage mail = new MailMessage();
mail.From = from;
mail.Cc = cc;
mail.To = to;
mail.Subject = subject;
mail.Body = body;
mail.BodyFormat = MailFormat.Html;
//基本权限
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//用户名
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", accountName);
//密码
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", accountPwd);
SmtpMail.SmtpServer = smtp;
//开始发送邮件
SmtpMail.Send(mail);
}
//调用
string subjectFail = "邮件发送测试";
string cc_failMails = ConfigurationManager.AppSettings["SMS_CCMails"];
string body_failSMSHtml = "Hi,Hope sended";//body_failSMSHtml可以拼接成表格发送
try
{
MailUtil.Send(subjectFail, body_failSMSHtml.ToString(), "zhangyun@imxiaomai.com", "1176624306@qq.com", "", "smtp.exmail.qq.com", "zhangyun@imxiaomai.com", "123qaz");
}
catch (Exception error)
{
lbltip.Text = error.Message;
lbltip.Visible = true;
HttpContext.Current.Response.End();
return;
}
2.2引入System.Net.Mail;命名空间
PS:该方法最新
CodeDemo:
#region 发送邮件-最新
/// <summary>
/// 发送邮件-最新
/// </summary>
/// <param name="subject"></param>
/// <param name="body"></param>
/// <param name="from"></param>
/// <param name="to"></param>
/// <param name="cc"></param>
public static void NewSend(string subject, string body, string from, string to, string cc, string smtp, string accountName, string accountPwd)
{
MailAddress mfrom = new MailAddress(from);
MailAddress mto = new MailAddress(to);
MailMessage message = new MailMessage(from, to);
message.Subject = subject;
message.Body = body;
MailAddress copy = new MailAddress(cc);
message.CC.Add(copy);
SmtpClient client = new SmtpClient(smtp);
client.Credentials = new NetworkCredential(accountName, accountPwd);
try
{
client.Send(message);
}
catch (Exception ex)
{
throw;
}
}
#endregion
不抄送,收件人多人(收件人以;分割),部分code如下:
System.Net.Mail.MailAddress mfrom = new System.Net.Mail.MailAddress(from);
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.IsBodyHtml = true;
message.From = mfrom;
string []toMails = to.Split(';');
if (toMails != null && toMails.Length > 0)
{
for (int i = 0; i < toMails.Length; i++)
{
message.To.Add(new System.Net.Mail.MailAddress(toMails[i]));
}
}