文章分类 | 推荐文章 | 最新文章 | 热点文章 | 最新软件 | 精品软件 | 下载排行 | 软件分类 | 推荐下载 | 常用软件| 专题栏目 |
软件下载网络学院数码学院绿色软件幽默笑话
业界资讯 | 图形图像 | 操作系统 | 网络冲浪 | 工具软件 | 办公软件 | 媒体动画 | 精文荟萃 | 认证考试 | 网站建设 | 技术开发 | 专栏
ASP空间 域名注册
当前位置:清风网络学院工具软件邮件工具C# - MailSender 邮件发送组件源代码 (支持ESMTP, 附件)
精品推荐

VPN路由技术交换技术 VoIP
特别推荐
·Windows 2003邮件服务器配置指南(图)
·三步建立电子邮局
·新手入门:企业邮箱及邮件服务器架设
·Foxmail Server 实用操作手册
·管理我们的邮件队列(图)
·使用E-mail的99条技巧
·掌握Gmail邮箱常用快捷键 用好Gmail
·忘了Foxmail帐户访问密码的七种解决办法
·Foxmail6.0超级实用技巧放送(一)
·新手使用foxmail应该注意的几个问题
·简单实用Foxmail小技巧三则
·邮件软件Foxmail也有切分器
·各种邮箱垃圾邮件自动删除的方法
·让Foxmail的特快专递更加准确
·新手使用foxmail应该注意的几个问题
·简单实用Foxmail小技巧三则
·轻松改变Foxmail邮箱帐户的存放位置
·发送邮件不求人 SMTP也可DIY
·纠正Foxmail自动过滤电子杂志的错误
·误将Foxmail密码忘记之后
热点TOP10
·用客户端软件收发Hotmail Yahoo邮件
·导出、备份Outlook邮件帐户及邮件
·自己电脑做smtp服务器不求人
·把数码相片制作成VCD到底有多难
·Windows 2003邮件服务器配置指南(图)
·什么是POP3和SMTP
·Vista系统家庭共享上网必读
·使用UTF-8编码/JMail组件发送邮件乱码问题
·.net中发mail到hotmail中乱码问题的解决
·Live Communication Server 2003安装和管理指南
·解决Outlook无法连接Exchanger Server一例
·用CDO.Message打造邮件发送程序
·解决Foxmail Server不能收邮件
·Gmail邮箱使用技巧集萃
·解决邮件乱码三板斧
·新手入门:企业邮箱及邮件服务器架设
·发送邮件不求人 SMTP也可DIY
·解决Foxmail Server不能发送邮件
·六则实用的Outlook应用技巧
·利用OpenSmtp.Net发送需要smtp验证的邮件

C# - MailSender 邮件发送组件源代码 (支持ESMTP, 附件)

日期:2006年12月19日 作者: 查看:[大字体 中字体 小字体]


//============================================================
// file: MailSender.cs
// 邮件发送组件
// 支持ESMTP, 多附件
//============================================================

