1。通用部分
2.调用部分
using System;
using System.Web.Mail;
namespace BusinessServer.DataServer
{
/// <summary>
/// Mail 的摘要说明。
/// </summary>
public class EMail
{
public const string C_strSenderName = "******";
public const string C_strsendpassword = "******";
public const string C_strSmtpServer = "smtp.***.com";
public const string C_strFrom = "******@***.com";
public EMail()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static bool Send(string strTo, string strFrom, string strSubject, string strMessage)
{
try
{
MailMessage mailObj = new MailMessage();
// 设置email的'from'和'to'的地址
mailObj.From = C_strFrom;
mailObj.To = strTo;
mailObj.Subject = strSubject;
mailObj.Body = strMessage;
// 可选: 使用html格式的Email
mailObj.BodyFormat = MailFormat.Html;
// 可选: 对邮件进行加密
// mailObj.BodyEncoding = MailFormat.Base64;
// 可选: 设置邮件的优先级别为高
mailObj.Priority = MailPriority.High;
mailObj.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
mailObj.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",C_strSenderName); //set your username here
mailObj.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", C_strsendpassword); //set your password here
// 使用SmtpMail对象来发送邮件。
SmtpMail.SmtpServer = C_strSmtpServer;
SmtpMail.Send(mailObj);
}
catch(Exception ee)
{
System.Diagnostics.Debug.Write(ee.ToString());
return false;
}
return true;
}
}
}
using System.Web.Mail;
namespace BusinessServer.DataServer
{
/// <summary>
/// Mail 的摘要说明。
/// </summary>
public class EMail
{
public const string C_strSenderName = "******";
public const string C_strsendpassword = "******";
public const string C_strSmtpServer = "smtp.***.com";
public const string C_strFrom = "******@***.com";
public EMail()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static bool Send(string strTo, string strFrom, string strSubject, string strMessage)
{
try
{
MailMessage mailObj = new MailMessage();
// 设置email的'from'和'to'的地址
mailObj.From = C_strFrom;
mailObj.To = strTo;
mailObj.Subject = strSubject;
mailObj.Body = strMessage;
// 可选: 使用html格式的Email
mailObj.BodyFormat = MailFormat.Html;
// 可选: 对邮件进行加密
// mailObj.BodyEncoding = MailFormat.Base64;
// 可选: 设置邮件的优先级别为高
mailObj.Priority = MailPriority.High;
mailObj.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
mailObj.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",C_strSenderName); //set your username here
mailObj.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", C_strsendpassword); //set your password here
// 使用SmtpMail对象来发送邮件。
SmtpMail.SmtpServer = C_strSmtpServer;
SmtpMail.Send(mailObj);
}
catch(Exception ee)
{
System.Diagnostics.Debug.Write(ee.ToString());
return false;
}
return true;
}
}
}
2.调用部分
private bool GetUserNameMenuName(ref string strUsers,ref string strMenuNames)
{
string strSQLUserName = string.Format("Select UserName FROM TBL_AccessUser Where RoleID = {0} ",H_RoleID);
string strSQLMenu = GetMenuSQL(H_AccessXml);
string strSQL = string.Format("{0} ; {1} ",strSQLUserName,strSQLMenu);
DataSet ds = DataProvider.RunSelect(strSQL);
if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count == 0 )
return false;
foreach(DataRow dr in ds.Tables[0].Rows)
{
if(strUsers != "")
strUsers += ",";
strUsers += dr["UserName"].ToString().Trim();
}
int icount = 1;
foreach(DataRow dr in ds.Tables[1].Rows)
{
string strMenuName = dr["MenuName"].ToString().Trim();
if (strMenuName != "")
{
if (icount % 5 == 1)
strMenuNames += string.Format("<TR><TD>{0}</TD><TD>",strMenuName); //头
else if (icount % 5 == 0)
strMenuNames += string.Format("{0}</TD></TR>",strMenuName); //尾
else
strMenuNames += string.Format("{0}</TD><TD>",strMenuName); //中间
icount ++;
}
}
return strUsers.Trim() != "";
}
private string GetMessage()
{
StringBuilder sbEmailMsg = new StringBuilder("");
string strUser = "";
string strMenuNames = "";
if (GetUserNameMenuName(ref strUser,ref strMenuNames))
{
sbEmailMsg.Append("<TABLE width='600' bgColor='#e0ecff'><caption>");
sbEmailMsg.Append("<caption style='FONT-SIZE: 20pt;COLOR: #f7f7f7; PADDING-TOP: 5px; BACKGROUND-COLOR: #4a3c8c'>权限角色修改记录</caption>");
sbEmailMsg.Append("<TR><TD width='20%'></TD><TD width='20%'></TD><TD width='20%'></TD><TD width='20%'></TD><TD width='20%'></TD></TR>");
sbEmailMsg.Append(string.Format("<TR><TD ColSpan=5>{0}的权限有变动,新权限为:</TD></TR>{1}</TABLE>",strUser,strMenuNames));
}
return sbEmailMsg.ToString();
}
private bool DoSendMail()
{
string strSubject = "角色修改";
string strMessage = GetMessage();
return EmailCommonFun.EmailSend(strSubject,strMessage);
}
{
string strSQLUserName = string.Format("Select UserName FROM TBL_AccessUser Where RoleID = {0} ",H_RoleID);
string strSQLMenu = GetMenuSQL(H_AccessXml);
string strSQL = string.Format("{0} ; {1} ",strSQLUserName,strSQLMenu);
DataSet ds = DataProvider.RunSelect(strSQL);
if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count == 0 )
return false;
foreach(DataRow dr in ds.Tables[0].Rows)
{
if(strUsers != "")
strUsers += ",";
strUsers += dr["UserName"].ToString().Trim();
}
int icount = 1;
foreach(DataRow dr in ds.Tables[1].Rows)
{
string strMenuName = dr["MenuName"].ToString().Trim();
if (strMenuName != "")
{
if (icount % 5 == 1)
strMenuNames += string.Format("<TR><TD>{0}</TD><TD>",strMenuName); //头
else if (icount % 5 == 0)
strMenuNames += string.Format("{0}</TD></TR>",strMenuName); //尾
else
strMenuNames += string.Format("{0}</TD><TD>",strMenuName); //中间
icount ++;
}
}
return strUsers.Trim() != "";
}
private string GetMessage()
{
StringBuilder sbEmailMsg = new StringBuilder("");
string strUser = "";
string strMenuNames = "";
if (GetUserNameMenuName(ref strUser,ref strMenuNames))
{
sbEmailMsg.Append("<TABLE width='600' bgColor='#e0ecff'><caption>");
sbEmailMsg.Append("<caption style='FONT-SIZE: 20pt;COLOR: #f7f7f7; PADDING-TOP: 5px; BACKGROUND-COLOR: #4a3c8c'>权限角色修改记录</caption>");
sbEmailMsg.Append("<TR><TD width='20%'></TD><TD width='20%'></TD><TD width='20%'></TD><TD width='20%'></TD><TD width='20%'></TD></TR>");
sbEmailMsg.Append(string.Format("<TR><TD ColSpan=5>{0}的权限有变动,新权限为:</TD></TR>{1}</TABLE>",strUser,strMenuNames));
}
return sbEmailMsg.ToString();
}
private bool DoSendMail()
{
string strSubject = "角色修改";
string strMessage = GetMessage();
return EmailCommonFun.EmailSend(strSubject,strMessage);
}