Click here to Skip to main content
13,000,569 members (57,627 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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 14:00pm
Updated 16-Jul-16 5:27am
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 7-May-12 7:03am
   
code shows error msg
Uma Shankar Patel 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 12-Sep-12 6:58am
   
thank you.it was great :)
RaviRanjankr 12-Sep-12 12:01pm
   
Most welcome! I glad it helped you.
Member 10383617 17-Feb-14 7:10am
   
good
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

//Send Email with ATTACHMENTS from GODADDY using C# Asp.Net



public void SendMail()
{
    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
    mail.To.Add(MailTo.Text);
    mail.From = new MailAddress(MailFrom.Text,"Invoice");
    mail.Subject = Subject.Text;
    mail.Body = Body.Text;
    mail.IsBodyHtml = true;
 

 
    string FileName = Path.GetFileName(FileUploadAttachments.PostedFile.FileName);
    Attachment attachment = new Attachment(FileUploadAttachments.PostedFile.InputStream ,FileName);
    mail.Attachments.Add(attachment);            
 
    SmtpClient client = new SmtpClient();
    client.Credentials = new System.Net.NetworkCredential("Your_Email@Email.com", "Your_Email_Password");
    client.Host = "smtpout.secureserver.net";
    client.Port = 80;
    try
    {
        client.Send(mail);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
}
  Permalink  
Comments
Richard Deeming 26-Jul-16 9:55am
   
This question was asked, answered, and solved FIVE YEARS AGO.

Your solution is not relevant to the question that was asked, and adds nothing to the existing solutions.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170624.1 | Last Updated 16 Jul 2016
Copyright © CodeProject, 1999-2017
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