Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

c# - Gmail: How to send an email programmatically

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("[email protected]", "MyPassWord"),
            EnableSsl = true
        };
        client.Send("[email protected]", "[email protected]", "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("[email protected]", "From Name");
        var toAddress = new MailAddress("[email protected]", "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("[email protected]");
        mail.From = new MailAddress("[email protected]");
        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
             ("[email protected]", "pass");
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...