namespace JcPersonal.Utility
{
using System;
using System.Collections;
using System.Net.Sockets;
using System.IO;
using System.Text;

///


/// Mail 发送器
///

public class MailSender
{
///
/// SMTP服务器域名
///

public string Server {
get { return server; }
set { if (value != server) server = value; }
} private string server = "";

///


/// SMTP服务器端口 [默认为25]
///

public int Port {
get { return port; }
set { if (value != port) port = value; }
} private int port = 25;

///


/// 用户名 [如果需要身份验证的话]
///

public string UserName {
get { return userName; }
set { if (value != userName) userName = value; }
} private string userName = "";

///


/// 密码 [如果需要身份验证的话]
///

public string Password {
get { return password; }
set { if (value != password) password = value; }
} private string password = "";

///


/// 发件人地址
///

public string From {
get { return from; }
set { if (value != from) from = value;}
} private string from = "";

///


/// 收件人地址
///

public string To {
get { return to; }
set { if (value != to) to = value;}
} private string to = "";

///


/// 发件人姓名
///

public string FromName {
get { return fromName; }
set { if (value != fromName) fromName = value; }
} private string fromName = "";

///


/// 收件人姓名
///

public string ToName {
get { return toName; }
set { if (value != toName) toName = value; }
} private string toName = "";

///


/// 邮件的主题
///

public string Subject {
get { return subject; }
set { if (value != subject) subject = value; }
} private string subject = "";

///


/// 邮件正文
///

public string Body {
get { return body; }
set { if (value != body) body = value; }
} private string body = "";

///


/// 超文本格式的邮件正文
///

public string HtmlBody {
get { return htmlBody; }
set { if (value != htmlBody) htmlBody = value; }
} private string htmlBody = "";

///


/// 是否是html格式的邮件
///

public bool IsHtml {
get { return isHtml; }
set { if (value != isHtml) isHtml = value; }
} private bool isHtml = false;

///


/// 语言编码 [默认为GB2312]
///

public string LanguageEncoding {
get { return languageEncoding; }
set { if (value != languageEncoding) languageEncoding = value; }
} private string languageEncoding = "GB2312";

///


/// 邮件编码 [默认为8bit]
///

public string MailEncoding {
get { return encoding; }
set { if (value != encoding) encoding = value; }
} private string encoding = "8bit";

///


/// 邮件优先级 [默认为3]
///

public int Priority {
get { return priority; }
set { if (value != priority) priority = value; }
} private int priority = 3;

///


/// 附件 [AttachmentInfo]
///

public IList Attachments {
get { return attachments; }
// set { if (value != attachments) attachments = value; }
} private ArrayList attachments = new ArrayList ();


///


/// 发送邮件
///

public void SendMail ()
{
// 创建TcpClient对象, 并建立连接
TcpClient tcp = null;
try
{
tcp = new TcpClient (server, port);
}
catch (Exception)
{
throw new Exception ("无法连接服务器");
}

ReadString (tcp.GetStream());//获取连接信息

// 开始进行服务器认证
// 如果状态码是250则表示操作成功
if (!Command (tcp.GetStream(), "EHLO Localhost", "250"))
throw new Exception ("登陆阶段失败");

if (userName != "")
{
// 需要身份验证
if (!Command (tcp.GetStream(), "AUTH LOGIN", "334"))
throw new Exception ("身份验证阶段失败");
string nameB64 = ToBase64 (userName); // 此处将username转换为Base64码
if (!Command (tcp.GetStream(), nameB64, "334"))
throw new Exception ("身份验证阶段失败");
string passB64 = ToBase64 (password); // 此处将password转换为Base64码
if (!Command (tcp.GetStream(), passB64, "235"))
throw new Exception ("身份验证阶段失败");
}


// 准备发送
WriteString (tcp.GetStream(), "mail From: " + from);
WriteString (tcp.GetStream(), "rcpt to: " + to);
WriteString (tcp.GetStream(), "data");

// 发送邮件头
WriteString (tcp.GetStream(), "Date: " + DateTime.Now); // 时间
WriteString (tcp.GetStream(), "From: " + fromName + "<" + from + ">"); // 发件人
WriteString (tcp.GetStream(), "Subject: " + subject); // 主题
WriteString (tcp.GetStream(), "To:" + toName + "<" + to + ">"); // 收件人

//邮件格式
WriteString (tcp.GetStream(), "Content-Type: multipart/mixed; boundary=\"unique-boundary-1\"");
WriteString (tcp.GetStream(), "Reply-To:" + from); // 回复地址
WriteString (tcp.GetStream(), "X-Priority:" + priority); // 优先级
WriteString (tcp.GetStream(), "MIME-Version:1.0"); // MIME版本

// 数据ID,随意
// WriteString (tcp.GetStream(), "Message-Id: " + DateTime.Now.ToFileTime() + "@security.com");
WriteString (tcp.GetStream(), "Content-Transfer-Encoding:" + encoding); // 内容编码
WriteString (tcp.GetStream(), "X-Mailer:JcPersonal.Utility.MailSender"); // 邮件发送者
WriteString (tcp.GetStream(), "");

WriteString (tcp.GetStream(), ToBase64 ("This is a multi-part message in MIME format."));
WriteString (tcp.GetStream(), "");

// 从此处开始进行分隔输入
WriteString (tcp.GetStream(), "--unique-boundary-1");

// 在此处定义第二个分隔符
WriteString (tcp.GetStream(), "Content-Type: multipart/alternative;Boundary=\"unique-boundary-2\"");
WriteString (tcp.GetStream(), "");

if(!isHtml)
{
// 文本信息
WriteString (tcp.GetStream(), "--unique-boundary-2");
WriteString (tcp.GetStream(), "Content-Type: text/plain;charset=" + languageEncoding);
WriteString (tcp.GetStream(), "Content-Transfer-Encoding:" + encoding);
WriteString (tcp.GetStream(), "");
WriteString (tcp.GetStream(), body);
WriteString (tcp.GetStream(), "");//一个部分写完之后就写如空信息,分段
WriteString (tcp.GetStream(), "--unique-boundary-2--");//分隔符的结束符号,尾巴后面多了--
WriteString (tcp.GetStream(), "");
}
else
{
//html信息
WriteString (tcp.GetStream(), "--unique-boundary-2");
WriteString (tcp.GetStream(), "Content-Type: text/html;charset=" + languageEncoding);
WriteString (tcp.GetStream(), "Content-Transfer-Encoding:" + encoding);
WriteString (tcp.GetStream(), "");
WriteString (tcp.GetStream(), htmlBody);
WriteString (tcp.GetStream(), "");
WriteString (tcp.GetStream(), "--unique-boundary-2--");//分隔符的结束符号,尾巴后面多了--
WriteString (tcp.GetStream(), "");
}

// 发送附件
// 对文件列表做循环
for (int i = 0; i < attachments.Count; i++)
{
WriteString (tcp.GetStream(), "--unique-boundary-1"); // 邮件内容分隔符
WriteString (tcp.GetStream(), "Content-Type: application/octet-stream;name=\"" + ((AttachmentInfo)attachments[i]).FileName + "\""); // 文件格式
WriteString (tcp.GetStream(), "Content-Transfer-Encoding: base64"); // 内容的编码
WriteString (tcp.GetStream(), "Content-Disposition:attachment;filename=\"" + ((AttachmentInfo)attachments[i]).FileName + "\""); // 文件名
WriteString (tcp.GetStream(), "");
WriteString (tcp.GetStream(), ((AttachmentInfo)attachments[i]).Bytes); // 写入文件的内容
WriteString (tcp.GetStream(), "");
}

Command (tcp.GetStream(), ".", "250"); // 最后写完了,输入"."

// 关闭连接
tcp.Close ();
}

///


/// 向流中写入字符
///

/// 来自TcpClient的流
/// 写入的字符
protected void WriteString (NetworkStream netStream, string str)
{
str = str + "\r\n"; // 加入换行符

// 将命令行转化为byte[]
byte[] bWrite = Encoding.GetEncoding(languageEncoding).GetBytes(str.ToCharArray());

// 由于每次写入的数据大小是有限制的,那么我们将每次写入的数据长度定在75个字节,一旦命令长度超过了75,就分步写入。
int start=0;
int length=bWrite.Length;
int page=0;
int size=75;
int count=size;
try
{
if (length>75)
{
// 数据分页
if ((length/size)*sizepage=length/size+1;
else
page=length/size;
for (int i=0;i{
start=i*size;
if (i==page-1)
count=length-(i*size);
netStream.Write(bWrite,start,count);// 将数据写入到服务器上
}
}
else
netStream.Write(bWrite,0,bWrite.Length);
}
catch(Exception)
{
// 忽略错误
}
}

///


/// 从流中读取字符
///

/// 来自TcpClient的流
/// 读取的字符
protected string ReadString (NetworkStream netStream)
{
string sp = null;
byte[] by = new byte[1024];
int size = netStream.Read(by,0,by.Length);// 读取数据流
if (size > 0)
{
sp = Encoding.Default.GetString(by);// 转化为String
}
return sp;
}

///


/// 发出命令并判断返回信息是否正确
///

/// 来自TcpClient的流
/// 命令
/// 正确的状态码
/// 是否正确
protected bool Command (NetworkStream netStream, string command, string state)
{
string sp=null;
bool success=false;
try
{
WriteString (netStream, command);// 写入命令
sp = ReadString (netStream);// 接受返回信息
if (sp.IndexOf(state) != -1)// 判断状态码是否正确
success=true;
}
catch(Exception)
{
// 忽略错误
}
return success;
}

///


/// 字符串编码为Base64
///

/// 字符串
/// Base64编码的字符串
protected string ToBase64 (string str)
{
try
{
byte[] by = Encoding.Default.GetBytes (str.ToCharArray());
str = Convert.ToBase64String (by);
}
catch(Exception)
{
// 忽略错误
}
return str;
}

///


/// 附件信息
///

public struct AttachmentInfo
{
///
/// 附件的文件名 [如果输入路径,则自动转换为文件名]
///

public string FileName {
get { return fileName; }
set { fileName = Path.GetFileName(value); }
} private string fileName;

///


/// 附件的内容 [由经Base64编码的字节组成]
///

public string Bytes {
get { return bytes; }
set { if (value != bytes) bytes = value; }
} private string bytes;

///


/// 从流中读取附件内容并构造
///

/// 附件的文件名
///
public AttachmentInfo (string ifileName, Stream stream)
{
fileName = Path.GetFileName (ifileName);
byte[] by = new byte [stream.Length];
stream.Read (by,0,(int)stream.Length); // 读取文件内容
//格式转换
bytes = Convert.ToBase64String (by); // 转化为base64编码
}

///


/// 按照给定的字节构造附件
///

/// 附件的文件名
/// 附件的内容 [字节]
public AttachmentInfo (string ifileName, byte[] ibytes)
{
fileName = Path.GetFileName (ifileName);
bytes = Convert.ToBase64String (ibytes); // 转化为base64编码
}

///


/// 从文件载入并构造
///

///
public AttachmentInfo (string path)
{
fileName = Path.GetFileName (path);
FileStream file = new FileStream (path, FileMode.Open);
byte[] by = new byte [file.Length];
file.Read (by,0,(int)file.Length); // 读取文件内容
//格式转换
bytes = Convert.ToBase64String (by); // 转化为base64编码
file.Close ();
}
}
}
}

--------------------------------------------------------------------------------


// 使用:

MailSender ms = new MailSender ();
ms.From = "jovenc@tom.com";
ms.To = "jovenc@citiz.net";
ms.Subject = "Subject";
ms.Body = "body text";
ms.UserName = "########"; // 怎么能告诉你呢
ms.Password = "********"; // 怎么能告诉你呢
ms.Server = "smtp.tom.com";

ms.Attachments.Add (new MailSender.AttachmentInfo (@"D:\test.txt"));

Console.WriteLine ("mail sending...");
try
{
ms.SendMail ();
Console.WriteLine ("mail sended.");
}
catch (Exception e)
{
Console.WriteLine (e);
}

,

(出处:http://www.yono.cn)


C# - MailSender 邮件发送组件源代码 (支持ESMTP, 附件) 相关软件:
C# - MailSender 邮件发送组件源代码 (支持ESMTP, 附件) 相关文章:

特别声明:本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
[打印本页] [关闭窗口] 转载请注明来源:http://www.yono.cn
| 帮助(?) | 版权声明 | 友情连接 | 关于我们 | 信息发布
Copyright 2005-2005 www.yono.cn All Rights Reserved.
Powered by:mesky