0

I am having an issue in my project trying to developing a mail webservice but getting an error transport failed to connect to server.
Below is the code...

protected void btnsubmit_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.From = "ruchi.dubey18@gmail.com";
        mail.To = txtTo.Text.Trim();
        mail.Subject = txtSubject.Text.Trim();
        mail.Body = txtBody.Text.Trim();
        SmtpMail.SmtpServer = "smtp.gmail.com";
        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "ruchi.dubey18@gmail.com");
 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "jaishreek");

 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "2");
        SmtpMail.Send(mail);
    }

please help its urgent...

Edited by __avd: Added [code] tags.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by __avd
0

You need to set username/password, and other parameters in order to send an email.

For instance,

try
{
    System.Net.Mail.MailMessage mm=new System.Net.Mail.MailMessage("to@aaa.com","aaa@gmail.com");

   mm.Subject = "Subject details";
   mm.Body = "Sample...";

   SmtpClient sc = new System.Net.Mail.SmtpClient();
   System.Net.NetworkCredential auth = new  System.Net.NetworkCredential("your@gmail.com", "your_pass");

   sc.Host = "smtp.googlemail.com";
   sc.Port = 587;
   sc.UseDefaultCredentials = false;
   sc.Credentials = auth;
   sc.EnableSsl = true;
   sc.Send(mm);

   Response.Write( "your mail has been sent successfully..");
   }catch (Exception ex)
    {
            Response.Write(ex);
     }
.....
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.