无法通过Gmail发送电子邮件

时间:2015-02-04 08:36:20

标签: c# gmail

我正在尝试使用stmp在c#中通过gmail发送,但继续在端口587和端口465下获取身份验证5.5.1问题,我得到一个操作超时。

请查看我的代码,让我知道我出错的方式和地点以及如何解决。

感谢

private void SendMessage(object sender, RoutedEventArgs e)
    {

        var connected = ConnectedToInternet();
        if (!connected) return;

        var Client = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 465,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            Credentials = new NetworkCredential(txtUserName.Text, txtPassword.Password),
            Timeout = 20000
        };

        var m = new MailMessage { Subject = txtSubject.Text, IsBodyHtml = true, Body = txtMessage.Text };
        foreach (var email in LbEmailAddresses.Items)
        {
            try
            {
                m.To.Add(new MailAddress(email.ToString().Trim()));
                m.From = new MailAddress(txtUserName.Text);
                Client.Send(m);
                m.To.Clear();
                Success.Add(email.ToString());
            }
            catch (SmtpException esException)
            {
                Errors.Add("Error sending to " + email + " " + esException.Message);
            }
            catch (Exception ex)
            {
                Errors.Add("Error sending to " + email + " " + ex.Message);

            }

        }
        var message = new StringBuilder();
        message.AppendLine(Success.Count() + " Sent Successfully");
        message.AppendLine(Errors.Count() + " Errors Click See Error Log for more information");
        if (CustomMessageBox.ShowYesNo(message.ToString(),
            "Result",
            "Ok",
            "View Log") != MessageBoxResult.No) return;
        using (var sw = new StreamWriter("log.txt"))
        {
            foreach (var error in Errors)
            {
                sw.WriteLine(error);
            }
            Process.Start("log.txt");
        }
    }

1 个答案:

答案 0 :(得分:0)

我上面关于来自Google的电子邮件的评论...如果您要从c#或其他Google批准的应用发送,则需要降低帐户的安全性。