0

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.

Nightmare Games
  • 2,205
  • 6
  • 28
  • 46
  • Very difficult to diagnose these kind of issues over here given that all the details you use have been changed, have you tried the above code with a simple GMail account? – Ruskin Jan 03 '15 at 06:38
  • @Ruskin: Thanks for the idea. I've updated the question accordingly. – Nightmare Games Jan 05 '15 at 18:16

2 Answers2

0

Try with EnableSsl=false, since you're using port 25 (which is plain SMTP): http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol#Ports.

Or use the proper port for secure connections.

HiDefLoLife
  • 555
  • 1
  • 8
  • 28
  • Thanks, but I got the same exception. – Nightmare Games Jan 03 '15 at 00:27
  • If you haven't done so already, one of the ways that I use to debug this type of issues is to use something like Outlook (or equivalent) to verify that the server is correct, credentials work, port settings are correct, etc. Once this sanity check is done, then I confirm the settings in code. – HiDefLoLife Jan 08 '15 at 21:11
  • Thanks for the idea. The info I'm seeing in Outlook isn't matching up with the settings SmtpClient is looking for, though (unless I'm looking in the wrong spot). For example, there's no port number as far as I can tell. There is a server value, but it's a long string of characters ending with @microsoft.com as if it were an email address. I've tried plugging that in as a replacement for the smtp.microsoft.com value before, but received an error. – Nightmare Games Jan 12 '15 at 19:27
  • If it is a POP/SMTP account, you should be able to access the account information: https://help.1and1.com/e-mail-and-office-c37589/outlook-c85091/set-up-e-mail-with-microsoft-outlook-2010-using-pop3-a759436.html – HiDefLoLife Jan 13 '15 at 17:56
  • For Microsoft Exchange access, check out this URL: https://support.office.com/en-US/Article/Settings-for-POP-and-IMAP-access-for-Office-365-for-business-or-Microsoft-Exchange-accounts-7fc677eb-2491-4cbc-8153-8e7113525f6c?ui=en-US – HiDefLoLife Jan 14 '15 at 18:11
0

Try your Gmail settings following this, let me know if you still get the "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]." issue

Community
  • 1
  • 1
Ruskin
  • 1,504
  • 13
  • 25
  • Thanks, but I actually already got a test email working with gmail, and the settings appear to be the same as what they have here (except for the port). It's with the original email account I was trying to use that I'm still having the problem. – Nightmare Games Jan 06 '15 at 22:35
  • I can't find any help around using smtp.microsoft.com, where did you extract those settings from? If it's an exchange email, the settings are a little different, see this link http://stackoverflow.com/questions/6412162/send-mails-using-exchange-server-microsoft-outlook-web-accessin-asp-net – Ruskin Jan 07 '15 at 19:03
  • Thanks for the link, but I can't seem to find any references to add for Microsoft.Exchange.WebServices.Data, which is preventing me from trying that answer. – Nightmare Games Jan 12 '15 at 19:11