0

我有 C# 代码来发送邮件。该代码适用于 gmail、yahoo 配置。但是对于hotmail,有时会发送邮件,有时不会发送邮件。我观察到 yahoo 邮件的响应时间比 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");

                }
 }
4

1 回答 1

0

如果您尝试使用上述代码发送电子邮件,它显然无法发送它,我不知道您使用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);
    }
于 2013-02-26T13:33:40.527 回答