使用 Yahoo 发送电子邮件时出现 SMTP 异常
SMTP exception when sending email with Yahoo
我使用以下代码从我的应用程序发送电子邮件:
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: Mailbox unavailable. The server response was: Requested mail action not taken: mailbox unavailable.
我知道一个事实,当使用 Gmail 和 Outlook 帐户发送电子邮件时,该方法非常有效,因为我尝试了几次。
我做错了什么?任何帮助将不胜感激!
第 1 步
client.Port = 587;
第 2 步
转到https://login.yahoo.com/account/security
第 3 步
启用允许使用不太安全的应用登录
第 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错误
Bad sequence of commands. The server response was: 5.5.0 Recipient already specified
如果您想重复使用 MailMessage
mail.To.Clear();
您确定您的 from/to 地址正确吗?
发件人和发件人必须是您的 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");
}
}
}
如果您将您的非雅虎地址放在发件人和发件人字段(注释代码)中,您将得到相同的异常。
我使用以下代码从我的应用程序发送电子邮件:
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: Mailbox unavailable. The server response was: Requested mail action not taken: mailbox unavailable.
我知道一个事实,当使用 Gmail 和 Outlook 帐户发送电子邮件时,该方法非常有效,因为我尝试了几次。
我做错了什么?任何帮助将不胜感激!
第 1 步
client.Port = 587;
第 2 步
转到https://login.yahoo.com/account/security
第 3 步
启用允许使用不太安全的应用登录
第 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错误
Bad sequence of commands. The server response was: 5.5.0 Recipient already specified
如果您想重复使用 MailMessage
mail.To.Clear();
您确定您的 from/to 地址正确吗? 发件人和发件人必须是您的 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");
}
}
}
如果您将您的非雅虎地址放在发件人和发件人字段(注释代码)中,您将得到相同的异常。