Click here to Skip to main content
11,920,353 members (54,404 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET
i am just learning to send email in using gmail smtp server.. but having unhandled exception.

Unable to connect to the SErver.

here is the code whats the problem

private void button1_Click(object sender, EventArgs e)
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("");
            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.Credentials = new System.Net.NetworkCredential
                 ("", "helloworld");
            smtp.EnableSsl = true;
Posted 14-Sep-11 15:00pm
Edited 14-Sep-11 15:06pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

try setting smtp.Port = 587; before you call smtp.Send(mail);

also have a read here
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

try this method
public static Boolean SendingMail(string From, string To, string Subject, string Body)
                MailMessage m = new MailMessage("Uma<>", To);
                m.Subject = Subject;
                m.Body = Body;
                m.IsBodyHtml = true;
                m.From = new MailAddress(From);
                m.To.Add(new MailAddress(To));
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "";
                NetworkCredential authinfo = new NetworkCredential("","password");
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = authinfo;
                return true;

            catch (Exception ex)
                return false;
nagalvimalkumar 7-May-12 7:03am
code shows error msg
Uma Shankar Patel 7-May-12 7:07am
What error occures?
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You can use given function to send mail using Gmail server

public string SendMail(string toList, string from, string ccList, string subject, string body)
    MailMessage message = new MailMessage();
    SmtpClient smtpClient = new SmtpClient();
    string msg = string.Empty;
        MailAddress fromAddress = new MailAddress(from);
        message.From = fromAddress;
        if (ccList != null && ccList != string.Empty)
        message.Subject = subject;
        message.IsBodyHtml = true;
        message.Body = body;
        smtpClient.Host = "";   // We use gmail as our smtp client
        smtpClient.Port = 587;
        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new System.Net.NetworkCredential("Your Gmail User Name", "Your Gmail Password");
        msg = "Successful<BR>";
    catch (Exception ex)
        msg = ex.Message;
    return msg;
Reference Link :- Sending Email using Gmail SMTP server[^]
or have a look there[^].
and the CP search[^] many question with solution to accomplishment of sending mail task.
nahid 2 12-Sep-12 6:58am
thank was great :)
RaviRanjankr 12-Sep-12 12:01pm
Most welcome! I glad it helped you.
Member 10383617 17-Feb-14 7:10am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 415
1 phil.o 170
2 Dave Kreskowiak 155
4 F-ES Sitecore 130
0 OriginalGriff 6,778
1 KrunalRohit 4,495
2 Sergey Alexandrovich Kryukov 3,203
3 George Jonsson 2,805
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web04 | 2.8.151120.1 | Last Updated 27 Mar 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100