Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
i am just learning to send email in asp.net 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.To.Add(textBox1.Text);
            
            mail.From = new MailAddress("farooqsp@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
                 ("farooqsp@gmail.com", "helloworld");
            
            smtp.EnableSsl = true;
            smtp.Send(mail);
 
        }
    }
Posted 14-Sep-11 15:00pm
Edited 14-Sep-11 15:06pm
v2
Rate this: bad
good
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 https://mail.google.com/support/bin/answer.py?answer=13287
  Permalink  
Rate this: bad
good
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)
    {
        
            try
            {
                MailMessage m = new MailMessage("Uma<test@gmail.com>", 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 = "smtp.gmail.com";
              
                NetworkCredential authinfo = new NetworkCredential("test@gmail.com","password");
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = authinfo;
                smtp.Send(m);
                return true;
 
               
 

            }
            catch (Exception ex)
            {
                return false;
            }
        }
 
  Permalink  
Comments
nagalvimalkumar at 7-May-12 7:03am
   
code shows error msg
Uma Shankar Patel at 7-May-12 7:07am
   
What error occures?
Rate this: bad
good
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;
    try
    {
        MailAddress fromAddress = new MailAddress(from);
        message.From = fromAddress;
        message.To.Add(toList);
        if (ccList != null && ccList != string.Empty)
            message.CC.Add(ccList);
        message.Subject = subject;
        message.IsBodyHtml = true;
        message.Body = body;
        smtpClient.Host = "smtp.gmail.com";   // 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");
 
        smtpClient.Send(message);
        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.
  Permalink  
Comments
nahid 2 at 12-Sep-12 6:58am
   
thank you.it was great :)
RaviRanjankr at 12-Sep-12 12:01pm
   
Most welcome! I glad it helped you.
Member 10383617 at 17-Feb-14 7:10am
   
good

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

  Print Answers RSS
0 Mathew Soji 274
1 BillWoodruff 270
2 Afzaal Ahmad Zeeshan 244
3 Sergey Alexandrovich Kryukov 240
4 Raul Iloc 160
0 OriginalGriff 6,219
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,103
3 Manas Bhardwaj 4,549
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 27 Mar 2014
Copyright © CodeProject, 1999-2014
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