Tuesday, July 12, 2005

C# Send mail, send web page by email

using System;
using System.Web.Mail;

namespace NEWS_RANK.util.net
{

public class Mail
{
private static string smtpserver = "127.0.0.1";


public static void sendMail(string To, string From, string Subject, string Body)
{
try
{
//-----Mail properties----
MailMessage mMessage = new MailMessage();
mMessage.From = From;
mMessage.To = To;
mMessage.Subject = Subject;
mMessage.Body = Body;
mMessage.BodyFormat = MailFormat.Html;
//-----Mail Send-----
SmtpMail.SmtpServer = smtpserver;
SmtpMail.Send(mMessage);
}
catch(Exception exp)
{
throw exp;
}
}


public static void sendMailHttpUrl(string To, string From, string Subject, string HttpUrl,string Query,string HttpMethod)
{
try
{
//-----Mail properties------
MailMessage mMessage = new MailMessage();
mMessage.From = From;
mMessage.To = To;
mMessage.Subject = Subject;
string mailContent = "";
if( HttpMethod != null && HttpMethod.ToLower().Equals("post") )
{
Uri baseUri = new Uri(HttpUrl);
string path = baseUri.GetLeftPart(System.UriPartial.Path);
string param = "";
if( Query != null && Query.Length > 1 )
param = Query;
mailContent = HttpUtil.HttpPOSTConnection(path,param);
}
else mailContent = HttpUtil.HttpConnection(HttpUrl);
mMessage.Body = mailContent;
mMessage.BodyFormat = MailFormat.Html;
//-----Mail Send-----
SmtpMail.SmtpServer = smtpserver;
SmtpMail.Send(mMessage);
}
catch(Exception exp)
{
throw exp;
}
}
}
}

No comments: