首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Microsoft365家庭订阅从.NET Core发送电子邮件

使用Microsoft365家庭订阅从.NET Core发送电子邮件
EN

Stack Overflow用户
提问于 2022-02-08 06:01:59
回答 2查看 1.8K关注 0票数 2

我有一个Office 365家庭订阅(因此使用outlook.com),并试图从我正在工作的C#应用程序发送电子邮件。有人知道这是否可能吗?从我的研究来看,似乎有很多人对这种方法有异议,但我正在努力找出这种方法是否得到支持

EN

回答 2

Stack Overflow用户

发布于 2022-02-08 09:38:33

完成所需工作的方式有多种:

  1. 使用标准的.net机制,同样的问题被发布到这里
代码语言:javascript
复制
            using (SmtpClient client = new SmtpClient()
            {
                Host = "smtp.office365.com",
                Port = 587,
                UseDefaultCredentials = false, // This require to be before setting Credentials property
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential("alias@fulldomain.com", "password"), // you must give a full email address for authentication 
                TargetName = "STARTTLS/smtp.office365.com", // Set to avoid MustIssueStartTlsFirst exception
                EnableSsl = true // Set to avoid secure connection exception
            })
            {

                MailMessage message = new MailMessage()
                {
                    From = new MailAddress("alias@fulldomain.com"), // sender must be a full email address
                    Subject = subject,
                    IsBodyHtml = true,
                    Body = "<h1>Hello World</h1>",
                    BodyEncoding = System.Text.Encoding.UTF8,
                    SubjectEncoding = System.Text.Encoding.UTF8,

                };
                var toAddresses = recipients.Split(',');
                foreach (var to in toAddresses)
                {
                    message.To.Add(to.Trim());
                }

                try
                {
                    client.Send(message);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

SmtpClient在.NET内核中是可用的,但不推荐使用它。相反,请考虑使用https://github.com/jstedfast/MailKit

  1. 使用图API
票数 0
EN

Stack Overflow用户

发布于 2022-02-09 05:10:05

对于那些在使用smtp.office 365.com和通过代码发送电子邮件方面有问题的人来说。您需要在您的Microsoft帐户中添加一个“应用密码”。登录到outlook.com时使用的常规密码将无法工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71029199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档