首页 > 其他分享 >使用Azure云应用发送Office365Email

使用Azure云应用发送Office365Email

时间:2023-02-03 17:25:23浏览次数:52  
标签:string 微软 发送 FromEmail 发件人 Azure new public Office365Email

Office 365 旧版发送邮件是直接使用明文账号密码 SMTP 发送邮件,近期微软为了安全考虑,将限制这种方式,改用OAuth2.0

.net core 代码

  1 public class Office365Email
  2     {
  3         /// <summary>
  4         /// 发件人邮箱
  5         /// </summary>
  6         private string _FromEmail;
  7 
  8         /// <summary>
  9         /// 发件人姓名
 10         /// </summary>
 11         private string _FromName;
 12 
 13         /// <summary>
 14         /// 微软云应用租户ID
 15         /// </summary>
 16         private string _TenantId;
 17 
 18         /// <summary>
 19         /// 微软云应用客户端ID
 20         /// </summary>
 21         private string _ClientId;
 22 
 23         /// <summary>
 24         /// 微软云应用秘钥
 25         /// </summary>
 26         private string _ClientSecret;
 27 
 28 
 29         public string FromEmail
 30         {
 31             set
 32             {
 33                 _FromEmail = value;
 34             }
 35         }
 36 
 37         public string FromName
 38         {
 39             set
 40             {
 41                 _FromName = value;
 42             }
 43         }
 44 
 45         public string TenantId
 46         {
 47             set
 48             {
 49                 _TenantId = value;
 50             }
 51         }
 52 
 53         public string ClientId
 54         {
 55             set
 56             {
 57                 _ClientId = value;
 58             }
 59         }
 60 
 61         public string ClientSecret
 62         {
 63             set
 64             {
 65                 _ClientSecret = value;
 66             }
 67         }
 68 
 69         public Office365Email()
 70         {
 71         }
 72 
 73         /// <summary>
 74         /// 初始化
 75         /// </summary>
 76         /// <param name="tenantId">微软云应用租户ID</param>
 77         /// <param name="clientId">微软云应用客户端ID</param>
 78         /// <param name="clientSecret">微软云应用秘钥</param>
 79         /// <param name="fromMail">发件人邮箱</param>
 80         /// <param name="fromName">发件人姓名</param>
 81         public Office365Email(string tenantId, string clientId, string clientSecret, string fromMail, string fromName)
 82         {
 83             _TenantId = tenantId;
 84             _ClientId = clientId;
 85             _ClientSecret = clientSecret;
 86             _FromEmail = fromMail;
 87             _FromName = fromName;
 88         }
 89 
 90         public bool Send(string toEmail, string subject, string body, bool bodyIsHtml = false)
 91         {
 92             Message message = new Message()
 93             {
 94                 Subject = subject,
 95 
 96                 Body = new ItemBody
 97                 {
 98                     ContentType = bodyIsHtml ? BodyType.Html : BodyType.Text,
 99                     Content = body
100                 },
101                 //发件人
102                 From = new Recipient()
103                 {
104                     EmailAddress = new EmailAddress
105                     {
106                         Address = _FromEmail,
107                         Name = _FromName
108                     }
109 
110                 },
111                 ToRecipients = new List<Recipient>()
112             {
113                 new Recipient
114                 {
115                     EmailAddress = new EmailAddress
116                     {
117                         Address = toEmail
118                     }
119                 }
120             }
121             };
122             GraphResponse _res = Send(_FromEmail, message, _TenantId, _ClientId, _ClientSecret);
123             return true;
124         }
125 
126         /// <summary>
127         /// 发送邮件
128         /// </summary>
129         /// <param name="fromEmail">发件人邮箱</param>
130         /// <param name="message">邮件内容</param>
131         /// <param name="tenantId">微软云应用租户ID</param>
132         /// <param name="clientId">微软云应用客户端ID</param>
133         /// <param name="clientSecret">微软云应用秘钥</param>
134         /// <returns></returns>
135         public GraphResponse Send(string fromEmail, Message message, string tenantId, string clientId, string clientSecret)
136         {
137             ClientSecretCredential credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
138             GraphServiceClient graphClient = new GraphServiceClient(credential);
139             bool saveToSentItems = true;  //是否保存到已发送的邮件列表
140             return graphClient.Users[fromEmail].SendMail(message, saveToSentItems).Request().PostResponseAsync().Result;
141         }
142     }

 

同样的代码  换成 .net framework 框架就不行了,后面就没再研究了

标签:string,微软,发送,FromEmail,发件人,Azure,new,public,Office365Email
From: https://www.cnblogs.com/wws96/p/17089923.html

相关文章