Take the 2-minute tour ×

I use the following code to send email.Sometimes it works fine and some time it generate error . Is there any better code to send emails.And one more thing is it necessary to provide password for sending mail.

using System.Net.Mail;
public void SendEmail()
    MailMessage mail = new MailMessage();

    mail.From = new MailAddress("ojhatraderscustomer@gmail.com");
    mail.Subject = "Contact Us Enquiry";

    string Body = "<b>From:<b>" + mail.From + "<br/>" + "Your Query Recived "+"<br/>"+"Name"+nameTextBox.Text+"<br/>"+"Mobile:"+mobileTextBox.Text+"<br/>"
    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("sample@gmail.com", "passsword");//Or your Smtp Email ID and Password
    smtp.EnableSsl = true;

Please provide some useful suggestion and better code.

share|improve this question
What's the error you get? That may help in attempting to find a solution. –  justin.chmura Jul 5 '13 at 17:02
What exception are you talking about? It would be easier to help if we knew the nature of the problem –  Charles Keyser Jul 5 '13 at 17:02
All your question depend on your service provider. You're using SMTP. Does your service provider give you any other options? Does your service provider require a password? –  banging Jul 5 '13 at 17:03
Wrap this code in Try/Catch and give us some exception codes. –  AmitApollo Jul 5 '13 at 17:04
@AmitApollo that is an awful idea, doing a high level catch could hide problems with the system that would be easy to solve if the exception is just left to be thrown –  Charles Keyser Jul 5 '13 at 17:05
show 5 more comments

3 Answers

up vote 1 down vote accepted

Have you tried adding

     smtp.Port = 587;   

The best bet to keep your credentials secret is having an SMTP Server running that accepts anonymous referrers. With this criteria met, credentials aren't required with anonymous requests. There are fewer and fewer mail services allowing anonymous requests, while it used to be rampant < 10 years ago. Now most SMTP services require valid username and password credentials, some even network domain credentials.

share|improve this answer
He says it sends sometimes. If it was a port issue it would never send. –  banging Jul 5 '13 at 17:13
It's discerning there's no port specified. 587 is the SSL port for google. I think everyone has used this same segment of code before, and port is always added. –  AmitApollo Jul 5 '13 at 17:16
Thanks AmitApollo now mail was send successfully. Now can i deploy my project,i was just little bit scared if any error occurs of mail failure after project delivery. –  Mohd Waseem Jul 5 '13 at 17:36
No problem Mohd! Good luck with your project delivery –  AmitApollo Jul 5 '13 at 17:45
add comment


Without informing us with the error that your code generates, we can't really help solving that issue. You should check the recommended smtp-port. From Google support websuite:

If you tried configuring your SMTP server on port 465 (with SSL) and port 587 (with TLS), but are still having trouble sending mail, try configuring your SMTP to use port 25 (with SSL).

'Better' code

Sending mails are quite obvious in ASP.NET so 'better' code to send mails is more an opinion rather than a fact. However, I should split things up a little bit from an architectural point of view. This will improve code quality and reduce duplicate code.

Things you should consider do to make this code 'better'.

  1. Make use of a stringbuilder to build your mail content and inject it into mailtemplate
  2. Make a separate class 'Email' with e.g. default constructor, Send() and GetTemplate() method
  3. Specify settings for SMTP in your web.config

That way you can make and send your e-mail from anywhere in your application in a few lines. Some example code from one of my applications:

var content = new StringBuilder();
content.Append("Name: " + contactForm.Name + "<br/>");
content.Append("Email: " + contactForm.Name + "<br/>");
content.Append("Message: " + contactForm.Name + "<br/>");

//Email constructor accepts two arguments: the content and the name of the template
var mail = new Email(content, "mailTemplateName")
mail.Send("mymail@domain.be", "recipient@gmail.com", "Subject of the mail")
share|improve this answer
add comment

Try including a port for your SMTP. Ones that are typically used are 25, 465, and 587. I usually use 25 without issues and you can also use 465 for ssl. I also usually set UseDefaultCredentials to false:

SmtpClient smtp = new SmtpClient();
smtp.Port = 25;
smtp.UseDefaultCredentials = false;

Also, you might want to dispose of your messages after you've used them. It's good practice and is safer for performance and cost.

using (smtp as IDisposable) 

All in all, pretty open ended question. Hope this helps, but be a little more specific so you can get better help next time

share|improve this answer
Better: using (SmtpClient smtp = new SmtpClient{Port=25, UseDefaultCredentials=false}){...} –  John Saunders Jul 5 '13 at 18:17
add comment

Your Answer


