受欢迎的博客标签

阿里云企业云邮箱参数配置及邮件发送代码

using System;
using System.Collections.Generic;
using System.Text;

using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;

namespace Common.Messages
{
    public class EmailSender 
    {

        /// <summary>
        /// 调试成功
        /// </summary>
        /// <param name="toEmail"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public async Task SendEmailAsync(string toEmail, string subject, string body)
        {
            string result;

            using (var smtpClient = new SmtpClient())
            {

                //发送方式
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                //smtp服务器
                smtpClient.Host = "smtp.mxhichina.com";
                //设置发送时间间隔
                // smtpClient.Timeout = m_timeout;

                smtpClient.UseDefaultCredentials = true;
                //用户名凭证               
                  smtpClient.Credentials = new System.Net.NetworkCredential("msdly@iaspnetcore.com", "12345");
               
                //邮件信息
                MailMessage message = new MailMessage();
                //发件人
                message.From = new MailAddress("msdly@iaspnetcore.com");
                //收件人


                //foreach (object item in MailTo)
                //{
                //    message.To.Add(item.ToString());
                //}
                message.To.Add(toEmail);
                message.To.Add("368@qq.com");
                //主题
                message.Subject = subject;
                //内容
                message.Body = body;
                //正文编码
                message.BodyEncoding = System.Text.Encoding.Default;
                //设置为HTML格式
                message.IsBodyHtml = true;
                //优先级
                message.Priority = MailPriority.High;

                //smtpClient.Send(message);//同步发送方式
                await smtpClient.SendMailAsync(message);//异步发送方式



            }



        }




    }
}

 

发送代码

  Common.Messages.EmailSender d = new Common.Messages.EmailSender();

          await  d.SendEmailAsync("2*4@qq.com", "dd", "dd");

 

腾讯企业邮箱

在appsettings.json配置收发邮件的账号等信息。

//appsettings.json
  "Email": {
    "Host": "smtp.exmail.qq.com",
    "Port": 465,
    "UseSsl": true,
    "From": {
      "Username": "123@meowv.com",
      "Password": "[Password]",
      "Name": "MEOWV.COM",
      "Address": "123@meowv.com"
    },
    "To": [
      {
        "Name": "test1",
        "Address": "test1@meowv.com"
      },
      {
        "Name": "test2",
        "Address": "test2@meowv.com"
      }
    ]
  }

 

企业云邮箱POP\SMTP\IMAP地址和端口信息