-1

I try to make an email service in my project using Gmail SMTP, but when the Socket connection start I get this error:

"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 64.233.166.109:25"

This is a part of the code:

private static string SmtpServer = "smtp.gmail.com";

private enum SMTPResponse : int
{
    CONNECT_SUCCESS = 220,  
    GENERIC_SUCCESS = 250,
    DATA_SUCCESS = 354,
    QUIT_SUCCESS = 221
}

public static bool Send(MailMessage message)
{
    IPHostEntry IPhst = Dns.GetHostEntry(SmtpServer);
    IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25);
    Socket s = new Socket(endPt.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    s.Connect(endPt);
}
croxy
  • 4,082
  • 9
  • 28
  • 46
Shoupash
  • 11
  • 3
  • 1
    Why are you trying to send email using sockets rather than `SmtpClient`? – mjwills Aug 22 '18 at 10:23
  • Gmail doesn't use port 25 which is non-secure email. Check the documentation at GMAIL website to get exact SMTP format. Make sure the FROM email address and the Credentials use the same email account. – jdweng Aug 22 '18 at 10:24
  • @mjwills Actually I am new to this and I use this https://codesnipets.wordpress.com/2010/04/21/smtp-email-using-sockets-system-web-mail/ as a guide – Shoupash Aug 22 '18 at 10:26
  • Thanks @jdweng I will check this out – Shoupash Aug 22 '18 at 10:28
  • `.NET Framework 4.0 has new property called SMTPClient.Dispose() to fix the issue` That article explains that it is basically pointless to do it that way if you are on 4.0 onwards. You should not follow that article. – mjwills Aug 22 '18 at 10:28
  • Take a look at https://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail?rq=1 – WynDiesel Aug 22 '18 at 10:31

1 Answers1

0

Try port 587 instead of port 25.

Port 25 is for smtp server to smtp server smtp communication.
Port 587 is for client to server smtp communication.

Outgoing connections to port 25 are frequently blocked to stop outgoing spam.
Connections to port 587 require some kind of authentication.

https://en.wikipedia.org/wiki/Mail_submission_agent

AnFi
  • 10,493
  • 3
  • 23
  • 47