954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Sending mail with asp.net/c#

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);
Shillz
Newbie Poster
23 posts since Oct 2009
Reputation Points: 5
Solved Threads: 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.

AleMonteiro
Junior Poster
164 posts since Aug 2010
Reputation Points: 15
Solved Threads: 30
 

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

Shillz
Newbie Poster
23 posts since Oct 2009
Reputation Points: 5
Solved Threads: 0
 

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

websoftcreation
Newbie Poster
6 posts since Nov 2011
Reputation Points: 4
Solved Threads: 0
 

hi,

thanks for the reply, but i got the same code working. It was SMTP problem.
Please help me with another problem i discussed in thread on link given below

http://www.daniweb.com/web-development/aspnet/threads/404986

Shillz
Newbie Poster
23 posts since Oct 2009
Reputation Points: 5
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: