C# 163邮箱发送 附件以及邮件信息
2021-06-08 20:05
标签:amp mst alt mail 用户 get lte cell lse demo 用来测试,以及发送。 注意:163后端需要在 开启客户端授权密码 C# 163邮箱发送 附件以及邮件信息 标签:amp mst alt mail 用户 get lte cell lse 原文地址:https://www.cnblogs.com/ichengq/p/10679232.html
public static bool SendEmail(EmailInfo mailInfo)
{
try
{
MailMessage mailMsg = new MailMessage();
mailMsg.To.Add(new MailAddress(mailInfo.ToAddress));
mailMsg.From = new MailAddress(mailInfo.FromAddress, mailInfo.DisplayName);
mailMsg.Subject = mailInfo.Subject;
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(mailInfo.Body, Encoding.UTF8, MediaTypeNames.Text.Html));
SmtpClient smtpClient = new SmtpClient(mailInfo.SmtpHost, mailInfo.SmtpPort);
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(mailInfo.SmtpUserName, mailInfo.SmtpPassword);
smtpClient.Credentials = credentials;
if (!string.IsNullOrEmpty(mailInfo?.filename) && mailInfo?.stream != null)
{
mailMsg.Attachments.Add(new Attachment(mailInfo.stream, mailInfo.filename));
}
smtpClient.Send(mailMsg);
}
catch (Exception ex)
{
return false;
}
return true;
} public class EmailInfo
{
///
附件依赖于 NPOI
public static MemoryStream ExportCompanyOrder()
{
List var mailInfo = new EmailInfo();
mailInfo.SmtpHost = "smtp.163.com";
//mailInfo.SmtpPort = 25;
mailInfo.SmtpUserName = "ichengq@163.com";
mailInfo.SmtpPassword = "cheng1993";
mailInfo.DisplayName = "智领";
mailInfo.FromAddress = "ichengq@163.com";
mailInfo.ToAddress = "41753655@qq.com";
mailInfo.Subject = "测试";
mailInfo.Body = "测试的啊 ";
mailInfo.filename = "111.xls";
mailInfo.stream = ExportCompanyOrder();
var aa = SendEmail(mailInfo);