【问题标题】:Failed to send email using outlook office365 c# code使用outlook office365 c#代码发送邮件失败
【发布时间】:2020-06-30 16:58:17
【问题描述】:

我已经编写了下面的代码来使用 Outlook Office365 发送电子邮件。

ExchangeService myService = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
myService.Credentials = new WebCredentials(sender_mailId,sender_password);

try
{
  string serviceUrl = <<service url>> // This URL 
  myService.Url = new Uri(serviceUrl);
  EmailMessage emailMessage = new EmailMessage(myservice);
  emailMessage.Subject = "Subject test ";
  emailMessage.Body = new MessageBody("Testing Exchange Web Service API");
  emailMessage.ToRecipients.Add(to_email_id);
  emailMessage.Send();
}
catch (SmtpException exception)
{
  string msg = "Mail cannot be sent (SmtpException):";
  msg += exception.Message;
  throw new Exception(msg);
}

应该使用什么网络服务 URL?

【问题讨论】:

  • FROM 电子邮件地址需要与凭据相同的帐户。我没有看到你设置 FROM。
  • 这能回答你的问题吗? Connection to Office 365 by EWS API
  • 这段代码运行良好。非常感谢。

标签: c#


【解决方案1】:

以下代码用于发送或保存到电子邮件草稿。

static void CheckEmail()
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
service.Credentials = new WebCredentials(senderEmailId, password);
 service.AutodiscoverUrl(senderEmailId, RedirectionUrlValidationCallback);

 EmailMessage emailMessage = new EmailMessage(service);
 emailMessage.Subject = "Test office 365 project draft ";
 emailMessage.Body = new MessageBody("Testing Exchange Web Service API");

 emailMessage.ToRecipients.Add(emailTo);

//send email
emailMessage.Send();

//save to draft
emailMessage.Save(WellKnownFolderName.Drafts);
}

   private static bool RedirectionUrlValidationCallback(string redirectionUrl)
    {
        // The default for the validation callback is to reject the URL.
        bool result = false;

        Uri redirectionUri = new Uri(redirectionUrl);

        // Validate the contents of the redirection URL. In this simple validation
        // callback, the redirection URL is considered valid if it is using HTTPS
        // to encrypt the authentication credentials. 
        if (redirectionUri.Scheme == "https")
        {
            result = true;
        }
        return result;
    }

【讨论】:

    猜你喜欢
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 2022-10-14
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    相关资源
    最近更新 更多