使用Yahoo

时间:2019-01-08 10:13:33

标签: c# email exception smtp yahoo-mail

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

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帐户发送电子邮件时,该方法非常有效,因为我尝试了几次。

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

2 个答案:

答案 0 :(得分:2)

步骤1

client.Port = 587;

步骤2

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

步骤3

启用允许使用安全性较低的登录应用

enter image description here

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

您将git Error

  

命令序列错误。服务器响应为:5.5.0已指定收件人

如果您想重复使用MailMessage

  mail.To.Clear();

答案 1 :(得分:0)

您确定您的发件人/收件人地址正确吗? 发件人和发件人必须是您的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");
    }
}
}

如果将非Yahoo地址放入“发件人”和“发件人”字段(带注释的代码)中,则会遇到相同的例外情况。