代码语言
.
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/11/21
/
542
using System; using System.Text; using System.IO; using System.Net; using System.Net.Sockets; namespace Blood.Com.ClassLib { /// <summary> /// TcpClient派生类,用来进行SMTP服务器的连接工作 /// </summary> public class SMTPClient : TcpClient { public bool isConnected() { return Active; } 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 ; } 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; } } public bool DoesStringContainSMTPCode(string s,string SMTPCode) { return(s.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 } public void SendMail(string SmtpHost,int Port,string From,string DisplayFromName,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 ; } string[] strSendBuffer = new string[6]; string[] strResponseCode = {"220","250","251","354","221"}; // success codes from SMTP server string strData = ""; strData = string.Concat("HELO ",SmtpHost); strData = string.Concat(strData,"\r\n"); strSendBuffer[0] = strData ; strData = ""; strData = string.Concat("MAIL FROM: ","<" + From + ">"); strData = string.Concat(strData,"\r\n"); strSendBuffer[1] = strData; strData = ""; strData = string.Concat("RCPT TO: ","<" + To + ">"); strData = string.Concat(strData,"\r\n"); strSendBuffer[2] = strData; strData = "" ; strData = string.Concat("DATA","\r\n"); strSendBuffer[3] = strData ; 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 == true) { 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[4] = strData; strData = "" ; strData = string.Concat(strData,"QUIT\r\n"); strSendBuffer[5] = strData; 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) { this.strErrMessage += strResponseNumber; this.strErrMessage += strSendBuffer[i]; return; } } } i++ ; } // 结束循环 } catch(SocketException err) { this.strErrMessage += err.Message + " " + err.StackTrace; } catch(Exception e) { this.strErrMessage += e.Message + " " + e.StackTrace; } } //结束邮件发送方法 } // 结束类 } // 结束Namespace
试试其它关键字
群发邮件
同语言下
.
文件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