0

I am trying to send an email by building a console based application but whenever i try to run it it gives an error which says "Socket Exception: No connection could be made because the target machine actively refused it 74.125.130.109:587" , can someone explain me what is wrong with my code or if not with my system settings?

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

namespace Email
{
class Program
{
    static void Main(string[] args)
    {
        Program obj = new Program();
        obj.SendEmail("Just a Mock Message");
    }
    public void SendEmail(string emailBody)
    {
        MailMessage mail = new MailMessage("FromEmail.com","ToEmail.com");
        mail.Subject = "Testing my Application";
        mail.Body = emailBody;
        SmtpClient smtp = new SmtpClient("smtp.gmail.com",587);
        smtp.Credentials = new NetworkCredential()
        {
            UserName = "EmailExample.com",
            Password = "example123"
        };

        smtp.EnableSsl = true;
        smtp.Send(mail);
    }

}
}
Shubham Tiwari
  • 959
  • 1
  • 11
  • 32

1 Answers1

1

smtp.gmail.com default port for SSL is 465. Try with

 SmtpClient smtp = new SmtpClient("smtp.gmail.com",465);
Perfect28
  • 11,089
  • 3
  • 25
  • 45