从C#通过gmail发送邮件会产生意外错误

问题描述 投票:0回答:1

我正在尝试使用g-mail 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设置设置为允许不安全的应用。

c# email smtpclient
1个回答
0
投票

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

net_io_connectionclosed

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

这是预料之中的。 默认情况下,GMail不再接受SMTP连接的常规身份验证! 它已经有几年了。

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

  1. 启用双因素身份验证并设置每应用程序密码

要么

  1. 支持OAuth身份验证

要么

  1. 启用Less Secure Apps

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

© www.soinside.com 2019 - 2024. All rights reserved.