1

in my application i m sending a mail from gmail server. when i m executing the code its giving error

here is the code

    mail.To.Add(txtTo.Text);
    mail.From = new MailAddress(txtFrom.Text);
    mail.Subject = txtSubject.Text;
    mail.Body = txtBody.Text;
    mail.IsBodyHtml = true;
    if (FileUpload1.HasFile)
    {
          mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream,        FileUpload1.FileName));
    }
     SmtpClient smtp = new SmtpClient();
     smtp.Host = "smtp.gmail.com"; 
     smtp.Port = 587;
     smtp.UseDefaultCredentials = false;

     smtp.Credentials = new System.Net.NetworkCredential
         ("my gmail ID", "my password");

    smtp.EnableSsl = true;
    smtp.Send(mail);

After execution i m getting below error

    Message : -  "Failure sending mail."
   Inner Exception : - {"Unable to connect to the remote server"}   

please have a look.

Gideon
  • 18,251
  • 5
  • 45
  • 64
Praveen k. Agrawal
  • 439
  • 1
  • 5
  • 15

1 Answers1

0

Inner Exception : - {"Unable to connect to the remote server"}

Check your firewall settings.

Also, are you using a shared hosting? Host shared hostings (at least the ones I know) do not allow you to do socket communication. Some allow it when you get dedicated IP.

Sarwar Erfan
  • 18,034
  • 5
  • 46
  • 57