`
yyw84
  • 浏览: 77243 次
社区版块
存档分类
最新评论

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

阅读更多
一个基于 .NET 2.0 System.Net.Mail namespace 的邮件发送接收组件,参考了以前网上一些基于 System.Web.Mail namespace 例子。


<!---->/**//********************************************
 * 
 * description: 基于 .NET 2.0 System.Net.Mail namespace 
 *              的邮件发送接收组件 
 * 
 * author:      yyw84
 * 
 * blogs:       
http://yyw84.cnblogs.com/
 * 
 * Date:        2006-5-4 
 * 
 *******************************************
*/


using System;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.Net.Sockets;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Net.Configuration;
using System.Configuration;

namespace Mailer.Components
{
    
邮件接收类#region 邮件接收类

    
/**//// <summary>
    
/// 邮件接收类
    
/// </summary>
    public class POP3
    
{
        
Fields#region Fields

        
string POPServer;
        
string mPOPUserName;
        
string mPOPPass;
        
int mPOPPort;
        NetworkStream ns;
        StreamReader sr;

        
#endregion


        
Constructors#region Constructors

        
/**//// <summary>
        
/// POP3
        
/// </summary>
        
/// <param name="server">POP3服务器名称</param>
        
/// <param name="userName">用户名</param>
        
/// <param name="password">用户密码</param>
        public POP3(string server, string userName, string password)
            : 
this(server, 110, userName, password)
        
{
        }


        
/**//// <summary>
        
/// POP3
        
/// </summary>
        
/// <param name="server">POP3服务器名称</param>
        
/// <param name="port">端口号</param>
        
/// <param name="userName">用户名</param>
        
/// <param name="password">用户密码</param>
        public POP3(string server, int port, string userName, string password)
        
{
            POPServer 
= server;
            mPOPUserName 
= userName;
            mPOPPass 
= password;
            mPOPPort 
= port;
        }


        
#endregion


        
Methods#region Methods

        
Public#region Public

        
/**//// <summary>
        
/// 获得新邮件数量
        
/// </summary>
        
/// <returns>新邮件数量</returns>
        public int GetNumberOfNewMessages()
        
{
            
byte[] outbytes;
            
string input;

            
try
            
{
                Connect();

                input 
= "stat" + "\r\n";
                outbytes 
= System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
                ns.Write(outbytes, 
0, outbytes.Length);
                
string resp = sr.ReadLine();
                
string[] tokens = resp.Split(new Char[] ' ' });

                Disconnect();

                
return Convert.ToInt32(tokens[1]);
            }

            
catch
            
{
                
return -1;
            }

        }


        
/**//// <summary>
        
/// 获取新邮件内容
        
/// </summary>
        
/// <param name="subj">邮件主题</param>
        
/// <returns>新邮件内容</returns>
        public List<MailMessage> GetNewMessages(string subj)
        
{

            
int newcount;
            List
<MailMessage> newmsgs = new List<MailMessage>();

            
try
            
{
                newcount 
= GetNumberOfNewMessages();
                Connect();

                
for (int n = 1; n < newcount + 1; n++)
                
{
                    List
<string> msglines = GetRawMessage(n);
                    
string msgsubj = GetMessageSubject(msglines);
                    
if (msgsubj.CompareTo(subj) == 0)
                    
{
                        MailMessage msg 
= new MailMessage();
                        msg.Subject 
= msgsubj;
                        msg.From 
= new MailAddress(GetMessageFrom(msglines));
                        msg.Body 
= GetMessageBody(msglines);
                        newmsgs.Add(msg);
                        DeleteMessage(n);
                    }

                }


                Disconnect();
                
return newmsgs;
            }

            
catch (Exception e)
            
{
                
return newmsgs;
            }

        }


        
/**//// <summary>
        
/// 获取新邮件内容
        
/// </summary>
        
/// <param name="nIndex">新邮件索引</param>
        
/// <returns>新邮件内容</returns>
        public MailMessage GetNewMessages(int nIndex)
        
{
            
int newcount;
            MailMessage msg 
= new MailMessage();

            
try
            
{
                newcount 
= GetNumberOfNewMessages();
                Connect();
                
int n = nIndex + 1;

                
if (n < newcount + 1)
分享到:
评论

相关推荐

    System.Linq.dll 让.net2.0也可以使用Linq

    如果服务器上只有.net2.0的时候,但是又使用了以前的3.5或者以上版本。我们就需要这个Linq.dll。

    Microsoft ASP.NET 2.0 AJAX Extensions 1.0.zip

    主要用来解决:Parser Error Message: Could not load file or assembly 'System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies....

    .net 2.0 的 system.dll

    标题中的".net 2.0 的 system.dll"指的是.NET Framework 2.0版本中的核心库文件System.dll。这个文件是微软开发的.NET框架的一部分,它包含了大量的基础类库,为.NET应用程序提供运行时环境和各种服务。.NET ...

    Wrox.Professional.ASP.NET.2.0.Server.Control.and.Component.Development.Aug.2006.rar

    书中详细介绍了如何利用基类System.Web.UI.WebControls.WebControl和System.Web.UI.WebControls.BaseDataBoundControl来创建自定义控件,并且讲解了控件的属性和事件绑定机制。 组件开发是另一个重要的主题,特别是...

    CS.NET中用System.Web.Mail发送邮件

    使用C#的System.Web.Mail类来实现邮件自动发送的示例

    Oracle.ManagedDataAccess.dll net 2.0

    Oracle.ManagedDataAccess.dll net 2.0

    c# .netframework2.0调用sqlite

    C# 2.0 调用sqlite的System.Data.SQLite.dll文件,含调用类实例。 1、将附件dll放于工程目录 2、添加引用,即可使用sqlite 关键词: C# .netFramework 2.0 Sqlite c#调用sqlite做参考,实际测试通过。

    ASP.NET2.0图片格式转换.

    总的来说,ASP.NET 2.0 中的图片格式转换可以通过 `System.Drawing` 命名空间提供的类来实现,通过读取原始图片,创建新的图像格式并保存,从而达到转换的目的。在实际开发中,可以根据项目需求扩展此功能,如添加更...

    .Net 3.5 使用 System.Threading.Task

    .Net 3.5 下使用的 System.Threading.Tasks。 安装完成后,添加引用时只需要在安装目录 C:\Program Files (x86)\Microsoft Reactive Extensions\Redist\DesktopV2 下找到 System.Threading.dll,添加即可

    System.Net.Http.Formatting.dll

    System.Net.Http.Formatting.dll

    c#.NET2.0发送邮件Demo源代码

    基于.NET2.0的System.Net.Mail发送邮件Demo源代码

    ASP.NET2.0数据库项目案例导航

    5. 邮件收发功能模块:ASP.NET 2.0的System.Net.Mail命名空间提供了发送邮件的功能。开发者可以设置SMTP服务器,创建MailMessage对象,添加收件人、主题、正文和附件,然后通过SmtpClient发送邮件。 6. 新闻管理...

    ASP.NET2.0配置文件Web.config 详细解释.rar

    例如,`&lt;system.web.extensions&gt;`用于管理ASP.NET AJAX库的配置,`&lt;compilation&gt;`元素则用于控制编译设置,如目标框架版本和调试模式。 5. **安全性设置**: `Web.config`还可以用于实现Web应用程序的安全性策略,...

    ASP.NET 2.0入门与提高系列课程(5):ASP.NET 2.0配置与部署

    3. 配置节:如system.web、system.webServer等,这些节用于定义特定的配置选项,如身份验证、授权、错误处理和缓存策略。 4. 应用程序配置:包括设置应用程序的根目录、启用调试模式、指定编译目标框架版本等。 二...

    System.Net.Http.Formatting.xml

    System.Net.Http.Formatting.xml

    深度挖掘.NET Framework 2.0(9):.NET 2.0对未处理异常的处理以及跟踪技巧.rar

    在.NET 2.0中,System.Diagnostics命名空间提供了Trace和Debug类,它们支持输出信息、警告和错误,帮助开发者在运行时追踪代码执行流程。这些类包含了Write、WriteLine、Assert等方法,可以根据需要在代码中插入跟踪...

    Wince.NET2.0(文件)

    8. **类库支持**: .NET Compact Framework虽然精简,但仍支持大部分常见的.NET类库,如System.IO(文件操作)、System.Net(网络通信)和System.Drawing(图形处理),但可能不包括所有桌面版的高级功能。...

    asp.net2.0 调用websevice 实现天气预报

    在本项目中,“asp.net2.0 调用websevice 实现天气预报”,我们将探讨如何利用ASP.NET 2.0集成Web服务(Web Service)来获取并展示实时天气信息,从而实现一个无刷新的天气预报应用。 首先,Web服务是一种基于XML的...

    基于.net2.0Email程序

    总结,基于.NET 2.0的Email程序利用了.NET Framework 2.0的`System.Net.Mail`命名空间,通过`MailMessage`和`SmtpClient`类实现邮件的创建与发送。"BMailer"作为这样的程序,旨在简化电子邮件的管理工作,为用户提供...

    ASP.NET 2.0网络开发技术 ASP.NET 2.0类,对象和命名空间

    ASP.NET 2.0 是微软推出的用于构建动态网站和Web应用程序的框架,它基于.NET Framework,支持面向对象的编程模型。在ASP.NET 2.0中,开发者可以通过使用类、对象和命名空间来构建复杂的Web应用程序。 面向对象编程...

Global site tag (gtag.js) - Google Analytics