简体   繁体   English

SMTP电子邮件未发送

[英]SMTP Email not sending

I made a simple program to send e-mails, but it fails to send them and gives me an exception. 我编写了一个简单的程序来发送电子邮件,但发送失败,给了我一个例外。

Here's the code: 这是代码:

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

public class test{
    public static void Main()
    {
        SmtpClient client = new SmtpClient();
        client.Host = "smtp.gmail.com";
        client.Port = 465;
        client.EnableSsl = true;
        client.Credentials = new NetworkCredential("myemail@gmail.com","mypassword");
        client.Send("myemail@gmail.com","myemail@gmail.com","Test","Test");
    }
}

NOTE: Just added the following lines of code: 注意:只需添加以下代码行:

client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;

and changed the port to 587, then it says that authentication is required, and I alredy put my credentials, maybe its because the port 587 needs TLS not SSL (from what I learned searching about this today, I know that SSL is an "evolution" of TLS, no?) 并将端口更改为587,然后说需要进行身份验证,然后我放了我的凭据,也许是因为端口587需要TLS而不是SSL(从我今天所学的搜索知识中,我知道SSL是一种“进化的TLS”,否?)

Tunigamer, just tried the code below and that worked: Tunigamer,只是尝试了下面的代码,并且有效:

SmtpClient gmailClient = new SmtpClient {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential("email@gmail.com", "password")
            };


            using(MailMessage gMessage = new MailMessage("email@gmail.com", "email@gmail.com", "Test", "Test")) {

                try {
                    gmailClient.Send(gMessage);
                } catch(Exception) { }
            }

Also, I received this notification below from GMAIL, you will need to disable the sign-in security features of your account: 另外,我也是从GMAIL收到此通知的,您需要禁用帐户的登录安全功能:

在此处输入图片说明

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

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