using MailKit.Net.Smtp;
using MimeKit;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class EmailEntity
{
/// <summary>
/// 同步发送邮件
/// </summary>
/// <param name="email">邮箱</param>
/// <param name="subject">标题</param>
/// <param name="message">文本</param>
/// <param name="filesPath">附件路径集合</param>
/// <param name="IsPostFiles">是否上传附件</param>
public static void Send(string email, string subject, string message, List<string> filesPath, bool IsPostFiles=true)
{
var emailMessage = new MimeMessage();
//发件人
emailMessage.From.Add(new MailboxAddress("Name", email));
//收件人
emailMessage.To.Add(new MailboxAddress("名称", "*************@****.***"));
emailMessage.To.Add(new MailboxAddress("名称", "***************@****.***"));
//标题
emailMessage.Subject = subject;
//抄送
emailMessage.Cc.Add(new MailboxAddress("*************@****.***"));
emailMessage.Cc.Add(new MailboxAddress("*************@****.***"));
//附件
//正文
var html = new TextPart("html")
{
Text = message,
};
var alternative = new Multipart("alternative");
alternative.Add(html);
//附件
var multipart = new Multipart("mixed");
multipart.Add(alternative);
if (IsPostFiles)
{
IList<MimePart> multiPartList = GetMimePartList(filesPath);
foreach (var item in multiPartList)
{
multipart.Add(item);
}
}
emailMessage.Body = multipart;
using (var client = new SmtpClient())
{
try
{
client.Connect("smtp.qiye.163.com", 465, true);
//用指定邮箱发送 需要密码账户
client.Authenticate("发送邮件地址", "密码");
client.Send(emailMessage);
client.Disconnect(true);
}
catch (Exception ex)
{
throw;
}
}
}
/// <summary>
/// 异步发送邮件
/// </summary>
/// <param name="email">邮件地址</param>
/// <param name="subject">邮件主题</param>
/// <param name="message">邮件信息</param>
/// <param name="filesPath">文件路径</param>
/// <param name="IsPostFiles">是否提交文件</param>
/// <returns></returns>
public static async Task SendEmailAsync(string email, string subject, string message, List<string> filesPath, bool IsPostFiles = true)
{
try
{
var emailMessage = new MimeMessage();
//发件人
emailMessage.From.Add(new MailboxAddress("Name", "发件人地址"));
//收件人
// emailMessage.To.Add(new MailboxAddress("jiwei", "收件人地址"));
// emailMessage.To.Add(new MailboxAddress("haining", "收件人地址"));
emailMessage.To.Add(new MailboxAddress("LA", email));
//标题
emailMessage.Subject = subject;
//抄送
// emailMessage.Cc.Add(new MailboxAddress("抄送人邮件地址"));
// emailMessage.Cc.Add(new MailboxAddress("抄送人邮件地址"));
//附件
//正文
var html = new TextPart("html")
{
Text = message,
};
var alternative = new Multipart("alternative");
alternative.Add(html);
//附件
var multipart = new Multipart("mixed");
multipart.Add(alternative);
if (IsPostFiles)
{
if (filesPath.Count > 0)
{
IList<MimePart> multiPartList = GetMimePartList(filesPath);
foreach (var item in multiPartList)
{
multipart.Add(item);
}
}
}
emailMessage.Body = multipart;
using (var client = new SmtpClient())
{
await client.ConnectAsync("smtp.qiye.163.com", 465).ConfigureAwait(false);
await client.AuthenticateAsync("邮件发送指定邮箱地址", "邮箱密码");
await client.SendAsync(emailMessage).ConfigureAwait(false);
await client.DisconnectAsync(true).ConfigureAwait(false);
}
}
catch (Exception e)
{
Console.Write(e.Message);
}
}
/// <summary>
/// 读取上传的文件
/// </summary>
/// <returns></returns>
public static IList<MimePart> GetMimePartList(List<string> filePathss)
{
IList<MimePart> mimePartList = new List<MimePart>();
foreach (string filePath in filePathss)
{
var sw = new FileStream(filePath, FileMode.Open);
StreamReader mysr = new StreamReader(sw);
Stream stream = mysr.BaseStream;
MimePart attachment = new MimePart()
{
ContentObject = new ContentObject(stream, ContentEncoding.Default),
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Base64,
FileName = Path.GetFileName(filePath)
};
mimePartList.Add(attachment);
}
return mimePartList;
}
}
}