Saturday, March 7, 2009

Sending Email Through ASP.NET using C#

The following Sample Code is the code which can be used to send mails through ASP.Net C#.... its a working code for me.....

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void Button1_Click(object sender, EventArgs e)
//Calling the function SendMail
Response.Write( SendMail("","","","Test Mail","Test Mail Body"));

public string SendMail(string toList, string from, string ccList, string subject, string body)
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
if (ccList != null && ccList != string.Empty)
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
smtpClient.Host = "";
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential("", "password");

msg = "Successful";
catch (Exception ex)
msg = ex.Message;
return msg;


Thanks and Regards
Meetu Choudhary


  1. Dear meetu,
    This is code is working fine.
    But i am able to send email to our network when ever i am trying to send mail to outside network it is giving error message

    "System.Net.Mail.SmtpFailedRecipientsException: Unable to send to all recipients. ---> System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 5.7.1 Unable to relay
    --- End of inner exception stack trace ---
    at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
    at System.Net.Mail.SmtpClient.Send(MailMessage message)
    at _Default.SendMail(String toList, String from, String ccList, String subject, String body) in c:\Documents and Settings\SUNIL\My Documents\Visual Studio 2005\WebSites\SendMail\Default.aspx.cs:line 68}

    My code is here


    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
    mail.From = new MailAddress("Mycompanydomain", "TEST MAIL");
    mail.Subject = Subject.Text;
    string Body = Bodymsg.Text;
    mail.Body = Body;
    mail.IsBodyHtml = false;
    SmtpClient smtp = new SmtpClient("mailserver", 587);
    smtp.Credentials = new System.Net.NetworkCredential("userID", "password");
    smtp.EnableSsl = true;
    lblTxt.Text = "Email successfully sent.";
    catch (Exception ex)
    lblTxt.Text = "Send Email Failed. " + ex.Message;

  2. Hello Meetu,

    Thanks for your post. It help me a lot. but i have to make an interesting functionality in my project. I have to send attachment to the person whom i want to mail. but have to provide security when the user clicked on attachment. before this it asked about password. if password is correct then only file has been open.

    I have searched it on google. but i din't get.

    Thanks in advance

  3. Hi ,
    sending emails i got this error.could you help me please.

    Server Error in '/SendEmail' Application.

    The resource cannot be found.

    Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

    Requested URL: /SendEmail/query_submitted.aspx

    Thank you,

  4. Hi Siva Kumarui..
    This is not the error of the mail. its like in your application the page url you specified is not the correct one it is not redirecting to the url specified by you the reason may be the page does not exist on that location

  5. Hi Meetu Choudhary,

    Thank you very much ,i would try again. I got this error Failure sending email.

    WCF how to starting could you help me step wise.

    Thank you.

  6. Hello Siva Kumari
    sure I will Help You Can you tell me The excerpt of what you are doing so that i may help you in rectifying the error

  7. Hello Siva Kumari
    check this article also may be of some help

  8. Hello Meetu Choudhary,

    Thank you very much for helping.just I got the .net training i am looking for the job i don't know WCF and XSLT in interview they are
    asking could you help me.

    Thank you.

  9. Hello Siva Kumari

    Check these sites for preparation.
    and there

  10. Hi Meetu Choudhary,

    Could you send the link for me about TFS.How to create.How to use in real project.

    Thank you ,

  11. Check this may be helpful. this is mostly implemented in the office itself.

  12. Dear Madam,

    i just refer your code for sending Email
    But i can't do that.

    from my application i want to send mail and my ISP at remote place i have all credentials information but still i am unable to send it.
    Can you help me out?

    Best Regards,
    Anup Shah

  13. Hi Mr Shah !

    it would be good if you can tell me what exact error you are getting. As ISP being at remote does not affect the process of sending mail as in the above example i have used my gmail account. the problem can be the port no. or it is allowed or open on your computer

  14. hi mam,
    can u help me to study on asp dot net using c#,any sites or can u teach me this please............i'm waiting 4 ur reply mam.................

  15. Hi Priya sure you can learn form
    and if you have any queries then you can ask them in my forums as well


  16. i have got this below error.Please help me

    Failure sending mail.


Subscribe via email

Enter your email address:

Delivered by FeedBurner


MSDotnetMentor My Website