c# 邮件发送
2021-03-26 23:27
标签:exception ase smtps ndt send string efault == return c# 邮件发送 标签:exception ase smtps ndt send string efault == return 原文地址:https://www.cnblogs.com/zxhome/p/9368925.html
1
2
private static readonly string _smtpServer = ConfigurationManager.AppSettings["SmtpServer"];
private static readonly string _userAccount = ConfigurationManager.AppSettings["FromUser"];
private static readonly string _userPwd = ConfigurationManager.AppSettings["FromPwd"];
private static readonly string _toUser = ConfigurationManager.AppSettings["ToUser"];
private static readonly string _defaultEmailAddress = ConfigurationManager.AppSettings["SmtpDeaultEmail"];
public static Operate SendEmail(string title, string content, string toUserEmail = "", string filepath = "")
{
var result=new Operate();
try
{
//邮件接收人
var sendTo = toUserEmail == "" ? _toUser : toUserEmail;
var client = new SmtpClient(_smtpServer);
client.Timeout = 60000;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(_userAccount, _userPwd);
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
var message = new MailMessage();
if (filepath != "")
{
var attach = new Attachment(filepath);
attach.Name = Path.GetFileName(filepath);
attach.NameEncoding = Encoding.GetEncoding("gb2312");
attach.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
attach.ContentDisposition.Inline = true;
attach.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
message.Attachments.Add(attach);
}
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.From = new System.Net.Mail.MailAddress(_defaultEmailAddress);
message.To.Add(new System.Net.Mail.MailAddress(sendTo));
message.IsBodyHtml = true;
message.Subject = title.Replace("\r", "").Replace("\n", "").Trim();
message.Body = content;
client.SendMailAsync(message);
result.Status = 1;
}
catch (Exception ex)
{
result.Status = -1;
result.Message = ex.Message;
Logger.WriteErrorLog(ex);
}
return result;
}