6

Possible Exact Duplicate: Sending Email in C#.NET Through Gmail

Hi,

I'm trying to send an email using gmail:

I tried various examples that I found on this site and other sites but I always get the same error:
Unable to connect to the remote server -- > System.net.Sockets.SocketException: No connection could be made because the target actively refused it 209.85.147.109:587

    public static void Attempt1()
    {
        var client = new SmtpClient("smtp.gmail.com", 587)
        {
            Credentials = new NetworkCredential("MyEmailAddress@gmail.com", "MyPassWord"),
            EnableSsl = true
        };
        client.Send("MyEmailAddress@gmail.com", "some.email@some.com", "test", "testbody"); 
    }

Any ideas?

UPDATE

More details.

Maybe I should say what other attempts I made that gave me the same error: (Note when i didn't specify a port it tryed port 25)

    public static void Attempt2()
    {
        var fromAddress = new MailAddress("MyEmailAddy@gmail.com", "From Name");
        var toAddress = new MailAddress("MyEmailAddy@dfdf.com", "To Name");
        const string fromPassword = "pass";
        const string subject = "Subject";
        const string body = "Body";
        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };
        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        }
            ) { smtp.Send(message); }
    }


    public static void Attempt3()
    {
        MailMessage mail = new MailMessage();
        mail.To.Add("MyEmailAddy@dfdf.com");
        mail.From = new MailAddress("MyEmailAddy@gmail.com");
        mail.Subject = "Email using Gmail";

        string Body = "Hi, this mail is to test sending mail" +
                      "using Gmail in ASP.NET";
        mail.Body = Body;

        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Credentials = new System.Net.NetworkCredential
             ("MyEmailAddy@gmail.com", "pass");
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }
Community
  • 1
  • 1

6 Answers6

9

I'm using the following code:

SmtpClient sc = new SmtpClient("smtp.gmail.com");
NetworkCredential nc = new NetworkCredential("username", "password");//username doesn't include @gmail.com
sc.UseDefaultCredentials = false;
sc.Credentials = nc;
sc.EnableSsl = true;
sc.Port = 587;
try {
    sc.Send(mm);
} catch (Exception ex) {
    EventLog.WriteEntry("Error Sending", EventLogEntryType.Error);
}
Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
  • Tried that and the very same error. Could it be a firewall issue or something? I'm doing it from work now, i could try it from home tonight –  Aug 25 '09 at 05:18
  • If you have 2-Step Verification, then you need to create a new App-Access Password, and use THAT Password (Same Username). https://security.google.com/settings/security/apppasswords – Suamere Jun 20 '16 at 03:56
2

With the following code, it will work successfully.

MailMessage mail = new MailMessage();
mail.From = new MailAddress("abc@mydomain.com", "Enquiry");
mail.To.Add("abcdef@yahoo.com");
mail.IsBodyHtml = true;
mail.Subject = "Registration";
mail.Body = "Some Text";
mail.Priority = MailPriority.High;

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
//smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "<my gmail pwd>");
smtp.EnableSsl = true;
//smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

smtp.Send(mail);

But, there is a problem with using gmail. The email will be sent successfully, but the recipient inbox will have the gmail address in the 'from address' instead of the 'from address' mentioned in the code.

To solve this, please follow the steps mentioned at the following link.

http://karmic-development.blogspot.in/2013/10/send-email-from-aspnet-using-gmail-as.html

before following all the above steps, you need to authenticate your gmail account to allow access to your application and also the devices. Please check all the steps for account authentication at the following link:

http://karmic-development.blogspot.in/2013/11/allow-account-access-while-sending.html

1

Here is my connection resource to connect to Gmail from Java

<!-- Java Mail -->
    <Resource name="mail/MailSession" auth="Container" type="javax.mail.Session" 
                mail.debug="true"
                mail.transport.protocol="smtp"
                mail.smtp.host="smtp.gmail.com"
                mail.smtp.user="youremail@gmail.com"
                mail.smtp.password="yourpassword"
                mail.smtp.port="465"
                mail.smtp.starttls.enable="true"
                mail.smtp.auth="true"
                mail.smtp.socketFactory.port="465"
                mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory"
                mail.smtp.socketFactory.fallback="false" 
                mail.store.protocol="pop3"
                mail.pop3.host="pop.gmail.com"
                mail.pop3.port="995" />

Connect your Gmail account on Secure ports (465 for SMTP and 995 for POP3) and use any .NET SSL Factory available to connect securely to Gmail.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
1

Are you sure that your GMail account is set up to allow POP/SMTP connections? It is a configurable option that you can turn on and off as you choose.

Michael Merrell
  • 1,006
  • 8
  • 21
  • It appears that gmail allows you to enable imap or pop ... BUT ... this appears to be distinct from programmatically sending email from your gmail account. – Tom Winans Nov 08 '12 at 17:28
0

Try using port number 465 for SSL connection

Joe
  • 2,381
  • 1
  • 17
  • 17
0

You can see my blog post here at http://codersatwork.wordpress.com/2010/02/14/sending-email-using-gmail-smtp-server-and-spring-mail/ which explains how to use spring mail for sending email via gmail smtp server.

I used java but you can see the configuration and use that in your c# code.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Ben W
  • 2,469
  • 1
  • 24
  • 24