简体   繁体   English

在Windows App中使用gmail SMTP服务器发送电子邮件时出错

[英]Getting error in sending email by using gmail SMTP server in windows app

I am trying to code software in C# to send email via using gmail account with help of the gmail SMTP server. 我正在尝试使用C#编写软件,以便在gmail SMTP服务器的帮助下通过使用gmail帐户发送电子邮件。 It does not produce any compile errors, but throws an exception at runtime even though code is correct and fine. 它不会产生任何编译错误,但是即使代码正确无误,也会在运行时引发异常。

Here is my code: 这是我的代码:

using System.Net;
using System.Net.Mail;

private void button1_Click(object sender, EventArgs e)
{
    if (from.Text != "" && password.Text != "" && to.Text != "" && subject.Text != "" && receive.Text != "")
    {
        try
        {
            MailMessage mail = new MailMessage(from.Text, to.Text, subject.Text, receive.Text);

            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            SmtpServer.Port = 465;
            SmtpServer.Credentials = new System.Net.NetworkCredential(from.Text, password.Text);
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);

            MessageBox.Show("Mail Sent");
        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.ToString());
            textBox6.Text = ex.ToString();
        }
    }
    else
    { 
        MessageBox.Show("Fill all fields, then press Send button"); 
    }

Here is the exception I get: 这是我得到的异常:

System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. System.Net.Mail.SmtpException:SMTP服务器需要安全连接,或者客户端未通过身份验证。 The server response was: 5.5.1 Authentication Required. 服务器响应为:5.5.1需要身份验证。 Learn more at 了解更多

at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) 在System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,字符串响应)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) 在System.Net.Mail.MailCommand.Send(SmtpConnection conn,Byte []命令,MailAddress来自,布尔值allowUnicode)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) 在System.Net.Mail.SmtpTransport.SendMail(MailAddress发件人,MailAddressCollection收件人,字符串deliveryNotify,布尔值allowUnicode,SmtpFailedRecipientException&异常)
at System.Net.Mail.SmtpClient.Send(MailMessage message) 在System.Net.Mail.SmtpClient.Send(MailMessage消息)
at Gmail_Sender.Form1.button1_Click(Object sender, EventArgs e) in C:\\Users\\junaid 在C:\\ Users \\ junaid中的Gmail_Sender.Form1.button1_Click(对象发件人,EventArgs e)

GMail does not support port 465 for SMTP submission. GMail不支持端口465进行SMTP提交。

You need to use port 587. 您需要使用端口587。

That said, they probably won't talk to you anyway, since they're very fussy about IP addresses. 话虽如此,他们可能仍然不会与您交谈,因为他们对IP地址非常挑剔。 If you're on a dynamic range, it's probably not going to work even if you do it correctly. 如果您处于动态范围内,则即使正确执行它也可能无法正常工作。

IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed. IO.IOException:无法从传输连接中读取数据:net_io_connectionclosed。

The error message indicates that Google didn't want to talk to you and closed the connection. 错误消息表明Google不想与您交谈并关闭了连接。

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

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