简体   繁体   English

使用office365发送电子邮件

[英]Send email using office365

I am trying to send email using office365 but facing this issue : 我正在尝试使用office365发送电子邮件,但面临此问题:

Additional information: The SMTP server requires a secure connection or the client was not authenticated. 附加信息:SMTP服务器需要安全连接,或者客户端未通过身份验证。 The server response was: 5.7.57 SMTP; 服务器响应为:5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [MA1PR01CA0073.INDPRD01.PROD.OUTLOOK.COM] 客户端未通过身份验证以在[MA1PR01CA0073.INDPRD01.PROD.OUTLOOK.COM]的邮件中发送匿名邮件

My code to send email is : 我发送电子邮件的代码是:

 var fromAddress = new MailAddress("email", "Relay");
        var toAddress = new MailAddress("email");
        const string fromPassword = "password";
        const string subject = "Subject";
        const string body = "Body";

        var smtp = new SmtpClient
        {
            Host = "smtp.office365.com",
            Port = 587,
            EnableSsl = true,

            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            TargetName = "STARTTLS/smtp.office365.com",
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword, "islandenergyservices.com")
        };
        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        })
        {
            smtp.Send(message);
        }

I've run into this problem before. 我以前遇到过这个问题。 It's a stupid little quirk with the order in which you set attributes on the client - you have to set the DefaultCredentials before you do anything else, and then you'll be fine. 这是一个愚蠢的小怪癖,说明了在客户端上设置属性的顺序 -您必须先设置DefaultCredentials,然后再执行其他操作,然后您就可以了。

See this answer. 看到这个答案。

I had success with the following: 我在以下方面取得了成功:

# Sending an email from PowerShell 5.1 script through outlook.office365.com
#
# 1. Create an encrypted password file
#   PS > Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File -FilePath <passwordfile>
#   This will prompt you for a password, encrypt and save in <passwordfile>
# 2. Obtain Outlook Office365 SMTP server name.
#   Go to your ISP and find the value of the MX record. For example <yourdomain>.mail.protection.outlook.com
# 3. If after running the script you get this error:
#   Send-MailMessage : Mailbox unavailable. The server response was: 5.7.606 Access denied, banned sending IP [X.X.X.X].
#   You will need to delist your IP by going here: https://sender.office.com/
#   Note:  Removing you IP from the block list could take up to 30 minutes.
#
$User = "<SMPT loging username>"
$PasswordFile = "<passwordfile>"
$SMTPCredentials=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString)
$EmailTo = "<to email address>"
$EmailFrom = "<from email address>"
$Subject = "<email subject>" 
$Body = "<email body>" 
$SMTPServer = "<Outlook STMP Server from MX record>"
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Port 25 -Credential $SMTPCredentials -UseSsl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 发送 email 与 office365 smtp - C# send email with office365 smtp 如何使用域Windows身份验证从Office365帐户发送电子邮件 - How to send an email from office365 account using domain windows authentication 如何使用Office365帐户从Azure网站发送带有附件的电子邮件 - How to send email with attachment from Azure Web Site using office365 account 不允许在Office365上代表我的客户发送电子邮件 - Not allow to send email on behalf of my client on office365 使用outlook office365 c#代码发送邮件失败 - Failed to send email using outlook office365 c# code 使用 Oauth2 通过 Office365 C# 发送 email - Using Oauth2 to send email via Office365 C# 使用设置为代表电子邮件帐户 B 发送的 Office365 电子邮件帐户 A 使用 SendGrid API 发送电子邮件 - Send email with SendGrid API using Office365 email account A which is set to send on behalf of an email account B 使用 Microsoft Graph 从 Office365 email 获取附件 - Get attachments from Office365 email using Microsoft Graph 如何在 asp.net C# 中使用启用双重身份验证的 office365 email 帐户发送 email? - How to send email using two factor authentication enabled office365 email account in asp.net C#? Office 365使用OutlookServicesClient发送带有附件的电子邮件 - Office 365 send email with attachment using OutlookServicesClient
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM