从C#通过gmail发送邮件出现意外错误

时间:2019-04-03 17:14:02

标签: c# email smtpclient

我正在尝试使用g-SMTP服务器从c#WinForms应用程序发送电子邮件。这是我的代码:

        string fromEmail = txtFromEmail.Text.Trim();
        string toEmail = txtToEmail.Text.Trim();
        string[] toArray = toEmail.Split(',');

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(fromEmail);
        for (int i = 0; i <= toArray.Length - 1; i++)
        {
            msg.To.Add(toArray[i]);
        }
        msg.Subject = "Test E-mail";
        msg.Body = "This is a test";
        msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
        msg.HeadersEncoding = Encoding.GetEncoding(1252);
        msg.SubjectEncoding = Encoding.GetEncoding(1252);
        msg.BodyEncoding = Encoding.GetEncoding(1252);

        AlternateView av1 = AlternateView.CreateAlternateViewFromString(msg.Body, null, System.Net.Mime.MediaTypeNames.Text.Html);
        msg.AlternateViews.Add(av1);

        smtp = new SmtpClient();
        smtp.Host = txtSMTPServer.Text.Trim();
        smtp.UseDefaultCredentials = false;

        NetworkCredential cred = new NetworkCredential();

        SecureString ss = new NetworkCredential("", txtSMTPPassword.Text.Trim()).SecurePassword;
        cred.UserName = txtSMTPUsername.Text.Trim();
        cred.SecurePassword = ss;

        smtp.Credentials = cred;
        smtp.EnableSsl = chkEnableSSL.Checked;
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.Port = Convert.ToInt16(txtSMTPPort.Text.Trim());
        smtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);


        smtp.SendAsync(msg, msg);

我收到以下错误消息:

'e.Error.InnerException.Message' threw an exception of type 'System.NullReferenceException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2147467261
HelpLink: null
InnerException: null
Message: "Object reference not set to an instance of an object."
Source: "8a9e67622e334c659c856a023d4b1631"
StackTrace: "   at <>x.<>m0(frmSettings <>4__this, Object sender, AsyncCompletedEventArgs e)"
TargetSite: {System.String <>m0(Ariba.frmSettings, System.Object, System.ComponentModel.AsyncCompletedEventArgs)}

我能够使用非Gmail SMTP服务器发送电子邮件。我想念什么?

此后,我已使用smtp.Send(msg)将send方法更改为同步,但现在却遇到了另一个错误:

{"Unable to read data from the transport connection: net_io_connectionclosed."}
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146232800
HelpLink: null
InnerException: null
Message: "Unable to read data from the transport connection: net_io_connectionclosed."
Source: "System"
StackTrace: "   at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)\r\n   at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)\r\n   at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)\r\n   at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)\r\n   at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)\r\n   at System.Net.Mail.SmtpClient.GetConnection()\r\n   at System.Net.Mail.SmtpClient.Send(MailMessage message)"
TargetSite: {Int32 ProcessRead(Byte[], Int32, Int32, Boolean)}

我意识到这可能会再次被标记为重复项,因为这可能是之前被问到的,但是我读过的任何内容都无法解决我的问题。

顺便说一句:我已将Google设置设为允许不安全的应用程序。

1 个答案:

答案 0 :(得分:0)

我们看到真正的错误在这里:

  

net_io_connection已关闭

这意味着GMail拒绝了您的连接。

这是预期的。
默认情况下,GMail不再接受SMTP连接的常规身份验证!
现在已经几年了。

如果要将GMail用作传统的SMTP服务器,则必须执行以下三种操作之一:

  1. 启用两要素身份验证并设置每个应用程序密码

  1. 支持OAuth身份验证

  1. 启用Less Secure Apps

如果您不执行其中任何一项操作,GMail将拒绝您的SMTP连接。