在C#Windows应用程序中发送电子邮件

时间:2019-03-06 17:10:39

标签: c# email gmail

我正在创建一个简单的应用程序,使您可以发送电子邮件。但是它给了我一个System.Net.Mail.SmtpException:'发送邮件失败。' 这是我的代码:

string FromEmailAdress = TxtFromEmailAdress.Text;
        string ToEmailAdress = TxtToEmailAdress.Text;
        string MessageText = TxtMessage.Text;
        string Subject = "Email from C#";
        MailMessage message = new MailMessage(FromEmailAdress , ToEmailAdress ,Subject, MessageText);
        SmtpClient smtp = new SmtpClient();
        message.From = new MailAddress(FromEmailAdress);
        message.To.Add(new MailAddress(ToEmailAdress));

        smtp.Port = 467;
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;

        smtp.Credentials = new NetworkCredential("MyEmail@gmail.com", "My Password");
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.Send(message);
        MessageBox.Show("Message sended");

The exception

1 个答案:

答案 0 :(得分:0)

根据https://support.google.com/a/answer/176600?hl=en,对于SSL,您需要使用端口465而不是端口467;对于TLS,则需要使用端口587。