2

I'm trying to send a simple test email to check how the mail class works in C#, but when trying to call smtp.send(message); I get the following error: SocketException: No connection could be made because the target machine actively refused it 173.194.207.109:587 I have looked around at the packets in wireshark (although admittedly I don't have the knowledge to make sense of them) and it seems like Google is sending back a [RST, ACK] packet every time I try to send the email. Is there a way to further diagnose this?

var fromAddress = new MailAddress("FROM@gmail.com", "FROM NAME");
var toAddress = new MailAddress("TO@gmail.com", "TO NAME");
const string fromPassword = "password";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}    
Alex Day
  • 39
  • 1
  • 7
  • Do you have an outlook account on the machine that is tied to the email server? The from address and the Network Credentials From have to be the same account. Google will also give error if the send box is full. I think the credentials should be the account name "FROM NAME" and not "From@gmail.com" – jdweng Jan 04 '19 at 17:06

1 Answers1

1

Looks like you may want to change your port value because you have EnableSsl = true,

Connect to smtp.gmail.com on port 465, if you're using SSL. (Connect on port 587 if you're using TLS.) Sign in with a Google username and password for authentication to connect with SSL or TLS. Reference

Try:

var fromAddress = new MailAddress("FROM@gmail.com", "FROM NAME");
var toAddress = new MailAddress("TO@gmail.com", "TO NAME");
const string fromPassword = "password";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 465, //or try: port = 587, or port = 25
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}

This StackOverflow Answer may also be a good reference to use.

Jaskier
  • 1,075
  • 1
  • 10
  • 33
  • I've tried both ports to no avail. I also scanned both ports with nmap and it says that they're closed. Could this be a firewall issue? If so I have no way to get around that. – Alex Day Jan 04 '19 at 17:57
  • If the ports are closed then that is an issue on the target machine or firewall, yes. You can try `port 25`. – Jaskier Jan 04 '19 at 18:00
  • The only ports that I can see are open on smtp.gmail.com are those for http and https. None of the ports listed (25, 587, 465) work in my code. I'm guessing that it is a firewall issue on my end rather than Google's SMTP server being down – Alex Day Jan 04 '19 at 18:05
  • Yes, if the ports are closed, then you cannot use them. [This Answer](https://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) may be a helpful reference? – Jaskier Jan 04 '19 at 18:08