• 使用FluentEmail发送outlook邮件


    一,邮箱账号相关设置

    1,创建outLook邮箱。

    2,进入邮箱设置->同步电子邮件->允许设备和应用使用pop

    3,设置microsoft账号的应用程序密码->进入安全性页面->更多安全选项->双层验证功能设置

    4,双层验证功能设置好后页面会出现 应用密码 项,创建新应用密码

    二,代码

    1,NuGet添加FluentEmail引用

    2,config文件添加邮件配置

      "Email": {
        "EmailAddress": "test@outlook.com",
        "Host": "smtp.office365.com",
        "From": "test@outlook.com",
        "Name": "无锡哈哈",
        "Password": "123123",//上一步中创建的应用密码
        "Port": 25
      },

    3,ConfigureServices中读取配置

                    var cemail = Configuration.GetSection("Email").Get<CompanyEmail>();
    
                    services
                        .AddFluentEmail(cemail.EmailAddress, cemail.Name)
                        .AddSmtpSender(new SmtpClient()
                        {
                            Host = cemail.Host,
                            EnableSsl = true,
                            UseDefaultCredentials = false,
                            Port=cemail.Port,
                            Credentials = new NetworkCredential(cemail.EmailAddress, cemail.Password)
                        });

    4,发送邮件

            public ActionResult Sendwlemail([FromServices]IFluentEmail fluentEmail)
            {
                try
                {
    
                   string toMail="aa@qq.com";
                   SendResponse resp= fluentEmail.To(toMail)
                            .Subject("邮件标题")
                             .Body("邮件内容")
                            .Send();
                    if (resp.Successful)
                    {
                     
                        return Success();
                    }
                  
                    var error = resp.ErrorMessages.ToJson();
                    LogNHelper.Warn(error);
                    return Error("Send Failure!");
                }
                catch (Exception e)
                {
                    LogNHelper.Exception(e);
                    return Error("Send Failure!");
                }
    
    
            }

    5,Over

  • 相关阅读:
    微信开发:MySQL utf8mb4 字符集
    Spring 事务
    Exception
    mysql系列之多实例介绍
    python连接MySQL
    1_archlinux_安装篇
    apache中如何调用CGI脚本
    1.1_Django简介及安装
    git分支合并脚本
    用python收集系统信息
  • 原文地址:https://www.cnblogs.com/yushuo/p/11187558.html
Copyright © 2020-2023  润新知