"sending email through gmail smtp server with c#" Code Answer

3

cvertex, make sure to review your code, and, if that doesn't reveal anything, post it. i was just enabling this on a test asp.net site i was working on, and it works.

actually, at some point i had an issue on my code. i didn't spot it until i had a simpler version on a console program and saw it was working (no change on the gmail side as you were worried about). the below code works just like the samples you referred to:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.net.mail;
using system.net;

namespace consoleapplication2
{
    class program
    {
        static void main(string[] args)
        {
            var client = new smtpclient("smtp.gmail.com", 587)
            {
                credentials = new networkcredential("myusername@gmail.com", "mypwd"),
                enablessl = true
            };
            client.send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
            console.writeline("sent");
            console.readline();
        }
    }
}

i also got it working using a combination of web.config, http://msdn.microsoft.com/en-us/library/w355a94k.aspx and code (because there is no matching enablessl in the configuration file :( ).

2021 update

by default you will also need to enable access for "less secure apps" in your gmail settings page: google.com/settings/security/lesssecureapps. this is necessary if you are getting the exception "`the server response was: 5.5.1 authentication required. – thanks to @ravendarksky

By mivk on May 30 2022

Answers related to “sending email through gmail smtp server with c#”

Only authorized users can answer the Search term. Please sign in first, or register a free account.