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.....

[code]
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.
WebParts;
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("meetuchoudhary@gmail.com","meetudmeet@gmail.com","meetudmeet@yahoo.com","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;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
if (ccList != null && ccList != string.Empty)
message.CC.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
smtpClient.Host = "mail.server.com";
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential("info@server.com", "password");

smtpClient.Send(message);
msg = "Successful";
}
catch (Exception ex)
{
msg = ex.Message;
}
return msg;
}

}
[/code]


--
Thanks and Regards
Meetu Choudhary

16 comments:

  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

    try
    {

    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
    mail.To.Add("SenderEmailAddress");
    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;
    smtp.Send(mail);
    lblTxt.Text = "Email successfully sent.";
    }
    catch (Exception ex)
    {
    lblTxt.Text = "Send Email Failed. " + ex.Message;
    }

    ReplyDelete
  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
    Omkar.

    ReplyDelete
  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,
    Kumari.

    ReplyDelete
  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

    ReplyDelete
  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.

    ReplyDelete
  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

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

    http://aspnetbymeetu.blogspot.com/2009/09/uri-format-not-supported.html

    ReplyDelete
  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.

    ReplyDelete
  9. Hello Siva Kumari

    Check these sites for preparation.

    http://www.msdotnetmentor.com/
    and there
    http://interview.msdotnetheaven.com/

    ReplyDelete
  10. Hi Meetu Choudhary,

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

    Thank you ,
    Kumari.

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

    http://msdn.microsoft.com/en-us/vstudio/ff637362.aspx

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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.................

    ReplyDelete
  15. Hi Priya sure you can learn form http://www.asp.net/web-pages
    and if you have any queries then you can ask them in my forums as well http://www.jaipurmentor.com/jaipurmentor-forum.html

    Thanks

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

    Failure sending mail.

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner

MSDotnetMentor

MSDotnetMentor My Website http://msdotnetmentor.com