SmtpClient send mail

17,001

Solution 1

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>

Solution 2

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

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

Solution 3

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)
};
Share:
17,001
steventnorris
Author by

steventnorris

Applications developer with a preference for object-oriented languages. Currently working at FortyAU, a development firm in Nashville, TN. http://www.fortyau.com

Updated on June 04, 2022

Comments

  • steventnorris
    steventnorris almost 2 years

    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("[email protected]", "mypass");
                client.Port = 587;
                client.Host = "smtp.gmail.com";
                client.EnableSsl = true;
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress("[email protected]");
                mail.To.Add("[email protected]");
                mail.Subject = "subject thing";
                mail.Body = "dubbly doo";
                try
                {
                    client.Send(mail);
                }
                catch(SmtpException e)
                {
                    Console.Write(e.InnerException.Message);
                    Console.ReadLine();
                }
            }
        }
    }
    
  • steventnorris
    steventnorris about 12 years
    Yes I am setup to allow smtp.gmail.com
  • lukiffer
    lukiffer about 12 years
    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?
  • steventnorris
    steventnorris about 12 years
    @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.
  • Todd H.
    Todd H. over 9 years
    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.
  • Joshua Frank
    Joshua Frank over 8 years
    That's actually the default value for DeliveryMethod.