代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
邮件发送类
作者:
Dezai.CN
/ 发布于
2012/8/30
/
648
using system; using system.text; using system.io; using system.net; using system.net.sockets; namespace osleagueforumxp.components { /// <summary> /// tcpclient派生类,用来进行smtp服务器的连接工作 /// </summary> public class smtpclient : tcpclient { /// <summary> /// 进行smtp服务器的连接 /// </summary> public smtpclient() { } /// <summary> /// 是否以连接 /// </summary> /// <returns>连接为true,不连接为false</returns> public bool isconnected() { return active; } /// <summary> /// 向服务器发送命令 /// </summary> /// <param name="command">命令</param> public void sendcommandtoserver(string command) { networkstream ns = this.getstream() ; byte[] writebuffer ; writebuffer = new byte[1024] ; writebuffer = encoding.default.getbytes(command); ns.write(writebuffer,0,writebuffer.length); return ; } /// <summary> /// 取得服务器反馈信息 /// </summary> /// <returns>字符串</returns> public string getserverresponse() { int streamsize ; string returnvalue = "" ; byte[] readbuffer ; networkstream ns = this.getstream() ; readbuffer = new byte[1024] ; streamsize = ns.read(readbuffer,0,readbuffer.length); if (streamsize==0) { return returnvalue ; } else { returnvalue = encoding.default.getstring(readbuffer); return returnvalue; } } /// <summary> /// 判断返回的信息中是否有指定的smtp代码出现 /// </summary> /// <param name="message">信息</param> /// <param name="smtpcode">smtp代码</param> /// <returns>存在返回false,不存在返回true</returns> public bool doesstringcontainsmtpcode(string message,string smtpcode) { return(message.indexof(smtpcode,0,10)==-1)?false:true; } } //结束类 /// <summary> /// 发送邮件类 /// </summary> public class smtpmail { /// <summary> /// 错误反馈信息 /// </summary> private string strerrmessage = null; /// <summary> /// smtp服务器反馈的信息 /// </summary> private string strresponse; /// <summary> /// 构造函数 /// </summary> public smtpmail() { strerrmessage = ""; strresponse = ""; } /// <summary> /// 取得错误反馈信息 /// </summary> public string errormessage { get { return strerrmessage ; } } /// <summary> /// 取得smtp服务器反馈的信息 /// </summary> public string serverresponse { get { return strresponse; } } /// <summary> /// 邮件发送优先级 /// </summary> public enum prioritys { /// <summary> /// 最高级别 /// </summary> high = 1, /// <summary> /// 默认级别 /// </summary> normal = 3, /// <summary> /// 最低级别 /// </summary> low = 5 } /// <summary> /// 进行base64编码 /// </summary> /// <param name="data">数据</param> /// <returns>字符串</returns> private string encode(string data) { byte[] btedata; btedata = encoding.default.getbytes(data); return convert.tobase64string(btedata); } /// <summary> /// 进行base64解码 /// </summary> /// <param name="data">数据</param> /// <returns>字符串</returns> private string decode(string data) { byte[] btedata; btedata = convert.frombase64string(data); return encoding.default.getstring(btedata); } /// <summary> /// 发送邮件 /// </summary> /// <param name="smtphost">smtp服务器</param> /// <param name="port">smtp服务器端口</param> /// <param name="from">邮件发送者</param> /// <param name="displayfromname">显示的发送者名称</param> /// <param name="authentication">是否进行认证</param> /// <param name="username">认证用户名</param> /// <param name="password">认证密码</param> /// <param name="to">邮件接收者</param> /// <param name="displaytoname">显示的接收者名称</param> /// <param name="priority">优先级</param> /// <param name="html">是否为html</param> /// <param name="base">url</param> /// <param name="subject">邮件主题</param> /// <param name="message">邮件内容</param> public void sendmail(string smtphost,int port,string from,string displayfromname,bool authentication,string username,string password,string to,string displaytoname,prioritys priority,bool html,string base,string subject,string message) { try { string strresponsenumber; smtpclient smtpcmail = new smtpclient(); smtpcmail.connect(smtphost,port); bool bolconnect = smtpcmail.isconnected(); //判断是否进行了连接 if (!bolconnect) { strerrmessage = "smtp服务器连接失败..."; return; } //读取反馈信息 strresponsenumber = smtpcmail.getserverresponse(); if (smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"220")) { this.strresponse += strresponsenumber; } else { this.strerrmessage = "连接失败" + strresponsenumber; return ; } int intbuffer = 0; int intarray = 0; if(authentication) { intbuffer = 9; } else { intbuffer = 6; } string[] strsendbuffer = new string[intbuffer]; string[] strresponsecode = {"220","250","251","354","221","334","235"}; // success codes from smtp server string strdata = ""; strdata = string.concat("helo ",smtphost); strdata = string.concat(strdata,"\r\n"); strsendbuffer[intarray] = strdata; intarray = intarray+1; if(authentication) { strdata = ""; strdata = string.concat("auth login","\r\n"); strdata = string.concat(strdata,"\r\n"); strsendbuffer[intarray] = strdata; intarray = intarray+1; strdata = ""; strdata = string.concat(encode(username),"\r\n"); strdata = string.concat(strdata,"\r\n"); strsendbuffer[intarray] = strdata; intarray = intarray+1; strdata = ""; strdata = string.concat(encode(password),"\r\n"); strdata = string.concat(strdata,"\r\n"); strsendbuffer[intarray] = strdata; intarray = intarray+1; } strdata = ""; strdata = string.concat("mail from: ","<" + from + ">" + "auth=" + from); strdata = string.concat(strdata,"\r\n"); strsendbuffer[intarray] = strdata; intarray = intarray+1; strdata = ""; strdata = string.concat("rcpt to: ","<" + to + ">"); strdata = string.concat(strdata,"\r\n"); strsendbuffer[intarray] = strdata; intarray = intarray+1; strdata = "" ; strdata = string.concat("data","\r\n"); strsendbuffer[intarray] = strdata ; intarray = intarray+1; strdata = "" ; strdata = string.concat("from: ",displayfromname + "<" + from + ">"); strdata = string.concat(strdata,"\r\n" ); strdata = string.concat(strdata,"to: " ); strdata = string.concat(strdata,displaytoname + "<" + to + ">"); strdata = string.concat(strdata,"\r\n" ); strdata = string.concat(strdata,"subject: " ); strdata = string.concat(strdata,subject); strdata = string.concat(strdata,"\r\n"); strdata = string.concat(strdata,"mime-version: 1.0" ); strdata = string.concat(strdata,"\r\n"); strdata = string.concat(strdata,"x-priority: " + priority); strdata = string.concat(strdata,"\r\n"); strdata = string.concat(strdata,"x-msmail-priority: " + priority); strdata = string.concat(strdata,"\r\n"); if(html) { strdata = string.concat(strdata,"content-type: text/html;" ); } else { strdata = string.concat(strdata,"content-type: text/plain;" ); } strdata = string.concat(strdata,"\r\n"); strdata = string.concat(strdata,"charset=\"iso-8859-1\"" ); strdata = string.concat(strdata,"\r\n"); if(html == true) { strdata = string.concat(strdata,"content-transfer-encoding: text/html;" ); } else { strdata = string.concat(strdata,"content-transfer-encoding: text/plain;" ); } strdata = string.concat(strdata,"\r\n"); strdata = string.concat(strdata,"content-base: \"" + base + "\"" ); strdata = string.concat(strdata,"\r\n" + "\r\n"); strdata = string.concat(strdata,message); strdata = string.concat(strdata,"\r\n.\r\n"); strsendbuffer[intarray] = strdata; intarray = intarray+1; strdata = "" ; strdata = string.concat(strdata,"quit\r\n"); strsendbuffer[intarray] = strdata; intarray = intarray+1; int i = 0 ; while(i < strsendbuffer.length) { smtpcmail.sendcommandtoserver(strsendbuffer[i]); strresponsenumber = smtpcmail.getserverresponse(); for(int j=0;j<strresponsecode.length;j++) { if (smtpcmail.doesstringcontainsmtpcode(strresponsenumber,strresponsecode[j])) { this.strresponse += strresponsenumber; this.strresponse += ""; break; } else { if(j==strresponsecode.length-1) { if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"500")) { this.strerrmessage = "邮箱地址错误"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"501")) { this.strerrmessage = "参数格式错误"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"502")) { this.strerrmessage = "命令不可实现"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"503")) { this.strerrmessage = "服务器需要smtp验证"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"504")) { this.strerrmessage = "命令参数不可实现"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"421")) { this.strerrmessage = "服务未就绪,关闭传输信道"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"450")) { this.strerrmessage = "要求的邮件操作未完成,邮箱不可用(例如,邮箱忙)"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"550")) { this.strerrmessage = "要求的邮件操作未完成,邮箱不可用(例如,邮箱未找到,或不可访问)"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"451")) { this.strerrmessage = "放弃要求的操作;处理过程中出错"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"551")) { this.strerrmessage = "用户非本地,请尝试<forward-path>"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"452")) { this.strerrmessage = "系统存储不足,要求的操作未执行"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"552")) { this.strerrmessage = "过量的存储分配,要求的操作未执行"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"553")) { this.strerrmessage = "邮箱名不可用,要求的操作未执行(例如邮箱格式错误)"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"553")) { this.strerrmessage = "邮箱名不可用,要求的操作未执行(例如邮箱格式错误)"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"432")) { this.strerrmessage = "需要一个密码转换"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"534")) { this.strerrmessage = "认证机制过于简单"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"538")) { this.strerrmessage = "当前请求的认证机制需要加密"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"454")) { this.strerrmessage = "临时认证失败"; } else if(!smtpcmail.doesstringcontainsmtpcode(strresponsenumber,"530")) { this.strerrmessage = "需要认证"; } return; } } } i++ ; } // 结束循环 } catch(socketexception err) { this.strerrmessage += err.message + " " + err.stacktrace; } catch(exception e) { this.strerrmessage += e.message + " " + e.stacktrace; } } //结束邮件发送方法 } // 结束类 }
试试其它关键字
邮件发送类
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3