【问题标题】:ASP.NET C# - Sending Email from Outlook Live SMTP ServerASP.NET C# - 从 Outlook Live SMTP 服务器发送电子邮件
【发布时间】:2016-12-18 06:17:27
【问题描述】:

我正在努力尝试从 .NET 应用程序发送电子邮件。

{"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("recipeint@email.com"));


                smtpClient.Send(mail);



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

谢谢!

【问题讨论】:

    标签: c# asp.net email outlook


    【解决方案1】:

    尝试使用它作为骨架来发送邮件: 请注意证书部分。 请注意,某些提供商需要对您的身份验证帐户进行额外配置才能使用他们的服务(例如:Google)

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

    【讨论】:

    • 如果您使用 SSL,端口也可以是 465(经常使用)。可以肯定的是,问题不在于代码,而在于您缺少一些参数/配置。祝你好运。
    【解决方案2】:

    我搜索了一些东西,但什么也没找到。您是否尝试使用 yahoo smt 服务器?

    smtp.mail.yahoo.com
    

    我认为它不起作用,因为 Microsoft 仅使用其他电子邮件地址来创建用户可以访问其他服务的自己的帐户。关于电子邮件服务,我认为 Microsoft 使用其他 smtp 服务器。例如,如果您使用不是 Microsoft 的电子邮件从 hotmail 或 Outlook 发送电子邮件,但您曾经注册并创建了 Microsoft 帐户,我认为该程序不使用 Microsoft 服务器,而是使用雅虎服务器。

    【讨论】:

    • 不幸的是,这不起作用。我收到此错误:+ $exception {“SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.7.1 需要身份验证”} System.Exception {System.Net.Mail.SmtpException}跨度>
    • 嗯..我无法理解。如果您知道 smtp 协议,请尝试使用 telnet 使用 yahoo 的电子邮件连接到 Microsoft 服务器,以了解这是代码问题还是本质上不了解服务器结构的问题。
    【解决方案3】:

    我参与了一个需要实施 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;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2017-05-18
      • 2016-02-09
      • 2013-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 2015-11-21
      相关资源
      最近更新 更多