实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
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.Data.SqlClient;
using System.Net.Mail;
public class WebMailProfile
{
public string UserName;
public string AliasName;
public string Email;
public string MailServerIP;
public int MailServerPort;
}
public interface IMail
{
/// <summary>
/// 获取系统配置信息
/// </summary>
/// <returns></returns>
SqlDataReader GetWebMailProfile();
/// <summary>
/// 修改系统的配置信息
/// </summary>
/// <param name="sUserName"></param>
/// <param name="sAliasName"></param>
/// <param name="sEmail"></param>
/// <param name="sMailServerIP"></param>
/// <param name="nMailServerPort"></param>
/// <returns></returns>
int WebMailProfile(string sUserName,string sAliasName,string sEmail,string sMailServerIP,
int nMailServerPort);
/// <summary>
/// 获取所有邮件
/// </summary>
/// <returns></returns>
SqlDataReader GetMails();
/// <summary>
/// 获取某个邮箱的邮件
/// </summary>
/// <param name="nFolderID"></param>
/// <returns></returns>
SqlDataReader GetMailsByFloder(int nFolderID);
/// <summary>
/// 获取单个邮件的记录
/// </summary>
/// <param name="nMailID"></param>
/// <returns></returns>
SqlDataReader GetSingleMail(int nMailID);
/// <summary>
/// 发送邮件
/// </summary>
/// <returns></returns>
int SenderMail(MailMessage mail);
/// <summary>
/// 添加发送的邮件到邮件箱中
/// </summary>
/// <param name="sName"></param>
/// <param name="sBody"></param>
/// <param name="sFrom"></param>
/// <param name="sTo"></param>
/// <param name="sCC"></param>
/// <param name="bHtmlFormat"></param>
/// <param name="nContain"></param>
/// <param name="bAttachmentFlag"></param>
/// <returns></returns>
int SaveAsMail(string sName,string sBody,string sFrom,string sTo,
string sCC,bool bHtmlFormat,int nContain,bool bAttachmentFlag);
/// <summary>
/// 添加邮件的附件
/// </summary>
/// <param name="sName"></param>
/// <param name="sUrl"></param>
/// <param name="sType"></param>
/// <param name="nContain"></param>
/// <param name="MailID"></param>
/// <returns></returns>
int SaveAsMailAttachment(string sName,string sUrl,string sType,
int nContain,int nMailID);
/// <summary>
/// 移动邮件
/// </summary>
/// <param name="nMailID"></param>
/// <param name="nFolderID"></param>
/// <returns></returns>
int MoveMail(int nMailID,int nFolderID);
/// <summary>
/// 删除邮件
/// </summary>
/// <param name="nMailID"></param>
/// <returns></returns>
int DeleteMail(int nMailID);
/// <summary>
/// 获取邮件的附件
/// </summary>
/// <param name="nMailID"></param>
/// <returns></returns>
SqlDataReader GetAttachmentsByMail(int nMailID);
}
/// <summary>
/// Mail 的摘要说明
/// </summary>
public class Mail:IMail
{
#region IMail 成员
public SqlDataReader GetWebMailProfile()
{
///创建链接
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
///定义SQL语句
string cmdText = "SELECT * FROM WebMailProfile WHERE WebMailID = 1";
///创建Command
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
///定义DataReader
SqlDataReader dr = null;
try
{
///打开链接
myConnection.Open();
///读取数据
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(SqlException ex)
{
///抛出异常
throw new Exception(ex.Message,ex);
}
///返回DataReader
return dr;
}
public int WebMailProfile(string sUserName,string sAliasName,string sEmail,string sMailServerIP,
int nMailServerPort)
{
///创建链接
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
///定义SQL语句
string cmdText = "UPDATE WebMailProfile SET"
" UserName='" sUserName "',"
" AliasName='" sAliasName "',"
" Email='" sEmail "',"
" MailServerIP='" sMailServerIP "',"
" MailServerPort='" nMailServerPort.ToString() "'"
" WHERE WebMailID=1";
///创建Command
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
///定义返回值
int nResult = -1;
try
{
///打开链接
myConnection.Open();
///执行SQL语句
nResult = myCommand.ExecuteNonQuery();
}
catch(SqlException ex)
{
///抛出异常
throw new Exception(ex.Message,ex);
}
finally
{ ///关闭链接
myConnection.Close();
}
///返回nResult
return nResult;
}
public SqlDataReader GetMails()
{
///创建链接
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
///定义SQL语句
string cmdText = "SELECT * FROM Mails";
///创建Command
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
///定义DataReader
SqlDataReader dr = null;
try
{
///打开链接
myConnection.Open();
///读取数据
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(SqlException ex)
{
///抛出异常
throw new Exception(ex.Message,ex);
}
///返回DataReader
return dr;
}
public SqlDataReader GetMailsByFloder(int nFolderID)
{
///创建链接
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
///定义SQL语句
string cmdText = "SELECT Mails.* FROM Mails WHERE FolderID='" nFolderID.ToString() "'";
///创建Command
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
///定义DataReader
SqlDataReader dr = null;
try
{
///打开链接
myConnection.Open();
///读取数据
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(SqlException ex)
{
///抛出异常
throw new Exception(ex.Message,ex);
}
///返回DataReader
return dr;
}
public SqlDataReader GetSingleMail(int nMailID)
{
///创建链接
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
///定义SQL语句
string cmdText = "SELECT * FROM Mails WHERE MailID='" nMailID.ToString() "'";
///创建Command
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
///定义DataReader
SqlDataReader dr = null;
try
{
///打开链接
myConnection.Open();
///读取数据
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(SqlException ex)
{
///抛出异常
throw new Exception(ex.Message,ex);
}
///返回DataReader
return dr;
}
public int SenderMail(MailMessage mail)
{
///定义发送邮件的Client
SmtpClient client = new SmtpClient();
///设置邮件服务器主机的IP地址
client.Host = ((WebMailProfile)HttpContext.Current.Application["WebMailProfile"]).MailServerIP;
///设置邮件服务器的端口
client.Port = ((WebMailProfile)HttpContext.Current.Application["WebMailProfile"]).MailServerPort;
///配置发送邮件的属性
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
///发送邮件
client.Send(mail);
return (0);
}
public int SaveAsMail(string sTitle,string sBody,string sFrom,string sTo,
string sCC,bool bHtmlFormat,int nContain,bool bAttachmentFlag)
{
///创建链接
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
SqlCommand myCommand = new SqlCommand("Pr_SaveAsMail",myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
///添加存储过程的参数
SqlParameter pTitle = new SqlParameter("@Title",SqlDbType.VarChar,200);
pTitle.Value = sTitle;
myCommand.Parameters.Add(pTitle);
SqlParameter pBody = new SqlParameter("@Body",SqlDbType.Text,2147483647);
pBody.Value = sBody;
myCommand.Parameters.Add(pBody);
SqlParameter pFrom = new SqlParameter("@FromAddress",SqlDbType.Text,2147483647);
pFrom.Value = sFrom;
myCommand.Parameters.Add(pFrom);
SqlParameter pTo = new SqlParameter("@ToAddress",SqlDbType.Text,2147483647);
pTo.Value = sTo;
myCommand.Parameters.Add(pTo);
SqlParameter pCC = new SqlParameter("@CCAddress",SqlDbType.Text,2147483647);
pCC.Value = sCC;
myCommand.Parameters.Add(pCC);
SqlParameter pHtmlFormat = new SqlParameter("@HtmlFormat",SqlDbType.Bit,1);
pHtmlFormat.Value = bHtmlFormat.ToString();
myCommand.Parameters.Add(pHtmlFormat);
SqlParameter pContain = new SqlParameter("@Contain",SqlDbType.Int,4);
pContain.Value = nContain;
myCommand.Parameters.Add(pContain);
SqlParameter pAttachmentFlag = new SqlParameter("@AttachmentFlag",SqlDbType.Bit,1);
pAttachmentFlag.Value = bAttachmentFlag.ToString();
myCommand.Parameters.Add(pAttachmentFlag);
SqlParameter pMailID = new SqlParameter("@MailID",SqlDbType.Int,4);
pMailID.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(pMailID);
///定义返回值
int nResult = -1;
try
{
///打开链接
myConnection.Open();
///执行SQL语句
nResult = myCommand.ExecuteNonQuery();
}
catch(SqlException ex)
{
///抛出异常
throw new Exception(ex.Message,ex);
}
finally
{ ///关闭链接
myConnection.Close();
}
///返回nResult
return(int)myCommand.Parameters[8].Value;
}
public int SaveAsMailAttachment(string sName,string sUrl,string sType,
int nContain,int nMailID)
{
///创建链接
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
///定义SQL语句
string cmdText = "INSERT INTO Attachments (Name,Url,Type,Contain,MailID)VALUES("
"'" sName "',"
"'" sUrl "',"
"'" sType "',"
"'" nContain.ToString() "',"
"'" nMailID.ToString() "'"
")";
///创建Command
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
///定义返回值
int nResult = -1;
try
{
///打开链接
myConnection.Open();
///执行SQL语句
nResult = myCommand.ExecuteNonQuery();
}
catch(SqlException ex)
{
///抛出异常
throw new Exception(ex.Message,ex);
}
finally
{ ///关闭链接
myConnection.Close();
}
///返回nResult
return nResult;
}
public int MoveMail(int nMailID,int nFolderID)
{
///创建链接
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
SqlCommand myCommand = new SqlCommand("Pr_MoveMail",myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
///添加存储过程的参数
SqlParameter pMailID = new SqlParameter("@MailID",SqlDbType.Int,4);
pMailID.Value = nMailID;
myCommand.Parameters.Add(pMailID);
SqlParameter pFolderID = new SqlParameter("@FolderID",SqlDbType.Int,4);
pFolderID.Value = nFolderID;
myCommand.Parameters.Add(pFolderID);
///定义返回值
int nResult = -1;
try
{
///打开链接
myConnection.Open();
///执行SQL语句
nResult = myCommand.ExecuteNonQuery();
}
catch(SqlException ex)
{
///抛出异常
throw new Exception(ex.Message,ex);
}
finally
{ ///关闭链接
myConnection.Close();
}
///返回nResult
return nResult;
}
public int DeleteMail(int nMailID)
{
///创建链接
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
SqlCommand myCommand = new SqlCommand("Pr_DeleteMail",myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
///添加存储过程的参数
SqlParameter pMailID = new SqlParameter("@MailID",SqlDbType.Int,4);
pMailID.Value = nMailID;
myCommand.Parameters.Add(pMailID);
///定义返回值
int nResult = -1;
try
{
///打开链接
myConnection.Open();
///执行SQL语句
nResult = myCommand.ExecuteNonQuery();
}
catch(SqlException ex)
{
///抛出异常
throw new Exception(ex.Message,ex);
}
finally
{ ///关闭链接
myConnection.Close();
}
///返回nResult
return nResult;
}
public SqlDataReader GetAttachmentsByMail(int nMailID)
{
///创建链接
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
///定义SQL语句
string cmdText = "SELECT * FROM Attachments WHERE MailID='" nMailID.ToString() "'";
///创建Command
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
///定义DataReader
SqlDataReader dr = null;
try
{
///打开链接
myConnection.Open();
///读取数据
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(SqlException ex)
{
///抛出异常
throw new Exception(ex.Message,ex);
}
///返回DataReader
return dr;
}
#endregion
}
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论