On trying to send an email from C#, I'm getting "Failure sending mail." The inner exception is "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 [ip address]."
Here's the code I currently have, although I've tried many variations to no avail:
string To = "[my email]";
string From = "[my email]";
string Subject = "Test Email";
string Body = "Test email.";
MailMessage completeMessage = new MailMessage(From, To, Subject, Body);
SmtpClient client = new SmtpClient("smtp.microsoft.com", 25);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential("[my login]", "[my password]");
client.EnableSsl = true;
client.Send(completeMessage);
I've tried this without the port number, without the login credentials, and without the delivery method (mostly based on other versions I've seen as correct answers to similar questions), but I always get the same exception.
UPDATE:
Based on Ruskin's advice, I tried this with my own gmail account (basically a test email from me to me). Here's what I've got in code:
string To = "[my email]@gmail.com";
string From = "[my email]@gmail.com";
string Subject = "Test Email";
string Body = "With stuff in it.";
MailMessage completeMessage = new MailMessage(From, To, Subject, Body);
SmtpClient client = new SmtpClient("smtp.gmail.com", 25);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential("[login]", "[password]");
client.EnableSsl = false;
The new message I got was "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. i3sm38798124pdf.39 - gsmtp".
After changing this line, I was able to successfully send and receive the email:
client.EnableSsl = true;
However, switching back to the original account credentials resulted in the same error from earlier.