Sunday, May 10, 2009

Send Mail Using ASP.net and C Sharp

This class is useful to send email using ASP.NET and C-Sharp.


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public class _Mail
{
string host = "xxx.xxxx.com";
string userid = "xxx@xxxx.com";
string password = "*****";
string from = "xxx@xxxxx.com";

string to = null;
string subject = null;
string msg = null;



public _Mail(String to,String from,String userid,String password,String subject,String msg)
{
this.to = to;
if (userid == null && password == null)
{
this.userid = this.userid;
this.password = this.password;
}
else
{
this.userid = userid;
this.password = password;
}
if (from == null)
{
this.from = this.from;
}
else
{
this.from = from;
}
this.subject = subject;
this.msg = msg;
}
public void SendMail()
{
System.Net.Mail.MailMessage email = null;
email = new MailMessage(this.from,this.to);
email.Subject = this.subject;
email.Body = this.msg;
SmtpClient mailClient = null;
mailClient = new SmtpClient();
System.Net.NetworkCredential basicAuthenticationInfo = null;
basicAuthenticationInfo = new System.Net.NetworkCredential(this.userid,this.password);
mailClient.Host = this.host;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = basicAuthenticationInfo;
mailClient.Send(email);
}
}

No comments:

Post a Comment