?
/// <summary>
?/// 發(fā)送郵件的方法
?/// </summary>
?public OperateResult<int> SendMail(MailModel mails)
?{
? ? ?var resultData = new OperateResult<int>();
? ? ?if (mails.to == null || !mails.to.Any())
? ? ?{
? ? ? ? ?resultData.Status = OperateStatus.Failure;
? ? ? ? ?resultData.Description = "收件人地址不能為空";
? ? ? ? ?return resultData;
? ? ?}
? ? ?var mailConfig = App.GetConfig<MailConfigOptions>("applicationsettings:mailConfig");
? ? ?if (mailConfig == null)
? ? ?{
? ? ? ? ?resultData.Status = OperateStatus.Failure;
? ? ? ? ?resultData.Description = "郵件配置信息無效";
? ? ? ? ?return resultData;
? ? ?}
? ? ?//將發(fā)件人郵箱帶入MailAddress中初始化
? ? ?MailAddress mailAddress = new MailAddress("郵件地址");
? ? ?//創(chuàng)建Email的Message對象
? ? ?MailMessage mailMessage = new MailMessage();
? ? ?//判斷收件人數(shù)組中是否有數(shù)據(jù)
? ? ?if (mails.to != null && mails.to.Any())
? ? ?{
? ? ? ? ?//循環(huán)添加收件人地址
? ? ? ? ?foreach (var item in mails.to)
? ? ? ? ?{
? ? ? ? ? ? ?if (!string.IsNullOrEmpty(item) && item.IsEmail())
? ? ? ? ? ? ? ? ?mailMessage.To.Add(item.ToString());
? ? ? ? ?}
? ? ?}
? ? ?if (mailMessage.To.Count <= 0)
? ? ?{
? ? ? ? ?resultData.Status = OperateStatus.Failure;
? ? ? ? ?resultData.Description = "收件人地址無效";
? ? ? ? ?return resultData;
? ? ?}
? ? ?//判斷抄送地址數(shù)組是否有數(shù)據(jù)
? ? ?if (mails.cc != null && mails.cc.Any())
? ? ?{
? ? ? ? ?//循環(huán)添加抄送地址
? ? ? ? ?foreach (var item in mails.cc)
? ? ? ? ?{
? ? ? ? ? ? ?if (!string.IsNullOrEmpty(item) && item.IsEmail())
? ? ? ? ? ? ? ? ?mailMessage.CC.Add(item.ToString());
? ? ? ? ?}
? ? ?}
? ? ?//發(fā)件人郵箱
? ? ?mailMessage.From = mailAddress;
? ? ?//標題
? ? ?mailMessage.Subject = mails.title;
? ? ?//編碼
? ? ?mailMessage.SubjectEncoding = Encoding.UTF8;
? ? ?//正文
? ? ?mailMessage.Body = mails.body;
? ? ?//正文編碼
? ? ?mailMessage.BodyEncoding = Encoding.Default;
? ? ?//郵件優(yōu)先級
? ? ?mailMessage.Priority = MailPriority.High;
? ? ?//正文是否是html格式
? ? ?mailMessage.IsBodyHtml = mails.isHtml;
? ? ?#region 添加附件
? ? ?if (mails.files != null && mails.files.Any())
? ? ?{
? ? ? ? ?//取得Web根目錄和內(nèi)容根目錄的物理路徑
? ? ? ? ?string webRootPath = App.WebHostEnvironment.WebRootPath;
? ? ? ? ?foreach (var fileItem in mails.files)
? ? ? ? ?{
? ? ? ? ? ? ?路徑拼接
? ? ? ? ? ? ?//webRootPath = _hostingEnvironment.WebRootPath + "\\" + "upload-file" + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + Path.GetFileNameWithoutExtension(item.FileName) + Path.GetExtension(item.FileName).ToLower();
? ? ? ? ? ? ?創(chuàng)建文件流
? ? ? ? ? ? ?//using (var FileStream = new FileStream(webRootPath, FileMode.Create))
? ? ? ? ? ? ?//{
? ? ? ? ? ? ?拷貝文件流
? ? ? ? ? ? ?//await item.CopyToAsync(FileStream);
? ? ? ? ? ? ?釋放緩存
? ? ? ? ? ? ?//FileStream.Flush();
? ? ? ? ? ? ?//}
? ? ? ? ? ? ?//再根據(jù)路徑打開文件,得到文件流
? ? ? ? ? ? ?//FileStream fileStream = File.OpenWrite(filePath);
? ? ? ? ? ? ?//using (fileStream)
? ? ? ? ? ? ?//{
? ? ? ? ? ? ?// ? ?Attachment attachment = new Attachment(fileStream, filePath);
? ? ? ? ? ? ?// ? ?mailMessage.Attachments.Add(attachment);
? ? ? ? ? ? ?//}
? ? ? ? ? ? ?try
? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ?string pathUrl = $"{webRootPath}/{fileItem.FilePath}/{fileItem.FileObjectName}";
? ? ? ? ? ? ? ? ?Attachment attachment = new Attachment(new FileStream(pathUrl, FileMode.Open), fileItem.FileOriginName, "text/plain");
? ? ? ? ? ? ? ? ?mailMessage.Attachments.Add(attachment);
? ? ? ? ? ? ?}
? ? ? ? ? ? ?catch (Exception ex)
? ? ? ? ? ? ?{
? ? ? ? ? ? ?}
? ? ? ? ? ? ?//using (Stream stream = new FileStream(filePath, FileMode.Open))
? ? ? ? ? ? ?//{
? ? ? ? ? ? ?// ? ?mailMessage.Attachments.Add(new Attachment(stream, Path.GetFileName(filePath), "text/plain"));
? ? ? ? ? ? ?//} ? ? ? ? ? ? ? ??
? ? ? ? ? ? ?//添加至附件中
? ? ? ? ? ? ?//mailMessage.Attachments.Add(new Attachment(stream, item.FileName)); ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ?//mailMessage.Attachments.Add(new Attachment(item.OpenReadStream(),item.FileName));
? ? ? ? ?}
? ? ?}
? ? ?#endregion
? ? ?try
? ? ?{
? ? ? ? ?//實例化一個Smtp客戶端
? ? ? ? ?SmtpClient smtp = new SmtpClient(ip地址, 端口號);
? ? ? ? ?//將發(fā)件人的郵件地址和客戶端授權(quán)碼帶入以驗證發(fā)件人身份
? ? ? ? ?//smtp.Credentials = new System.Net.NetworkCredential(郵件地址, SMTP服務(wù)授權(quán)碼);
? ? ? ? ?//指定SMTP郵件服務(wù)器
? ? ? ? ?//smtp.Host = GetMailHost(郵件地址);
? ? ? ? ?smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
? ? ? ? ?smtp.Credentials = new System.Net.NetworkCredential(郵件地址, SMTP服務(wù)授權(quán)碼); //郵箱賬號 ?密碼
? ? ? ? ?smtp.Timeout = 6000; ?//6秒超時
? ? ? ? ?//郵件發(fā)送到SMTP服務(wù)器
? ? ? ? ?smtp.Send(mailMessage);文章來源:http://www.zghlxwxcb.cn/news/detail-803187.html
? ? ? ? ?resultData.Status = OperateStatus.Success;
? ? ? ? ?resultData.Data = 1;
? ? ?}
? ? ?catch (Exception ex)
? ? ?{
? ? ? ? ?resultData.Status = OperateStatus.Failure;
? ? ? ? ?resultData.Description = "郵件發(fā)送異常!" + ex.Message;
? ? ?}
? ? ?return resultData;
?}文章來源地址http://www.zghlxwxcb.cn/news/detail-803187.html
到了這里,關(guān)于C# SMTP 郵件發(fā)送傻瓜操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!