Sending Email via SMTP Server
it's very easy to send mail via SMTP.First create static class, static class name
MailtUtility. In that create static method GetConfiguredSmtpClient() return type smtpClient.
public static class MailUtility
{
Initializes the SMTP client that will be used by all mail functions.
Call this function to get the default SMTP Client based on the Configuration Settings
public static SmtpClient GetConfiguredSmtpClient()
{
try
{
return GetConfiguredSmtpClient(null, 0, null, null);
}
catch (Exception oEx)
{
return null;
}
}
Call this function to get a smtp client that is setup using the values passed as
arguments.
Pass a null value for smtpServer to fallback to the configured value for a parameter.
Parameters:
Hostname of the SMTP server
Port
Username used for authentication
Password used for authentication
public static SmtpClient GetConfiguredSmtpClient(string smtpServer, int smtpPort, string userName, string password)
{
SmtpClient smtp = new SmtpClient();
try
{
if (!string.IsNullOrEmpty(smtpServer))
{
smtp.Port = smtpPort;
smtp.Host = smtpServer;
smtp.Credentials = new NetworkCredential(userName, password);
}
return smtp;
}
catch (Exception oEx)
{
return null;
}
finally
{
smtp = null;
}
}
Sending Emails
These are parameters
From Address
To Addresses
Subject
Mail Body
Return type:True on success, False on failure
public static bool SendEmail(string mailFrom, string mailTo, string mailSubject, string mailBody)
{
try
{
return SendEmail(mailFrom, mailTo, mailSubject, mailBody,null,null,true);
}
catch (Exception oEx)
{
return false;
}
}
If the reply to address is different from the From Address,you can use this method.
Return Type:True on success, False on failure
public static bool SendEmail(string mailFrom, string mailTo, string mailSubject, string mailBody, string mailCopyTo, string replyTo)
{
try
{
return SendEmail(mailFrom,mailTo,mailSubject,mailBody,mailCopyTo,replyTo,true);
}
catch (Exception oEx)
{
return false;
}
}
Here actual sendmail method
private static bool SendEmail(string mailFrom, string mailTo, string mailSubject, string mailBody, string mailCopyTo, string replyTo, bool isHtmlBody)
{
MailMessage oMailMsg = new MailMessage();
SmtpClient oSmtp;
try
{
oMailMsg.IsBodyHtml = isHtmlBody;
// Add From Address to mail
oMailMsg.From = new MailAddress(mailFrom);
if(!string.IsNullOrEmpty(mailCopyTo))
oMailMsg.CC.Add(mailCopyTo);
// Add reply to Mail
oMailMsg.To.Add(mailTo);
if (!string.IsNullOrEmpty(replyTo))
oMailMsg.ReplyTo = new MailAddress(replyTo);
oMailMsg.Subject = mailSubject;
oMailMsg.Body = mailBody;
oSmtp = GetConfiguredSmtpClient();
if (oSmtp != null)
oSmtp.Send(oMailMsg);
return true;
}
catch(Exception oEx)
{
return false;
}
finally
{
oMailMsg.Dispose();
oMailMsg = null;
oSmtp = null;
}
}
}
Mailutility class Ends here.
Calling MailUtility class
private bool SendMyMail(string strAdminEmailId, string userEmail, string strEmailSubject, string emailContent)
{
bool bIsSend = false;
try
{
if (!emailContent.Equals(string.Empty))
{
bIsSend = MailUtility.SendEmail(strAdminEmailId, userEmail, strEmailSubject, emailContent, true);
}
return bIsSend;
}
catch (Exception ex)
{
return bIsSend;
}
}
Your web.config file enties in <system.web> tag
<mailSettings>
<!--<smtp deliveryMethod="PickupDirectoryFromIis" />-->
<!-- Common usage is to use other network existing mail server other than
IIS Virtual SMTP Server.
In such cases use the commented settings below with your server
location and authentication credentials.
-->
<smtp from="admin@abc.com" deliveryMethod="Network">
<network host="100.0.0.0" port="25" userName="" password="" defaultCredentials="false" />
</smtp>