2

Can't send an email with SMTP. I always get this error:

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

I enabled in my Gmail account for less secured app and protocol POP and IMAP.

This is my code

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();
        }
    }
}

I don't know if there is a problem in my code or if I have something wrong in settings. Any suggestions about how to fix this?

  • What version of .Net Framework are you using? – Crowcoder May 26 '19 at 11:14
  • Are you able to send a mail throw a local SMTP server ? Could you be blocked by a firewall ? – Skrface May 26 '19 at 11:19
  • It usually works. Try it with port 25 instead. Note that SmtpClient is obsolete/deprecated. You can [see the warning](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient) in the Docs. – Jimi May 26 '19 at 11:19
  • I created Console app(.net core), do I have to use (.net Framework)? – Dalibor Barabas May 26 '19 at 11:21
  • You cannot do anything without targeting a framework (it's in the project properties). Pick the more recent you have. – Jimi May 26 '19 at 11:23
  • I created new project .net framework and it is working, can I ask what is the main difference between core and network or when should I use which, I am new into visual studio – Dalibor Barabas May 26 '19 at 11:27
  • [.NET Standard vs .NET Core](https://stackoverflow.com/q/44085424/7444103). – Jimi May 26 '19 at 13:30

0 Answers0