I have been trying to send mail through my c# website, but kinda stuck up somewhere and not able to figure out the problem. :-/
M getting SmtpException saying "Failure sending mail". Please help.:icon_confused:

MailAddress fromAddress = new MailAddress("abc@gmail.com", "n1");
                MailAddress toAddress = new MailAddress("xyz@yahoo.co.in", "n2");
                const string fromPassword = "pwd";
                const string subject = "Subject";
                const string body = "Body";
                // SMTP Configuration
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new System.Net.NetworkCredential(fromAddress.Address, fromPassword);
                MailMessage message = new MailMessage(fromAddress, toAddress);
                message.Subject = subject;
                message.Body = body;
                // SEND EMAIL

Recommended Answers

All 4 Replies

Your code looks ok, did you check those itens:
- From e-mail and from password are really ok?
- Gmail port is really 587?

If it's ok, try those things:
- Don't set EnableSsl
- Don't set UseDefaultCredentials
- Don't set DeliveryMethod

i used gmail smpt and never set those, and it worked. But i don't remeber the smpt port.

Hope it helps.

Thank you...
I found the problem. SMTP for my system was disabled by the OTG dept. Code is working fine now.


You can do this by just write some lines of code which has given below

protected void Button1_Click(object sender, EventArgs e)
  MailMessage mail = new MailMessage();
  mail.From = new MailAddress("jainamit.agra@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"; //Or Your SMTP Server Address
  smtp.Credentials = new System.Net.NetworkCredential
//Or your Smtp Email ID and Password
  smtp.EnableSsl = true;


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.