Click here to Skip to main content
15,936,362 members
Please Sign up or sign in to vote.
2.50/5 (4 votes)
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

C#
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
Updated 16-Jul-16 5:27am
v2

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
 
Share this answer
 
try this method
C#
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;
            }
        }
 
Share this answer
 
Comments
nagalkumar 7-May-12 7:03am    
code shows error msg
uspatel 7-May-12 7:07am    
What error occures?
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.
 
Share this answer
 
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
//Send Email with ATTACHMENTS from GODADDY using C# Asp.Net



C#
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);
    }
}
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900