【问题标题】:SMTP exception when sending email with Yahoo使用 Yahoo 发送电子邮件时出现 SMTP 异常
【发布时间】:2019-01-08 10:13:33
【问题描述】:

我有以下代码用于从我的应用程序发送电子邮件:

var config = DeserializeUserConfig(perfilAcesso.GetClientConfigPath() + "Encrypted");

using (SmtpClient client = new SmtpClient())
{
    client.Host = config.GetClientSMTP();
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential(config.GetClientEmail(), config.GetClientPassword());

    using (MailMessage mail = new MailMessage())
    {
        mail.Sender = new MailAddress(config.GetClientEmail(), config.GetClientName());
        mail.From = new MailAddress(config.GetClientEmail(), config.GetClientCompany());
        mail.To.Add(new MailAddress("emailToReceive"));
        mail.Subject = "[PME] SOS - Equipamento Parado";
        mail.Body = "";

        client.Send(mail);
        MessageBox.Show("Email enviado com sucesso!");
   }
}

我设置了三个可能的 SMTP 主机供用户选择:Gmail ("smtp.gmail.com")、Outlook ("smtp.live.com") 和 Yahoo ("smtp.mail.yahoo.com")。

当我尝试使用 Yahoo 帐户发送电子邮件时,会引发此异常:

System.Net.Mail.SmtpException:邮箱不可用。服务器响应为:未采取请求的邮件操作:邮箱不可用。

我知道在使用 Gmail 和 Outlook 帐户发送电子邮件时,该方法非常有效,因为我尝试了多次。

我做错了什么?任何帮助将不胜感激!

【问题讨论】:

    标签: c# email exception smtp yahoo-mail


    【解决方案1】:

    第一步

    client.Port = 587;
    

    第 2 步

    转到https://login.yahoo.com/account/security

    第三步

    启用允许使用不太安全的登录方式的应用

    第 4 步:完整代码

    using System;
    using System.Net.Mail;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                using (SmtpClient client = new SmtpClient())
                {
                    client.Host = config.GetClientSMTP();
                    client.EnableSsl = true;
                    client.Port = 587;
                    client.UseDefaultCredentials = false;
                    client.Credentials = new System.Net.NetworkCredential(config.GetClientEmail(), config.GetClientPassword());
    
                    using (MailMessage mail = new MailMessage())
                    {
                        mail.Sender = new MailAddress(config.GetClientEmail(), config.GetClientName());
                        mail.From = new MailAddress(config.GetClientEmail(), config.GetClientCompany());
                        mail.To.Add(new MailAddress(config.emailToReceive));
                        mail.Subject = "Test 2";
                        mail.Body = "Test 2";
                        var isSend = false;
                        try
                        {
                            client.Send(mail);
                            isSend = true;
                        }
                        catch (Exception ex)
                        {
                            isSend = false;
                            Console.WriteLine(ex.Message);
                        }
    
                        Console.WriteLine(isSend ? "All Greeen" : "Bad Day");
                        Console.ReadLine();
                    }
                }
    
            }
        }
    }
    

    如果您添加相同的电子邮件

     mail.To.Add(new MailAddress(config.emailToReceive));
    mail.To.Add(new MailAddress(config.emailToReceive));
    

    你会报错

    错误的命令顺序。服务器响应为:5.5.0 Recipient already specified

    如果你想重用 MailMessage

      mail.To.Clear();
    

    【讨论】:

    • 执行您的建议似乎已经改变了错误。现在,我得到了这个:System.Net.Mail.SmtpException: Bad sequence of commands. The server response was: 5.5.0 Recipient already specified
    • @krobelusmeetsyndra "smtp.mail.yahoo.com"
    • 是的,这就是我使用config.GetClientSMTP() 方法传递给代码的 SMTP。
    • mail.To.Add(new MailAddress(config.emailToReceive)); mail.To.Add(new MailAddress(config.emailToReceive));
    • 很抱歉,我似乎无法仅通过提供的代码来理解这个想法。你能解释一下吗?
    【解决方案2】:

    您确定您的发件人/收件人地址正确吗? From 和 sender 必须是您的 Yahoo 地址。

    这是一个有效的示例:

    public static void Main(string[] args)
    {
    using (SmtpClient client = new SmtpClient())
    {
        client.Host = "smtp.mail.yahoo.com";
        client.EnableSsl = true;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential("my-yahoo-login", "yahoo-password");
    
        using (MailMessage mail = new MailMessage())
        {
            // This works 
            mail.Sender = new MailAddress("my-email-address@yahoo.co.uk", "Tom Test");
            mail.From = new MailAddress("my-email-address@yahoo.co.uk", "Tom Test");
            mail.To.Add(new MailAddress("my-email-address@outlook.com"));
    /* This does not
                    mail.Sender = new MailAddress("my-email-address@outlook.com", "Tom Test");
                    mail.From = new MailAddress("my-email-address@outlook.com", "Tom Test");
                    mail.To.Add(new MailAddress("my-email-address@yahoo.co.uk"));
    */
                mail.Subject = "Test mail";
            mail.Body = "Test mail";
    
            client.Send(mail);
            Console.WriteLine("Mail sent");
        }
    }
    }
    

    如果您将非雅虎地址放在发件人和发件人字段(注释代码)中,您将获得相同的异常。

    【讨论】:

    • 我确定传递给SenderFrom 字段的电子邮件是Yahoo,因为我正在使用一种方法来分配它。如果它适用于 Gmail 和 Outlook,我知道电子邮件正在正确传递,这也适用于 Yahoo。
    猜你喜欢
    • 2017-01-26
    • 2018-10-17
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 2012-10-12
    • 2017-04-17
    • 2014-05-16
    相关资源
    最近更新 更多