using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace elder.BLL
{
public partial class sendmail
{
//成功开启POP3/SMTP服务,在第三方客户端登录时,密码框请输入以下授权码:
//oicbllypdhhzbehi
/// <summary>
/// 发送邮件(附件好像是不能超过50M)
/// </summary>
/// <param name="sendTo">收件人地址</param>
/// <param name="sendCC">抄送人地址(多人)</param>
/// <param name="fromEmail">发件人邮箱</param>
/// <param name="fromName">发件人名称</param>
/// <param name="fromCode">授权码(百度上有)</param>
/// <param name="title">邮件标题</param>
/// <param name="body">邮件内容(支持html格式)</param>
/// <param name="attachment">附件</param>
public static void SendEmail(String sendTo, string sendCC,
String fromEmail, String fromName,string fromCode, String title, String body,
List<string> attachment
)
{
if (string.IsNullOrEmpty(fromEmail)) { fromEmail = "xxxxxxx@qq.com"; }
if (string.IsNullOrEmpty(fromCode)) { fromCode = "oicbllypdhhzbehi"; }
if (string.IsNullOrEmpty(title)) { title = DateTime.Now.ToString("yyyyMMdd") + "数据备份"; }
if (string.IsNullOrEmpty(body)) { body = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); }
MailMessage msg = new MailMessage();
String[] sendCCArr = null;
//设置收件人地址
msg.To.Add(sendTo); //收件人地址
//设置抄送人地址
if (!string.IsNullOrEmpty(sendCC))
{
sendCCArr = sendCC.Split(';');
if (sendCCArr.Length > 0)
{
foreach (String cc in sendCCArr)
{
msg.CC.Add(cc);
}
}
}
//设置发件人邮箱及名称
msg.From = new MailAddress(fromEmail, fromName);
msg.Subject = title;//邮件标题
msg.SubjectEncoding = Encoding.UTF8; //标题格式为UTF8
msg.Body = body;//邮件内容
msg.BodyEncoding = Encoding.UTF8; //内容格式为UTF8
msg.IsBodyHtml = true;//设置邮件格式为html格式
//string filePath = @"E:\导出数据.xls";//添加附件
for(int i=0;i<attachment.Count;i++)
{
if(System.IO.File.Exists(attachment[i]))
{
msg.Attachments.Add(new Attachment(attachment[i]));
}
}
SmtpClient client = new SmtpClient();
//发送邮箱信息
client.Host = "smtp.qq.com"; //SMTP服务器地址
client.Port = 587; //SMTP端口,QQ邮箱填写587
client.EnableSsl = true; //启用SSL加密 (使用除QQ邮箱之外的最好关闭)
client.Timeout = 9999999;
//发件人邮箱账号,授权码
//授权码获取请自行百度
client.Credentials = new System.Net.NetworkCredential(fromEmail,fromCode);
try
{
client.Send(msg); //发送邮件
// client.SendAsync(msg, fromCode);
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// 邮箱格式校验
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
private bool EmailFormat(string address)
{
Regex r = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$");
if (r.IsMatch(address))
{
return true;
}
else
{
return false;
}
}
}
}