• C#发送QQ邮箱


    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using SendEmailDemo.Models;
    using System.Net;
    using System.Net.Mail;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace SendEmailDemo.Controllers
    {
        [Route("[controller]/[action]")]
        [ApiController]
        public class HomeController : ControllerBase
        {
            private readonly IConfiguration _configuration;
            public HomeController(IConfiguration configuration)
            {
                _configuration = configuration;
            }
            public async Task<IActionResult> SendQQEmail()
            {
                //读取配置文件
                EmailConfig emailConfig = _configuration.GetSection("QQEmailConfig").Get<EmailConfig>();
    
                //实例化对象
                using (MailMessage mail = new MailMessage())
                {
                    //发件人
                    mail.From = new MailAddress(emailConfig.From.email, emailConfig.From.displayname);
                    //收件人
                    foreach (var item in emailConfig.Addressee)
                    {
                        mail.To.Add(new MailAddress(item.email, item.displayname));
                    }
                    //标题
                    mail.Subject = emailConfig.Subject;
                    //内容
                    mail.Body = "<b>财报显示</b>,保利地产2019年上半年净利润同比增长53.28%,2018年上半年净利润同比增长14.94%,2017年净利润同比增长13.92%。由此可见,保利地产今年上半年净利润增速创下三年来新低。";
                    //内容是否为 html 渲染
                    mail.IsBodyHtml = true;
                    //标题编码
                    mail.SubjectEncoding = Encoding.UTF8;
                    //内容编码
                    mail.BodyEncoding = Encoding.UTF8;
                    //附件
                    mail.Attachments.Add(new Attachment(@"D:Music林俊杰 - 曹操.m4a"));
                    //smtp客户端
                    using (SmtpClient client = new SmtpClient())
                    {
                        //主机
                        client.Host = emailConfig.Host;
                        //端口
                        client.Port = emailConfig.Port;
                        //是否为加密
                        client.EnableSsl = emailConfig.EnableSsl;
                        //凭证(发件人email、 授权码)
                        client.Credentials = new NetworkCredential(emailConfig.From.email, emailConfig.AuthorizationCode);
                        //发送邮件
                        await client.SendMailAsync(mail);
                    }
                    return new JsonResult(new
                    {
                        code = 200,
                        message = "邮件已发送请注意查收",
                        data = emailConfig.Addressee
                    });
    
                }
    
            }
    
    
    
        }
    }
    //配置文件实体类
    public
    class EmailConfig { public class Config { public string email { get; set; } public string displayname { get; set; } } public Config From { get; set; } public Config[] Addressee { get; set; } public string AuthorizationCode { get; set; } public string Subject { get; set; } public string Host { get; set; } public int Port { get; set; } public bool EnableSsl { get; set; } }
    //配置文件
    {
    "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "QQEmailConfig": { "From": { //发件人 "email": "123456789@qq.com", //邮件 "displayname": "tomorrow得到系统通知" //显示名称 }, "AuthorizationCode": "************", //授权码 "Addressee": [ //收件人 { "email": "147258369@qq.com", "displayname": "张三" }, { "email": "963852741@qq.com", "displayname": "李四" } ], "Subject": "tomorrow的系统通知", //统一标题 "Host": "smtp.qq.com", //主机 "Port": 587, //端口 "EnableSsl": true //是否加密 }, "AllowedHosts": "*" }

    获取授权码

     

     发送短信,后点击 “我已发送”

    附件预览

     

  • 相关阅读:
    【视频教学】Maclean教你用Vbox在Linux 6.3上安装Oracle 11gR2 RAC
    了解你所不知道的SMON功能(十二):Shrink UNDO(rollback) SEGMENT
    大叔问题定位分享(41)logstash消费kafka延迟
    大叔经验分享(122)linux系统蓝牙失效
    大叔问题定位分享(42)yarn被利用提交getshell任务挖矿
    大叔问题定位分享(43)hbase大面积重启
    arcgis 栅格转Point点
    arcgis建立渔网(fishnet)
    Docker容器:将带UI的程序直接转为Web应用,so easy
    架构漫谈(五):什么是软件
  • 原文地址:https://www.cnblogs.com/licm/p/13357959.html
Copyright © 2020-2023  润新知