First create one class File with name MailHelper.cs
Place the code
public class MailHelper
{
private const int Timeout = 180000;
private readonly string _host;
private readonly int _port;
private readonly string _user;
private readonly string _pass;
private readonly bool _ssl;
public string Sender { get; set; }
public string Recipient { get; set; }
public string RecipientCC { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public string AttachmentFile { get; set; }
public MailHelper()
{
_host = ConfigurationManager.AppSettings["MailServer"];
_port = int.Parse(ConfigurationManager.AppSettings["Port"]);
_user = ConfigurationManager.AppSettings["MailAuthUser"];
_pass = ConfigurationManager.AppSettings["MailAuthPass"];
_ssl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSSL"]);
}
public void Send()
{
try
{
// We do not catch the error here... let it pass direct to the caller
Attachment att = null;
var message = new MailMessage(Sender, Recipient, Subject, Body) { IsBodyHtml = true };
if (RecipientCC != null)
{
message.Bcc.Add(RecipientCC);
}
var smtp = new SmtpClient(_host, _port);
if (!String.IsNullOrEmpty(AttachmentFile))
{
if (File.Exists(AttachmentFile))
{
att = new Attachment(AttachmentFile);
message.Attachments.Add(att);
}
}
if (_user.Length > 0 && _pass.Length > 0)
{
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(_user, _pass);
smtp.EnableSsl = _ssl;
}
smtp.Send(message);
if (att != null)
att.Dispose();
message.Dispose();
smtp.Dispose();
}
catch (Exception ex)
{
}
}
}
Then place these line of code in Web..config file in appsettings
<appSettings>
<add key="MailServer" value="smtp.gmail.com"/>
<add key="Port" value="587"/>
<add key="EnableSSL" value="true"/>
<add key="EmailFromAddress" value="xxx@gmail.com"/>
<add key="MailAuthUser" value="xxx@gmail.com"/>
<add key="MailAuthPass" value="xxxxx"/>
</appSettings>
then For example forgot password
SendMail();//Method
private void SendMail()
{
try
{
string body;
//place the text file in the appcode -> templates-> ForgotPass.txt
using (var sr = new StreamReader(Server.MapPath("App_Data\\Templates\\") + "ForgotPass.txt"))
{
body = sr.ReadToEnd();
}
string name = Session["Name"].ToString();
string username = EmailIdTextBox.Text;
string password = Session["Password"].ToString();
string subject = "Your Password";
string emailBody = String.Format(body, name, username, password);
string sender = ConfigurationManager.AppSettings["EmailFromAddress"].ToString();
var mailer = new MailHelper
{
Sender = sender, //email.Sender,
Recipient = EmailIdTextBox.Text,
Subject = subject,
Body = emailBody,
};
mailer.Send();
}
catch (Exception ex)
{
}
}
Place the code
public class MailHelper
{
private const int Timeout = 180000;
private readonly string _host;
private readonly int _port;
private readonly string _user;
private readonly string _pass;
private readonly bool _ssl;
public string Sender { get; set; }
public string Recipient { get; set; }
public string RecipientCC { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public string AttachmentFile { get; set; }
public MailHelper()
{
_host = ConfigurationManager.AppSettings["MailServer"];
_port = int.Parse(ConfigurationManager.AppSettings["Port"]);
_user = ConfigurationManager.AppSettings["MailAuthUser"];
_pass = ConfigurationManager.AppSettings["MailAuthPass"];
_ssl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSSL"]);
}
public void Send()
{
try
{
// We do not catch the error here... let it pass direct to the caller
Attachment att = null;
var message = new MailMessage(Sender, Recipient, Subject, Body) { IsBodyHtml = true };
if (RecipientCC != null)
{
message.Bcc.Add(RecipientCC);
}
var smtp = new SmtpClient(_host, _port);
if (!String.IsNullOrEmpty(AttachmentFile))
{
if (File.Exists(AttachmentFile))
{
att = new Attachment(AttachmentFile);
message.Attachments.Add(att);
}
}
if (_user.Length > 0 && _pass.Length > 0)
{
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(_user, _pass);
smtp.EnableSsl = _ssl;
}
smtp.Send(message);
if (att != null)
att.Dispose();
message.Dispose();
smtp.Dispose();
}
catch (Exception ex)
{
}
}
}
Then place these line of code in Web..config file in appsettings
<appSettings>
<add key="MailServer" value="smtp.gmail.com"/>
<add key="Port" value="587"/>
<add key="EnableSSL" value="true"/>
<add key="EmailFromAddress" value="xxx@gmail.com"/>
<add key="MailAuthUser" value="xxx@gmail.com"/>
<add key="MailAuthPass" value="xxxxx"/>
</appSettings>
then For example forgot password
SendMail();//Method
private void SendMail()
{
try
{
string body;
//place the text file in the appcode -> templates-> ForgotPass.txt
using (var sr = new StreamReader(Server.MapPath("App_Data\\Templates\\") + "ForgotPass.txt"))
{
body = sr.ReadToEnd();
}
string name = Session["Name"].ToString();
string username = EmailIdTextBox.Text;
string password = Session["Password"].ToString();
string subject = "Your Password";
string emailBody = String.Format(body, name, username, password);
string sender = ConfigurationManager.AppSettings["EmailFromAddress"].ToString();
var mailer = new MailHelper
{
Sender = sender, //email.Sender,
Recipient = EmailIdTextBox.Text,
Subject = subject,
Body = emailBody,
};
mailer.Send();
}
catch (Exception ex)
{
}
}

0 comments:
Post a Comment