2016-01-18 134 views
0

我想通过交换服务器ie(outlook.office365.com,因为我可以从Outlook设置中看到它)使用SMTP从我的控制器(MVC4)发送电子邮件,但无法成功。我尝试了许多以前的帖子的解决方案,但无法得到任何线索。我衷心感谢所有帮助,从你们.. 这里是我的邮件配置:使用SMTP和交换服务器发送电子邮件时出错office365.com

<system.net> 
    <mailSettings> 
     <smtp> 
     <network host="smtp.office365.com" userName="[email protected]" password="defaultPassword"/> 
     </smtp> 
    </mailSettings> 
    </system.net> 

这里是我的电子邮件类: 公共级电子邮件 { 公共电子邮件(){}

private string Sender{ get; set; } 
    private List<string> Recipients { get; set; } 
    private string Subject { get; set; } 
    private string Body { get; set; } 

    public bool SendEmail() 
    { 
     try 
     { 
      var smptClient = new SmtpClient { EnableSsl = true }; 

      MailMessage newEmail = new MailMessage(); 
      foreach (var reciepent in this.Recipients) 
       newEmail.To.Add(new MailAddress(recipient)); 

      newEmail.From = new MailAddress(this.Sender); 
      newEmail.Subject = this.Subject; 
      newEmail.Body = this.Body; 
      newEmail.IsBodyHtml = false; 

      smptClient.Send(newEmail); 

      return true; 
     } 
     catch { return false; } 
    } 
} 

在上面的代码中,如果我使用“EnableSsl = true”,我得到错误“服务器不支持安全连接。”。如果我禁用SSL,我得到以下错误:

SMTP服务器需要安全连接或客户端未通过身份验证。服务器响应是:5.7.57 SMTP;客户没有通过身份验证发送匿名邮件在邮件从

回答

1
use this piece of code: 

MailMessage msg = new MailMessage(); 
    msg.To.Add(new MailAddress("[email protected]", "SomeOne")); 
    msg.From = new MailAddress("[email protected]", "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 = 25; // give 587 if 25 is blocked 
    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(); 
    } 
+0

谢谢,但我已经试过这个。它导致像我在我的问题 –

+0

中提到的相同的错误检查您使用的端口号。可能是您的端口被阻止 –

+0

以前我使用过端口25,但是我收到错误“服务器不支持安全连接”。启用S​​SL,所以我用端口587,但它仍然无法工作 –

1

最后,我会解决与我们有的域有关的问题。 对于面临类似问题的人,以下是我如何修复它。 Follwoing被我的电子邮件设置:

<network host="smtp.office365.com" userName="[email protected]" password="defaultPassword"/> 

如果您在域,那么您的用户名必须具有域您的用户名提到即[email protected]和Bang :)

干杯! !

+0

很酷:) ...终于它为你工作 –

相关问题