Powered By Blogger

Wednesday, December 30, 2009

send mail in asp.net c#

web.config










aspx.cs

string ToMail, FromMail, MailBody, host, portNo, password;

page load:

FromMail = ConfigurationManager.AppSettings["mailFrom"];
password = ConfigurationManager.AppSettings["password"];
host = ConfigurationManager.AppSettings["host"];
portNo = ConfigurationManager.AppSettings["port"];
ToMail = Request.QueryString[1].ToString();
MailBody="test mail";

button click


MailMessage mailMessage = new MailMessage(FromMail, ToMail, "Hello ! " + ToMail + ", Mr/Mrs" + ToMail + "Has Replied you", MailBody);
mailMessage.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = false;
NetworkCredential networkCredential = new NetworkCredential(FromMail, password);
smtpClient.Host = host;
mailMessage.Priority = MailPriority.High;
if (!string.IsNullOrEmpty(portNo))
{
smtpClient.Port = Convert.ToInt32(portNo);
smtpClient.EnableSsl = true;
}
smtpClient.Credentials = networkCredential;
smtpClient.Send(mailMessage);


------------------------------------------------------------------------------------------



using System.Net;
using System.Net.Mail;
using System.Web.Configuration;

Method I
-----------

System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
System.Net.NetworkCredential credential = new System.Net.NetworkCredential(settings.Smtp.Network.UserName, settings.Smtp.Network.Password);

int i = 0;
//Create the SMTP Client
SmtpClient client = new SmtpClient();
client.Host = settings.Smtp.Network.Host;
client.Credentials = credential;
HttpResponse response = HttpContext.Current.Response;
MailMessage email = new MailMessage();

email.IsBodyHtml = true;
email.From = new MailAddress(FromMail);
email.To.Add("ranjith@isolve.co.in");
// email.To.Add(ToMail);

//email.To.Add(strToEmail)
email.Subject = SubjecT;
email.IsBodyHtml = true;
email.Body = MailBody;
email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

try
{
client.Send(email);
}
catch (Exception exc)
{
response.Write("Send failure: " + exc.ToString());
}


WEB.CONFIG
============


<appSettings>
<add key="SenderUsername" value="xx@gmail.com"/>
<add key="SenderPassword" value="xx"/>
<add key="HostIP" value="***.***.***.***"/>

</appSettings>





============================================================

Method II
-------------

public void SendMail(string FrmMail, string ToMail, string MailBody, string SubjecT)
{
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
System.Net.NetworkCredential credential = new System.Net.NetworkCredential(settings.Smtp.Network.UserName, settings.Smtp.Network.Password);

int i = 0;
//Create the SMTP Client
SmtpClient client = new SmtpClient();
client.Host = settings.Smtp.Network.Host;
client.Credentials = credential;
HttpResponse response = HttpContext.Current.Response;
MailMessage email = new MailMessage();



email.IsBodyHtml = true;
email.From = new MailAddress(FrmMail);
email.To.Add(ToMail);
//email.To.Add(ToMail);

//email.To.Add(strToEmail)
email.Subject = SubjecT;
email.IsBodyHtml = true;
email.Body = MailBody;
email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

try
{
client.Send(email);

}
catch (Exception exc)
{
response.Write("Send failure: " + exc.ToString());

}
}


*FIREWALL SHOULD BE OFF

-------------------------------------------------------------------

No comments:

Post a Comment