private static string emailAccount = WebConfigurationManager.AppSettings["emailAccount"];
private static string emailPwd = WebConfigurationManager.AppSettings["emailPwd"];
private static string emailCode = WebConfigurationManager.AppSettings["emailCode"];
private static string emailPOP3 = WebConfigurationManager.AppSettings["emailPOP3"];
private static string emailSMTP = WebConfigurationManager.AppSettings["emailSMTP"];
private static string emailIMAP = WebConfigurationManager.AppSettings["emailIMAP"];
1 普通发送邮件
public static void SendMailByEmail(string[] file,string subject = "", string body = "", List<string> receivers = null)
{
MailMessage message = new MailMessage();
MailAddress fromAddr = new MailAddress(emailAccount);
message.From = fromAddr;
foreach (var item in receivers)
{
message.To.Add(item);
}
foreach (var item in file)
{
if (string.IsNullOrEmpty(item))
continue;
message.Attachments.Add(new Attachment(item));
}
message.Subject = subject;
message.SubjectEncoding = Encoding.UTF8;
message.Body = body;
//允许html标签
message.IsBodyHtml = true;
message.BodyEncoding = Encoding.UTF8;
SmtpClient client = new SmtpClient(emailSMTP, 25);
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential(emailAccount, emailCode);
client.EnableSsl = true;
//避免远程证书无效
ServicePointManager.ServerCertificateValidationCallback =
delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };
client.Send(message);
}
2 使用默认邮件发送
public static void SendEmail_Process(string subject = "", string body = "", string receivers = "")
{
//Process.Start(new ProcessStartInfo("mailto:user@126.com?subject=test&body=Dear:%0A%0DThis is a test mail."));
string email = string.Format("mailto:{0}?subject={1}&body={2}"
, receivers
, subject
, body);
Process.Start(new ProcessStartInfo(email));
}