简体   繁体   English

从hotmail发送邮件有时会失败,使用C#

[英]sending mail from hotmail fails sometimes using c#

I have c# code to send mail. 我有C#代码发送邮件。 The code is working fine for gmail,yahoo configuration. 该代码对于gmail,yahoo配置工作正常。 But for hotmail sometimes the mail is sent and sometimes the mail is not sent. 但是对于hotmail,有时会发送邮件,而有时不会发送邮件。 I observed that the response time for yahoo mail is better compare to gmail. 我观察到,与gmail相比,雅虎邮件的响应时间更好。 But hotmail has very poor response compare to yahoo,gmail. 但是与yahoo,gmail相比,hotmail的响应非常差。

What will be reason? 会是什么原因? how to configure hotmail to get good response. 如何配置hotmail以获得良好的响应。

this is my code to send mail : 这是我发送邮件的代码:

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

                }
 }

If you are trying the above code to Send Email, it will obviously not be able to send it, I dont know for what reason you are using the Ping class to send email. 如果您尝试使用上述代码发送电子邮件,则显然将无法发送它,我不知道您使用Ping类发送电子邮件的原因是什么。

Fixed Code : 固定代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM