[DEV] 在 .Net 中使用第三方邮件服务(SMTP)发送邮件

服务地址

  服务地址 端口
163 邮箱 smtp.163.com 25
QQ 邮箱 smtp.qq.com 587
Outlook 邮箱 smtp.office365.com 587


范例:163 邮箱

1) 开启 SMTP 服务

 

2) 申请 "授权密码"

 

邮件发送功能

using System;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace Demo_Send_163_Mail
{
    class Program
    {
        static void Main(string[] args)
        {
            var _mailBody = System.IO.File.ReadAllText("邮件内容");
            var msg = new MailMessage
            {
                IsBodyHtml = true,
                BodyEncoding = Encoding.UTF8,
                From = new MailAddress("发件人@163.com"),
                Subject = "邮件标题",
                Body = _mailBody
            };
            msg.To.Add(new MailAddress("收件人@163.com"));
            var client = new SmtpClient
            {
                Host = "smtp.163.com",
                Port = 25,
                EnableSsl = true,
                UseDefaultCredentials = false, /* 我的测试场景,这里需要设置为 False */
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential("发件人@163.com", "2)申请的授权密码")
            };
            client.Send(msg);
            Console.WriteLine("邮件发送完了...");
            Console.ReadLine();
        }
    }
}

 

参考资料
https://blog.csdn.net/hynkoala/article/details/112678464

posted @ 2023-06-29 19:42  jinzesudawei  阅读(138)  评论(0编辑  收藏  举报