C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Email »

Send Email From C#


Posted Date: 12 Jun 2009    Resource Type: Code Snippets    Category: Email
Author: DotNetMatrixMember Level: Gold    
Rating: 1 out of 5Points: 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.



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Email with Image  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Sending Email to multiple users using VB.net
Previous Resource: Send email using smtp object in c#
Return to Discussion Resource Index
Post New Resource
Category: Email


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use