簡體   English   中英

無法使用C#使用Office 365憑據發送電子郵件

[英]cannot send email using office 365 credentials using C#

我正在嘗試使用C#發送帶有Office 365憑據的電子郵件。 但是它無法發送並獲得異常。

如何解決這個問題? 是否可以從Office 365發送?

     foreach (var filename in files)
     {
           String userName = "sri@MyOffice365domain.com";
           String password = "password";
           MailMessage mail = new MailMessage();
           SmtpClient SmtpServer = new SmtpClient("smtp.office365.com");
           mail.From = new MailAddress("sri@MyOffice365domain.com");
           mail.To.Add("senderaddress@senderdomain.com");
           mail.Subject = "Fwd: for " + filename;
           mail.Body = "mail with attachment";

           System.Net.Mail.Attachment attachment;
           attachment = new System.Net.Mail.Attachment(filename);
           mail.Attachments.Add(attachment);

           SmtpServer.Port = 587;
           SmtpServer.Credentials = new System.Net.NetworkCredential(userName, password);
           SmtpServer.EnableSsl = true;

           SmtpServer.Send(mail);
}

交易失敗。 服務器響應為:5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; 由於消息“無法提交消息”的永久性異常,無法處理消息。

您要同時發送多少封電子郵件?

foreach(文件中的var文件名)

通過https://outlook.office.com/apihttps://outlook.office365.com/api訪問的所有Outlook API的限制是每個應用程序ID每用戶(或組)每分鍾60個請求。 因此,目前,開發人員只能從應用程序中進行有限的API調用。 通讀Microsoft博客文章,以獲取有關REST API調用限制的更多詳細信息。

嘗試改用以下代碼:

MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("someone@somedomain.com", "SomeOne"));
msg.From = new MailAddress("you@yourdomain.com", "You");
msg.Subject = "This is a Test Mail";
msg.Body = "This is a test message using Exchange OnLine";
msg.IsBodyHtml = true;

SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("your user name", "your password");
client.Port = 587; // You can use Port 25 if 587 is blocked (mine is!)
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
try
{
    client.Send(msg);
    lblText.Text = "Message Sent Succesfully";
}
catch (Exception ex)
{
    lblText.Text = ex.ToString();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM