SmtpClient send mail

I am trying to send mail via gmail using the code below, but I continue to receive the error message “Cannot connect to the remote host”. I double-checked my config and everything looks good to me. Does anyone see what I am 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();
            }
        }
    }
}
+3
source share
3 answers

, . , . telnet , .. telnet smtp.gmail.com 587. , , ctrl +] quit, . telnet , /.

, .config:

<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>
+8

sending-email-in-net-through-gmail. , , SmtpClient :

DeliveryMethod = SmtpDeliveryMethod.Network

:

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
+1
source

All Articles