[英]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,然后再执行其他操作,然后您就可以了。
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.