基于 .NET 2.0 System.Net.Mail namespace 的邮件发送组件

该组件基于.NET 2.0 System.Net.Mail namespace,支持邮件接收功能,包括获取新邮件数量及内容等。
一个基于 .NET 2.0 System.Net.Mail namespace 的邮件发送接收组件,参考了以前网上一些基于 System.Web.Mail namespace 例子。

ContractedBlock.gifExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif/**//********************************************
InBlock.gif * 
InBlock.gif * description: 基于 .NET 2.0 System.Net.Mail namespace 
InBlock.gif *              的邮件发送接收组件 
InBlock.gif * 
InBlock.gif * author:      yyw84
InBlock.gif * 
InBlock.gif * blogs:       
http://yyw84.cnblogs.com/
InBlock.gif * 
InBlock.gif * Date:        2006-5-4 
InBlock.gif * 
ExpandedBlockEnd.gif *******************************************
*/

None.gif
None.gif
using System;
None.gif
using System.Net;
None.gif
using System.Net.Mail;
None.gif
using System.Net.Mime;
None.gif
using System.Threading;
None.gif
using System.Net.Sockets;
None.gif
using System.IO;
None.gif
using System.Collections;
None.gif
using System.Collections.Generic;
None.gif
using System.Net.Configuration;
None.gif
using System.Configuration;
None.gif
None.gif
namespace Mailer.Components
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif    
邮件接收类#region 邮件接收类
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 邮件接收类
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class POP3
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Fields#region Fields
InBlock.gif
InBlock.gif        
string POPServer;
InBlock.gif        
string mPOPUserName;
InBlock.gif        
string mPOPPass;
InBlock.gif        
int mPOPPort;
InBlock.gif        NetworkStream ns;
InBlock.gif        StreamReader sr;
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Constructors#region Constructors
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// POP3
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="server">POP3服务器名称</param>
InBlock.gif        
/// <param name="userName">用户名</param>
ExpandedSubBlockEnd.gif        
/// <param name="password">用户密码</param>

InBlock.gif        public POP3(string server, string userName, string password)
InBlock.gif            : 
this(server, 110, userName, password)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// POP3
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="server">POP3服务器名称</param>
InBlock.gif        
/// <param name="port">端口号</param>
InBlock.gif        
/// <param name="userName">用户名</param>
ExpandedSubBlockEnd.gif        
/// <param name="password">用户密码</param>

InBlock.gif        public POP3(string server, int port, string userName, string password)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            POPServer 
= server;
InBlock.gif            mPOPUserName 
= userName;
InBlock.gif            mPOPPass 
= password;
InBlock.gif            mPOPPort 
= port;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Methods#region Methods
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Public#region Public
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获得新邮件数量
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>新邮件数量</returns>

InBlock.gif        public int GetNumberOfNewMessages()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte[] outbytes;
InBlock.gif            
string input;
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Connect();
InBlock.gif
InBlock.gif                input 
= "stat" + "\r\n";
InBlock.gif                outbytes 
= System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
InBlock.gif                ns.Write(outbytes, 
0, outbytes.Length);
InBlock.gif                
string resp = sr.ReadLine();
ExpandedSubBlockStart.gifContractedSubBlock.gif                
string[] tokens = resp.Split(new Char[] dot.gif' ' });
InBlock.gif
InBlock.gif                Disconnect();
InBlock.gif
InBlock.gif                
return Convert.ToInt32(tokens[1]);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return -1;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取新邮件内容
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="subj">邮件主题</param>
ExpandedSubBlockEnd.gif        
/// <returns>新邮件内容</returns>

InBlock.gif        public List<MailMessage> GetNewMessages(string subj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
int newcount;
InBlock.gif            List
<MailMessage> newmsgs = new List<MailMessage>();
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                newcount 
= GetNumberOfNewMessages();
InBlock.gif                Connect();
InBlock.gif
InBlock.gif                
for (int n = 1; n < newcount + 1; n++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    List
<string> msglines = GetRawMessage(n);
InBlock.gif                    
string msgsubj = GetMessageSubject(msglines);
InBlock.gif                    
if (msgsubj.CompareTo(subj) == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        MailMessage msg 
= new MailMessage();
InBlock.gif                        msg.Subject 
= msgsubj;
InBlock.gif                        msg.From 
= new MailAddress(GetMessageFrom(msglines));
InBlock.gif                        msg.Body 
= GetMessageBody(msglines);
InBlock.gif                        newmsgs.Add(msg);
InBlock.gif                        DeleteMessage(n);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                Disconnect();
InBlock.gif                
return newmsgs;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return newmsgs;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取新邮件内容
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="nIndex">新邮件索引</param>
ExpandedSubBlockEnd.gif        
/// <returns>新邮件内容</returns>

InBlock.gif        public MailMessage GetNewMessages(int nIndex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int newcount;
InBlock.gif            MailMessage msg 
= new MailMessage();
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                newcount 
= GetNumberOfNewMessages();
InBlock.gif                Connect();
InBlock.gif                
int n = nIndex + 1;
InBlock.gif
InBlock.gif                
if (n < newcount + 1)
ExpandedSubBlockStart.gifContractedSubBloc
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值