3

I'm attempting to send mail through gmail using the below code, but I keep getting an errors stating "Unable to connect to the remote host". I've double-checked my config and everything looks fine to me. Anyone see something I'm missing?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SmtpClient client = new SmtpClient();
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("mymail@gmail.com", "mypass");
            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("mymail@gmail.com");
            mail.To.Add("tomail@tomail.org");
            mail.Subject = "subject thing";
            mail.Body = "dubbly doo";
            try
            {
                client.Send(mail);
            }
            catch(SmtpException e)
            {
                Console.Write(e.InnerException.Message);
                Console.ReadLine();
            }
        }
    }
}
lukiffer
  • 11,025
  • 8
  • 46
  • 70
steventnorris
  • 5,656
  • 23
  • 93
  • 174

3 Answers3

8

It's most likely that you have a problem reaching the host and port that you specified. This could be a range of things, one of which is being blocked by a firewall. Start by using the telnet program from a command prompt to connect to the host and port, i.e. telnet smtp.gmail.com 587. If it does connect then you can disconnect by pressing ctrl+] and typing quit when you get a prompt. If telnet fails to connect start looking into firewall/network related problems.

You can also get a file containing detailed debugging information by adding the following to your application's .config file:

<system.diagnostics>
  <sources>
    <source name="System.Net">
      <listeners>
        <add name="TraceFile"/>
      </listeners>
    </source>
    <source name="System.Net.Sockets" maxdatasize="1024">
      <listeners>
        <add name="TraceFile"/>
      </listeners>
    </source>
  </sources>
  <sharedListeners>
    <add name="TraceFile" type="System.Diagnostics.TextWriterTraceListener" initializeData="System.Net.trace.log" traceOutputOptions="DateTime"/>
  </sharedListeners>
  <switches>
    <add name="System.Net" value="Verbose"/>
    <!--<add name="System.Net.Sockets" value="Verbose"/>-->
  </switches>
  <trace autoflush="true" />
</system.diagnostics>
JamieSee
  • 12,696
  • 2
  • 31
  • 47
  • Excellent tip @JamieSee ! I might add that you can put a full path on the output file in "initializeData" where the AppPool identity has permissions to write. – Todd H. Sep 12 '14 at 15:54
1

have you setup your email account to allow smtp.gmail.com?

http://support.google.com/mail/bin/answer.py?hl=en&answer=13273

nycynik
  • 7,371
  • 8
  • 62
  • 87
  • Yes I am setup to allow smtp.gmail.com – steventnorris May 02 '12 at 20:59
  • Since your account is configured to allow smtp.gmail.com have you checked your local firewall to ensure you can access outbound on TCP 587? – lukiffer May 02 '12 at 21:20
  • @lukiffer I'm not certain of that, but I think you might be on the correct track. The firewall here is pretty intense. I'll have to check with our head sysadmin tomorrow to be sure. – steventnorris May 02 '12 at 21:27
1

Comparing your code to the answer for sending-email-in-net-through-gmail. Not positive if this is the issue, but you are missing the following when creating the SmtpClient:

DeliveryMethod = SmtpDeliveryMethod.Network

Example from the linked question:

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
Community
  • 1
  • 1
Josh Mein
  • 28,107
  • 15
  • 76
  • 87