2016-08-11 157 views
2

我努力尝试从.NET应用程序发送电子邮件。ASP.NET C# - 从Outlook Live SMTP服务器发送电子邮件

{"Mailbox unavailable. The server response was: 5.7.3 Requested action aborted; user not authenticated"} System.Exception {System.Net.Mail.SmtpException} 

现在,这似乎表明(从我有限的了解至少)有什么毛病我的凭据,就是我的电子邮件地址和密码。

这是问题所在。我使用Yahoo电子邮件地址登录到我的Microsoft帐户。这是我作为凭证的一部分提供的地址。如果这不正确,我可以在哪里找到合适的电子邮件地址,或者我可能会丢失什么?

try 
      { 
       SmtpClient smtpClient = new SmtpClient("smtp-mail.outlook.com", 587); 

       smtpClient.EnableSsl = true; 
       smtpClient.Credentials = new System.Net.NetworkCredential("kelly*******@yahoo.com", "myMicrosoftPassword"); 
       smtpClient.UseDefaultCredentials = true; 
       smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; 

       MailMessage mail = new MailMessage(); 

       //Setting From , To and CC 
       mail.From = new MailAddress("kelly*******@yahoo.com", "Kelly"); 
       mail.To.Add(new MailAddress("[email protected]")); 


       smtpClient.Send(mail); 



      } 
      catch (Exception ex) 
      { 
       Console.Write(ex.Message); 
      } 

谢谢!

回答

0

尝试使用此作为骨架发送邮件: 请注意证书部分。 请注意,一些供应商需要额外的配置,在您的认证账户完成使用他们的服务(如:谷歌)

using(var mail = new System.Net.Mail.MailMessage()) 
{ 
    mail.From = new System.Net.Mail.MailAddress(fromAddress); 
    mail.Subject = subject; 
    mail.Body = body;    

    foreach(var attachment in attachments) 
     mail.Attachments.Add(new Attachment(attachment)); 

    foreach(var address in toAddresses) 
     mail.To.Add(address); 

    using(var smtp = new System.Net.Mail.SmtpClient(smtpAddress, smtpPort)) 
    { 
     smtp.EnableSsl = enableSsl; 
     smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
     smtp.UseDefaultCredentials = false; 
     smtp.Credentials = new NetworkCredential(authUserName, authPassword); 

     ServicePointManager.ServerCertificateValidationCallback = 
     (sender, certificate, chain, sslPolicyErrors) => true; 

     smtp.Send(mail); 
    } 
} 
+0

不幸的是,但无论如何感谢。 – KellyMarchewa

+0

如果您使用SSL,端口也可以是465(经常使用)。当然,问题不在于代码,而在于缺少一些参数/配置。祝你好运。 – sam

0

我搜索的东西,我没有发现什么。你有没有尝试使用雅虎smt服务器?

smtp.mail.yahoo.com 

我认为这是行不通的becouse微软使用其他电子邮件地址只为打造自己的帐户至极的用户可以访问到othere服务。关于电子邮件服务,我认为微软使用其他smtp服务器。例如,如果您使用不是Microsoft的电子邮件从hotmail或Outlook发送电子邮件,但您曾经注册并创建Microsoft帐户,我认为该程序不使用Microsoft服务器,而是使用雅虎的服务器。

+0

不幸的是,这并没有奏效。我收到此错误:+ \t \t $例外\t {“SMTP服务器要求安全连接或客户端未通过身份验证服务器响应为:5.7.1需要验证”} \t System.Exception的{System.Net.Mail。 SmtpException} – KellyMarchewa

+0

嗯..我不明白。如果您知道smtp协议,请尝试使用telnet连接到使用yahoo电子邮件的Microsoft服务器,以了解它是代码问题还是问题本质上不是关于服务器结构的知识。 –

0

我在一个需要SMTP实现的项目上工作过。主要区别是使用DefaultCredentials。尝试改变它,看看它是否工作。如果您已经尝试过使用其他电子邮件帐户尝试,问题可能也在那里。 下面是我做到这一点:

public void SendEmail(string recipient, string subject, string text) 
    { 
     //The smtp and port can be adjusted, deppending on the sender account 
     SmtpClient client = new SmtpClient(_smtpHostServer); 
     client.Port = _smtpHostPort; 
     client.DeliveryMethod = SmtpDeliveryMethod.Network; 
     client.UseDefaultCredentials = false; 

     try 
     { 
      System.Net.NetworkCredential credentials = 
       new System.Net.NetworkCredential(_serveraddress, _serverpassword); 
      client.EnableSsl = true; 
      client.Credentials = credentials; 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 

     //Creates a new message 
     try { 
      var mail = new MailMessage(_serveraddress.Trim(), recipient.Trim()); 
      mail.Subject = subject; 
      mail.Body = text; 

      client.Send(mail); 
     } 
     //Failing to deliver the message or to authentication will throw an exception 
     catch (Exception ex){ 
      Console.WriteLine(ex.Message); 
      throw; 
     } 
    } 
相关问题