C# 使用smtp.qq.com邮箱服务发送邮件

1、开启SMTP服务,相当于授权过程(开启后,才可以通过这个模拟发送)

位置: 设置--> 账号--> 账号安全--> POP3/SMTP服务 -->开启

 

2、编写发送邮箱邮件API(引用:System.Net,System.Net.Mail)

[Route("api/[controller]")]
[ApiController]
public class MailController : ControllerBase
{
    /// <summary>
    /// 发送邮件
    /// </summary>
    [HttpPost("send")]
    public IActionResult PostEmail([FromBody] Mail mailDto)
    {
        if (!ModelState.IsValid)
        {
            // 返回验证错误信息
            BadRequest(ModelState);
        }
        SMTPSendHelper.SendEmailBySMTP(mailDto);
        return new JsonResult(new
        {
            message = "发送成功",
            code = 0,
            success = true
        });
    }
}
/// <summary>
/// 邮件发送帮助类
/// </summary>
public class SMTPSendHelper
{
    /// <summary>
    /// 发送邮件
    /// </summary>
    public static void SendEmailBySMTP(Mail mail)
    {
        //实例化一个发送邮件类
        MailMessage mailMsg = new MailMessage();
        //发件人邮箱地址
        mailMsg.From = new MailAddress(mail.senderEmail);
        //收件人邮箱地址
        mailMsg.To.Add(new MailAddress(mail.receiverEmail));
        //邮件标题的编码格式
        mailMsg.SubjectEncoding = Encoding.UTF8;
        //邮件标题
        mailMsg.Subject = mail.emailTitle;
        //邮件内容的编码格式
        mailMsg.BodyEncoding = Encoding.UTF8;
        //邮件内容
        mailMsg.Body = mail.emailContent;
        //是否是html邮件
        mailMsg.IsBodyHtml = true;
        //邮件优先级
        mailMsg.Priority = MailPriority.High;
        //实例化一个SmtpClient类
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
        //设置邮件服务名,这里使用的是QQ邮箱,所以是smtp.qq.com, 若使用163邮箱,则是 smtp.163.com
        client.Host = mail.smtpService;
        //设置邮件端口
        client.Port = 587;
        //使用安全加密连接
        client.EnableSsl = true;
        //不和请求一块发送
        client.UseDefaultCredentials = false;
        //验证发件人身份(发件人邮箱,邮箱生成的授权码)
        client.Credentials = new NetworkCredential(mail.senderEmail, mail.authCode);
        //发送
        client.Send(mailMsg);
    }
}
/// <summary>
/// 邮件参数类 
/// </summary>
public class Mail
{
    /// <summary>
    /// 发件人邮箱
    /// </summary>
    public string senderEmail { set; get; } = "471812366@qq.com";
    /// <summary>
    /// 发件人邮箱授权码
    /// </summary>
    public string authCode { set; get; } = "igghkcotbwfycadh";
    /// <summary>
    /// 收件人邮箱
    /// </summary>
    [Required(ErrorMessage = "收件人邮箱是必填项")]
    public string? receiverEmail { set; get; }
    /// <summary>
    /// 邮件标题
    /// </summary>
    [Required(ErrorMessage = "邮件标题是必填项")]
    public string? emailTitle { set; get; }
    /// <summary>
    /// 邮件内容
    /// </summary>
    [Required(ErrorMessage = "邮件内容是必填项")]
    public string? emailContent { set; get; }
    /// <summary>
    /// 邮件服务名:smtp.qq.com
    /// </summary>
    public string smtpService { set; get; } = "smtp.qq.com";
}

 

 3、调用发送邮箱邮件代码

//测试发送邮件
var mail = new Mail()
{
    receiverEmail = "504968510@qq.com",
    emailTitle = "测试邮件",
    emailContent = "测试邮件内容:htttp://www.baidu.com"
};

 或者JSON 调用API发生邮件

{
	"receiverEmail":"504968510@qq.com",
	"emailTitle":"测试标题",
	"emailContent":"测试邮件内容:<b>htttp://www.baidu.com</b>"
}

 

 4、发送后效果

 

posted @ 2024-12-21 22:10  microsoft-zhcn  阅读(10)  评论(0编辑  收藏  举报