我无法使用SMTP发送来自Gmail的电子邮件

时间:2019-05-26 11:04:58

标签: c# email gmail visual-studio-2019

无法通过SMTP发送电子邮件。我总是会收到此错误:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No connection could be made because the target machine actively refused it. [::ffff:74.125.140.109]:587

我在Gmail帐户中启用了安全性较低的应用程序和协议POP和IMAP。

这是我的代码

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

namespace send_email
{
    class Program
    {
        static void Main(string[] args)
        {
            SmtpClient clientDetails = new SmtpClient();
            clientDetails.Port = 587;
            clientDetails.Host = "smtp.gmail.com";
            clientDetails.EnableSsl = true;
            clientDetails.DeliveryMethod = SmtpDeliveryMethod.Network;
            clientDetails.UseDefaultCredentials = false;
            clientDetails.Credentials = new NetworkCredential("login","pasword");

            MailMessage mesasge = new MailMessage();
            mesasge.From = new MailAddress("app.reminder.2019@gmail.com");
            mesasge.To.Add("barabas.dalibor@gmail.com");
            mesasge.Subject = "Test";
            mesasge.Body = "This is Test";
            try
            {
                clientDetails.Send(mesasge);
            }
            catch (Exception e) {
                Console.WriteLine(e);
            }
            Console.ReadLine();
        }
    }
}

我不知道我的代码是否有问题或设置是否有问题。有关如何解决此问题的任何建议?

0 个答案:

没有答案