using MailKit.Net.Smtp;
using MimeKit;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;html
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>async
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));spa
//收件人
emailMessage.To.Add(new MailboxAddress("名稱", "*************@****.***"));
emailMessage.To.Add(new MailboxAddress("名稱", "***************@****.***"));htm
//標題
emailMessage.Subject = subject;
ip
//抄送
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);string
//附件
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;it
using (var client = new SmtpClient())
{
try
{
client.Connect("smtp.qiye.163.com", 465, true);io
//用指定郵箱發送 須要密碼帳戶
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;
}
}}