使用c#从hotmail发送邮件有时会失败
本文关键字:失败 hotmail 使用 | 更新日期: 2023-09-27 18:01:24
我有c#代码发送邮件。代码是工作良好的gmail,雅虎配置。但是对于hotmail来说,有时邮件发送,有时邮件不发送。我观察到yahoo mail的响应时间比gmail要好。但是hotmail和yahoo,gmail相比反响很差。
什么是理性?如何配置hotmail以获得良好的响应。
这是我发送邮件的代码:
private void button2_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
MailMessage message = new MailMessage();
message.To.Add("user_name");
message.From = new MailAddress("user_name");
message.Subject = "Email Confirmation";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.SubjectEncoding = System.Text.Encoding.UTF8;
client.Port=587
client.Host="smtp.live.com";
System.Net.NetworkCredential nc = new System.Net.NetworkCredential("user_name", "password");
if (mail_server == "smtp.gmail.com" || mail_server == "smtp.live.com")
{
client.EnableSsl = true;
}
client.UseDefaultCredentials = false;
client.Credentials = nc;
Ping p = new Ping();
bool result = false;
int i = 10;
do
{
PingReply reply = p.Send("smtp.live.com", 587);
if (reply.Status == IPStatus.Success)
{
result = true;
} Thread.Sleep(500);
i--;
} while (result == false && i != 0);
if (result == true)
{
client.Send(message);
MessageBox.Show("Email sent successfully");
}
else
{
MessageBox.Show("Email not sent");
}
}
如果你正在尝试上面的代码发送电子邮件,它显然将无法发送它,我不知道你使用Ping
类发送电子邮件的原因。
SmtpClient client = new SmtpClient();
MailMessage message = new MailMessage();
message.To.Add("user_name");
message.From = new MailAddress("user_name");
message.Subject = "Email Confirmation";
message.BodyEncoding = Encoding.UTF8;
message.SubjectEncoding = Encoding.UTF8;
client.Port = 587;
client.Host = "smtp.live.com";
NetworkCredential nc = new NetworkCredential("user_name", "password");
string mail_server = "smtp.live.com";
if (mail_server == "smtp.gmail.com" || mail_server == "smtp.live.com")
{
client.EnableSsl = true;
}
client.UseDefaultCredentials = false;
client.Credentials = nc;
try
{
client.Send(message);
}
catch (SmtpException exception)
{
Console.WriteLine(exception.Message);
}