首页 > 解决方案 > 我无法使用 smtp 从 gmail 发送电子邮件

问题描述

无法使用 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();
        }
    }
}

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

标签: c#emailgmailvisual-studio-2019

解决方案


推荐阅读