Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GMail SMTP via C# .Net errors on all ports

Tags:

c#

.net

gmail

I've been trying for a whlie on this, and have so far been failing miserably. My most recent attempt was lifted from this stack code here: Sending email through Gmail SMTP server with C#, but I've tried all the syntax I could find here on stack and elsewhere. My code currently is:

var client = new SmtpClient("smtp.gmail.com", 587)
{
    Credentials = new NetworkCredential("[email protected]", "mypass"),
    EnableSsl = true
};

client.Send("[email protected]","[email protected]","Test", "test message");

Running that code gives me an immediate exception "Failure sending mail" that has an innerexeption "unable to connect to the remote server".

If I change the port to 465 (as gmail docs suggest), I get a timeout every time.

I've read that 465 isn't a good port to use, so I'm wondering what the deal is w/ 587 giving me failure to connect. My user and pass are right. I've read that I have to have POP service setup on my gmail account, so I did that. No avail.

I was originally trying to get this working for my branded GMail account, but after running into the same problems w/ that I thought going w/ my regular gmail account would be easier... so far that's not the case.

like image 946
Paul Avatar asked Jul 04 '09 13:07

Paul


People also ask

Berapa port SMTP Gmail?

Di perangkat atau aplikasi Anda, hubungkan ke smtp-relay.gmail.com di salah satu port berikut: 25, 465, atau 587.

Apa itu server IMAP Gmail com?

Apa itu IMAP? IMAP (Internet Mail Access Protocol) adalah sebuah protokol email yang memungkinkan data email Anda di simpan di server secara remote kemudian bisa di akses oleh aplikasi email secara offline ataupun online. Artinya, data-data seperti folder, penandaan “flag” bisa disimpan di server Gmail.

Apa itu server SMTP?

SMTP adalah salah satu protokol standar di jaringan internet yang digunakan untuk pengiriman email dari lokal email ke mail server hingga dikirimkan ke alamat email penerima.


2 Answers

I tried your code, and it works prefectly with port 587, but not with 465.

Have you checked the fire wall? Try from the command line "Telnet smtp.gmail.com 587" If you get "220 mx.google.com ESMTP...." back, then the port is open. If not, it is something that blocks you call.

Daniel

like image 38
Daniel Svalefelt Avatar answered Oct 07 '22 03:10

Daniel Svalefelt


I ran in to this problem a while ago as well. The problem is that SmtpClient does not support implicit SSL connections, but does support explicit connections (System.Net.Mail with SSL to authenticate against port 465). The previous class of MailMessage (I believe .Net 1.0) did support this but has long been obsolete.

My answer was to call the CDO (Collaborative Data Objects) (http://support.microsoft.com/kb/310212) directly through COM using something like the following:

    /// <summary>
    /// Send an electronic message using the Collaboration Data Objects (CDO).
    /// </summary>
    /// <remarks>http://support.microsoft.com/kb/310212</remarks>
    private void SendTestCDOMessage()
    {
        try
        {
            string yourEmail = "[email protected]";

            CDO.Message message = new CDO.Message();
            CDO.IConfiguration configuration = message.Configuration;
            ADODB.Fields fields = configuration.Fields;

            Console.WriteLine(String.Format("Configuring CDO settings..."));

            // Set configuration.
            // sendusing:               cdoSendUsingPort, value 2, for sending the message using the network.
            // smtpauthenticate:     Specifies the mechanism used when authenticating to an SMTP service over the network.
            //                                  Possible values are:
            //                                  - cdoAnonymous, value 0. Do not authenticate.
            //                                  - cdoBasic, value 1. Use basic clear-text authentication. (Hint: This requires the use of "sendusername" and "sendpassword" fields)
            //                                  - cdoNTLM, value 2. The current process security context is used to authenticate with the service.

            ADODB.Field field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
            field.Value = "smtp.gmail.com";

            field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"];
            field.Value = 465;

            field = fields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
            field.Value = CDO.CdoSendUsing.cdoSendUsingPort;

            field = fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"];
            field.Value = CDO.CdoProtocolsAuthentication.cdoBasic;

            field = fields["http://schemas.microsoft.com/cdo/configuration/sendusername"];
            field.Value = yourEmail;

            field = fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"];
            field.Value = "YourPassword";

            field = fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"];
            field.Value = "true";

            fields.Update();

            Console.WriteLine(String.Format("Building CDO Message..."));

            message.From = yourEmail;
            message.To = yourEmail;
            message.Subject = "Test message.";
            message.TextBody = "This is a test message. Please disregard.";

            Console.WriteLine(String.Format("Attempting to connect to remote server..."));

            // Send message.
            message.Send();

            Console.WriteLine("Message sent.");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

Do not forget to browse through your COM references and add the "Microsoft CDO for Windows 200 Library" which should add two references: ADODB, and CDO.

like image 81
Bryan Allred Avatar answered Oct 07 '22 02:10

Bryan Allred