.Net core-邮件发送(同步,异步)底层代码(欢迎留言讨论)
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;
}
}
}