dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersShine S
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » Email

Send Email From C#


Posted Date:     Category: Email    
Author: Member Level: Gold    Points: 5



 


Now throw this junk of code we can able to send email. Its a method you can call this method in any event

We take host name from web.config you can find the code below

private void SendMail()
{

//Create message object and populate with the data from form
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new System.Net.Mail.MailAddress(txtFrom.Text.Trim());
message.To.Add(txtTo.Text.Trim());
message.Subject = txtSubject.Text.Trim();
message.Body = txtBody.Text.Trim();

//Setup SmtpClient to send email. Uses web.config settings.
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();

//Error handling for sending message
try
{
smtpClient.Send(message);
//Exception contains information on each failed receipient
}
catch (System.Net.Mail.SmtpFailedRecipientsException recExc)
{
for (int recipient = 0; recipient < recExc.InnerExceptions.Length - 1; recipient++)
{
System.Net.Mail.SmtpStatusCode statusCode;

statusCode = recExc.InnerExceptions[recipient].StatusCode;

if ((statusCode == System.Net.Mail.SmtpStatusCode.MailboxBusy) || (statusCode == System.Net.Mail.SmtpStatusCode.MailboxUnavailable))
{

System.Threading.Thread.Sleep(5000);
smtpClient.Send(message);
}
else
{
ErrorLabel.Text = recExc.Message;


}

}
}
//General SMTP execptions
catch (System.Net.Mail.SmtpException smtpExc)
{

ErrorLabel.Text = smtpExc.StatusCode.ToString();
}
catch (Exception ex)
{
//Log error to event log.
}
}

Configuring web.config file

< system.net>
< mailSettings >
< smtp >
< network host="YOUR HOST INFO HERE"/ >
< /smtp >
< /mailSettings >
< /system.net >


Cheers,
Feel free to send your opinion.





Did you like this resource? Share it with your friends and show your love!


Responses to "Send Email From C#"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Easy Way to send mail through ASP.Net & C#.Net
    Previous Resource: Send email using smtp object in c#
    Return to Resources
    Post New Resource
    Category: Email


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Email with Image  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.