0

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);
                // MAIL MESSAGE CONFIGURATION
                MailMessage message = new MailMessage(fromAddress, toAddress);
                message.Subject = subject;
                message.Body = body;
                // SEND EMAIL
                smtp.Send(message);

Edited by Shillz: n/a

3
Contributors
4
Replies
9
Views
5 Years
Discussion Span
Last Post by Shillz
0

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.

0

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

-1

Hi,

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.To.Add("jainamit.agra@gmail.com");
  mail.To.Add("amit_jain_online@yahoo.com");
  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
       ("YourUserName@gmail.com","YourGmailPassword");
//Or your Smtp Email ID and Password
  smtp.EnableSsl = true;
  smtp.Send(mail);
}

From
Websoftcreation,jaipur

Edited by Nick Evan: Fixed formatting

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.