【问题标题】:Send email using office365使用 office365 发送电子邮件
【发布时间】:2017-10-12 10:13:47
【问题描述】:

我正在尝试使用 office365 发送电子邮件,但遇到了这个问题:

附加信息:SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.7.57 SMTP;在 MAIL FROM [MA1PR01CA0073.INDPRD01.PROD.OUTLOOK.COM] 期间,客户端未通过身份验证发送匿名邮件

我发送电子邮件的代码是:

 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);
        }

【问题讨论】:

    标签: c# asp.net smtp sendmail smtpclient


    【解决方案1】:

    我以前遇到过这个问题。这是您在客户端上设置属性的 order 的一个愚蠢的小怪癖 - 您必须在执行任何其他操作之前设置 DefaultCredentials,然后就可以了.

    请参阅this 答案。

    【讨论】:

    • 正确,我遇到了同样的问题,这解决了。
    • 我没有发布问题 ;-)
    【解决方案2】:

    我在以下方面取得了成功:

    # 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
    

    【讨论】:

      猜你喜欢
      • 2020-02-06
      • 2020-04-26
      • 2021-02-24
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 2021-09-28
      • 1970-01-01
      相关资源
      最近更新 更多