.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Email >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

sending mails

Posted By:nishithraj       Posted Date: December 15, 2009    Points: 10    Category: Email    URL: http://www.dotnetspark.com  
 

A simple code example which elaborates the smtp mailing concept.

The mails sent from the smtp server should be retrieved in the beginning before sending the mail.

public static bool SendMessage(MailMessage message)

{

try

{

SiteSettings settings = SiteSettings.GetSiteSettings();

message.Body = message.Body.Replace("\r", string.Empty);

message.Body = message.Body.Replace("\n", "\r\n");

message.SubjectEncoding = Encoding.GetEncoding(settings.EmailSubjectEncoding);

message.BodyEncoding = Encoding.GetEncoding(settings.EmailEncoding);

message.Headers["Content-Base"] = settings.SiteUrl;

message.Headers["Content-Location"] = settings.SiteUrl;

if (message.From == null)

message.From = new MailAddress(settings.AdminEmailAddress);

SmtpClientName().Send(message);

return true;

}

catch (Exception e)

{

Log.Exception(e);

return false;

}

}

Method Extension

private static SmtpClient SmtpClientName()

{

if (smtpClient == null)

{

string smtpsettings = configuratonManager.Appsettings[""]; SiteSettings settings = SiteSettings.GetSiteSettings();

if (!string.IsNullOrEmpty(settings.SmtpPortNumber))

{

try

{

int port = int.Parse(settings.SmtpPortNumber);

smtpClient = new SmtpClient(settings.SmtpServer, port);

}

catch

{

smtpClient = new SmtpClient(settings.SmtpServer);

}

}

else

smtpClient = new SmtpClient(settings.SmtpServer);

}

}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend