POP3收邮件附件~~求代码!

睡神在睡觉 2009-05-14 05:17:37
POP3收邮件如何读取附件内容?帮顶有分~
...全文
269 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
睡神在睡觉 2009-05-21
  • 打赏
  • 举报
回复
多谢各位.用EMail.dll做好了,发命令也做了个,但是有现成的dll何必非要自己写啊...呵呵,散分~
jinjazz 2009-05-16
  • 打赏
  • 举报
回复
google
随风v5 2009-05-15
  • 打赏
  • 举报
回复
顶一下,学习了

************************************************
zhengliyan_star 2009-05-15
  • 打赏
  • 举报
回复
!!!@#$@#$ 无语!!顶 !!
睡神在睡觉 2009-05-15
  • 打赏
  • 举报
回复
自己顶一下次啊~
睡神在睡觉 2009-05-15
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 jinjazz 的回复:]
C# codeprivate bool ReceiveMail()
{
Aspose.Network.Pop3.Pop3Client pc = new Aspose.Network.Pop3.Pop3Client();
pc.Host = Properties.Settings.Default.POPHost;
pc.Username = Properties.Settings.Default.UserName_pop;
pc.Password = Properties.Settings.Default.Password_pop;
try
{
pc.Connect();

[/Quote]


Aspose.Network.Pop3.Pop3Client 大哥,这个能给个不?
jinjazz 2009-05-15
  • 打赏
  • 举报
回复
private bool ReceiveMail()
{
Aspose.Network.Pop3.Pop3Client pc = new Aspose.Network.Pop3.Pop3Client();
pc.Host = Properties.Settings.Default.POPHost;
pc.Username = Properties.Settings.Default.UserName_pop;
pc.Password = Properties.Settings.Default.Password_pop;
try
{
pc.Connect();
pc.Login();

if (System.IO.File.Exists(fileName))
{
System.IO.File.Delete(fileName);
}
int i = pc.GetMessageCount();
if (i == 0) return false;
pc.SaveMessage(1, fileName);
//删除
pc.DeleteMessage(1);
pc.CommitDeletes();
if (System.IO.File.Exists(fileName))
{
DoPrintMessage("邮件已经下载");
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw new Exception("读取邮件出错:" + ex.Message.ToString());
}
}
Insmnia 2009-05-15
  • 打赏
  • 举报
回复
我以前保存的一篇文章,没看,全发给你了,你自己整理整理。
Insmnia 2009-05-15
  • 打赏
  • 举报
回复
===========================
我做了一个pop3接收客户端,在收取邮件时,服务器返回得内容如下:

+OK Return-Path: <com_com@21cn.com>Delivered-To: yysscheng@sina.comReceived: (qmail 32409 invoked from network); 28 Nov 2002 01:31:31 -0000Received: from unknown (HELO 21cn.com) (61.140.60.248) by 202.106.187.186 with SMTP; 28 Nov 2002 01:31:31 -0000Received: from 21cn.com([127.0.0.1]) by 21cn.com(AIMC 2.9.5.6) with SMTP id jm93de57653; Thr, 28 Nov 2002 09:48:55 +0800Received: from 21cn.com([10.2.2.1]) by 21cn.com(AIMC 2.9.5.1) with SMTP id jm73de5e038; Thr, 28 Nov 2002 09:48:55 +0800Content-Type: text/plainContent-Transfer-Encoding: 8bitMIME-Version: 1.0Message-ID: <ow978871906566.19829@webmail1.inner-21cn.com>X-AIMailer: AIMC 2.9.5.6 2002.10.10X-AIMime: MIME/SMIME Lib 2.9 2.9 2002.10.10Date: Thu, 28 Nov 2002 09:34:33 +0800 (CST)From: "??????"<com_com@21cn.com>To: yysscheng <yysscheng@sina.com>Cc: Subject: =?gb2312?b?sOy5q9fUtq+7rw==?=X-Priority: 3X-Originating-IP: 61.163.14.2X-AIMC-AUTH: (null)X-AIMC-MAILFROM: com_com@21cn.com?????? ?????????????????????????? ?????????? ?????? 11.28----------------------------------------------?????????????????????????????????? http://y.21cn.com ????A??210099???????????????????? http://sms.21cn.com/baobei/sjsy.html 21CN??????3??????12??10?????????? http://mail.21cn.com/huodong/9yue/ 21CN????????????????????????U?? http://mail.21cn.com/huodong/11yue/business/

其中的汉字如何解析出来?
(注:我得连接类如下:
public class Pop3Connection
{
private TcpClient socket;
private StreamReader reader;
private StreamWriter writer;
private bool connected;
public bool Connected
{
get{return connected;}
}
public Pop3Connection()
{
// TODO: 在此处添加构造函数逻辑
connected=false;
socket=new TcpClient();
}
~Pop3Connection()
{
socket=null;
}
internal void Open(string host,int port)
{
if(host==null || host.Trim()=="")
{
throw new ArgumentException("Host is null!");
}
if(port<0)
{
throw new ArgumentException("Port is invalid!");
}
try
{

socket.Connect(host,port);
reader=new StreamReader(socket.GetStream(),System.Text.Encoding.ASCII );
writer=new StreamWriter(socket.GetStream(),System.Text.Encoding.ASCII);
connected=true;
}
catch(Exception e)
{
connected=false;
}
}
internal void SendCommand(string cmd)
{
writer.WriteLine(cmd);
writer.Flush();
}
internal void GetReply(out string reply )
{
if(Connected)
{reply=reader.ReadLine();}
else
{reply="";}
}
internal void Close()
{
reader.Close();
writer.Flush();
writer.Close();
reader=null;
writer=null;
socket.Close();
connected=false;
}
}


Insmnia 2009-05-15
  • 打赏
  • 举报
回复
如何使用C#访问POP3服务器
内容:
This is a follow up to my SMTP example that shows how to
access your POP3 server. This program connects and logs on
to your POP3 server, and checks to see how many new
messages you have.

The instantiation of the POP is in Main() like this:
POP pop = new POP("pop-server", "loginname", "password");
You must replace "pop-server" with the name of your POP
server, "loginname" with your own log in, and "password"
with your password. The class has two methods. The Connect
method takes care of actually logging in to the server. The
TCPClient class is used to establish the connection.
The "user" and "pass" commands are used to login. Connect
returns a NetworkStream object created during the
connection process. The second method is
GetNumberOfNewMessages, which returns the number of unread
messages on the server. The response to the "stat" command
is parsed to extract the number of new messages.


Requirement:

Requires .NET SDK


How To Compile?


csc /r:System.Net.dll /r:System.IO.dll pop.cs

Source Code

using System.Net.Sockets;
using System.IO;
using System.Net;
using System;

class POP
{
string POPServer;
string user;
string pwd;
public POP(){}
public POP(string server, string _user, string _pwd)
{
POPServer = server;
user = _user;
pwd = _pwd;
}
private NetworkStream Connect()
{
TCPClient sender = new TCPClient(POPServer,110);
Byte[] outbytes;
string input;
NetworkStream ns = null;
try{
ns = sender.GetStream();
StreamReader sr = new StreamReader(ns);
Console.WriteLine(sr.ReadLine() );

input = "user " + user + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes
(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length) ;
Console.WriteLine(sr.ReadLine() );

input = "pass " + pwd + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes
(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length) ;
Console.WriteLine(sr.ReadLine() );

return ns;
}
catch(InvalidOperationException ioe){
Console.WriteLine("Could not connect to mail server");
return ns;
}
}
public int GetNumberOfNewMessages()
{
Byte[] outbytes;
string input;
try{
NetworkStream ns = Connect();
StreamReader sr = new StreamReader(ns);

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

input = "quit" + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes
(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length);
Console.WriteLine(sr.ReadLine());

sr.Close();
ns.Close();
return tokens[1].ToInt32();
}
catch(InvalidOperationException ioe){
Console.WriteLine("Could not connect to mail server");
return 0;
}
}
public static void Main()
{
POP pop = new POP("pop-server", "loginname", "password");
Console.WriteLine("New Messages = {0}",
pop.GetNumberOfNewMessages() );
Console.ReadLine();
}
}


======================
命令解析部分

private bool SwitchCommand(string POP3_commandTxt)
{
try
{
//---- Parse command --------------------------------------------------//
string[] cmdParts = POP3_commandTxt.TrimStart().Split(new char[]{' '});
string POP3_command = cmdParts[0];
POP3_command = POP3_command.ToUpper();
//---------------------------------------------------------------------//

//----- loging stuff --------------------------------------------------------------------------//
if(m_pPOP3_Server.LogCommands){
string lCmdTxt = POP3_commandTxt.Replace("\r\n","<CRLF>");
m_pLogWriter.AddEntry(lCmdTxt,this.SessionID,m_ConnectedIp,"C");
}
//---- End of loging ---------------------------------------------------------------------------//

switch(POP3_command)
{
case "USER":
USER(Core.GetArgsText(POP3_commandTxt,"USER"));
break;

case "PASS":
PASS(Core.GetArgsText(POP3_commandTxt,"PASS"));
break;

case "STAT":
STAT();
break;

case "LIST":
LIST(Core.GetArgsText(POP3_commandTxt,"LIST"));
break;

case "RETR":
RETR(Core.GetArgsText(POP3_commandTxt,"RETR"));
break;

case "DELE":
DELE(Core.GetArgsText(POP3_commandTxt,"DELE"));
break;

case "NOOP":
NOOP();
break;

case "RSET":
RSET();
break;

case "QUIT":
QUIT();
return true;


//----- Optional commands ----- //
case "UIDL":
UIDL(Core.GetArgsText(POP3_commandTxt,"UIDL"));
break;

case "APOP":
APOP(Core.GetArgsText(POP3_commandTxt,"APOP"));
break;

case "TOP":
TOP(Core.GetArgsText(POP3_commandTxt,"TOP"));
break;

default:
SendData("-ERR Invalid command\r\n");

//---- Check that maximum bad commands count isn't exceeded ---------------//
if(m_BadCmdCount > m_pPOP3_Server.MaxBadCommands-1){
SendData("421 Too many bad commands, closing transmission channel\r\n");
return true;
}
m_BadCmdCount++;
//-------------------------------------------------------------------------//

break;

}
}
catch(Exception x)
{
// Connection lost
if(!m_pClientSocket.Connected){
return true;
}

SendData("-ERR Unkown temp error\r\n");
m_pPOP3_Server.OnSysError(x,new System.Diagnostics.StackTrace());
}

return false;
}

Dobzhansky 2009-05-15
  • 打赏
  • 举报
回复
你要全部自己写?

邮件正文跟附件是作为一个整体收到的, 你要拆分的, 比较复杂

你可以找个开源的, 把邮件消息的封装类搞出来, 用来分析邮件, 分离附件.
睡神在睡觉 2009-05-15
  • 打赏
  • 举报
回复
自己仍回去....高手们,给个思路吧~
SK_Aqi 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhengliyan_star 的回复:]
public static class operatorMail
{
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="from">发件人的邮箱 </param>
/// <param name="to">收件人的邮箱 </param>
/// <param name="copy">抄送人的邮箱 </param>
/// <param name="subject">发送邮件的主题 </param>
/// <param name="content">发送邮件的内容 </param>

[/Quote]

这个是发送邮件的啊!?
睡神在睡觉 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wangkuang5 的回复:]
LumiSoft.Net
网络协议栈类库有完整实现,你自己去看
很有名的开源方案
[/Quote]
因为应用特殊,得自己写,呵呵
睡神在睡觉 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhengliyan_star 的回复:]
我这几天也账号在做发送邮件!呵呵 你在试验发送邮件的时候用企业邮箱!或者126的 别的不好用!
[/Quote]

哥们看错了吧?我要的是收邮件的POP的,不是stmp的~不过谢了,你这个发别的也可以吧?stmp服务器改了端口改了应该就可以吧....
wangkuang5 2009-05-14
  • 打赏
  • 举报
回复
LumiSoft.Net
网络协议栈类库有完整实现,你自己去看
很有名的开源方案
zhengliyan_star 2009-05-14
  • 打赏
  • 举报
回复
我这几天也账号在做发送邮件!呵呵 你在试验发送邮件的时候用企业邮箱!或者126的 别的不好用!
zhengliyan_star 2009-05-14
  • 打赏
  • 举报
回复
public static class operatorMail
{
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="from">发件人的邮箱</param>
/// <param name="to">收件人的邮箱</param>
/// <param name="copy">抄送人的邮箱</param>
/// <param name="subject">发送邮件的主题</param>
/// <param name="content">发送邮件的内容</param>
/// <param name="serverIp">smtp主机的 Ip 地址</param>
/// <param name="serverIp">附件</param>
/// <returns></returns>
public static string sendMail(string from, string to,string[] copy, string subject, string content,string serverIp,string attachment)
{
//发送邮件
//设置MailMessage类的from属性所需的MailAddress发件人的地址rowei.x.li@oyesgo.com
MailAddress fromAddress = new MailAddress(from);
//设置MailMessage类的to属性所需的MailAddress 收件人的地址
MailAddress toAddress = new MailAddress(to);

//新建一个MailMessage类实例
MailMessage message = new MailMessage(fromAddress, toAddress);
//设置这个实例的cc属性 邮件的抄送
for (int i = 0; i < copy.Length; i++)
{
if(copy[i]!="")
message.CC.Add(copy[i]);
}
//设置这个实例的Subject属性 邮件的主题
message.Subject = subject;
//设置这个实例的Body属性 邮件的内容
message.Body = content;
//设置邮件的格式
message.IsBodyHtml = true;
//添加附件
if (attachment != "")
{
Attachment files = new Attachment(attachment);
message.Attachments.Add(files);
}

//新建一个SmtpClient类的实例
SmtpClient client = new SmtpClient();
//设置在本机smtp服务器中绑定的ip地址 指定smtp主机
client.Host = serverIp;
//smtp端口,默认为25
client.Port = 25;
//发送
try
{
client.Send(message);
//发送完毕后提示
return "<script language='javascript'>alert('发送成功');</script>";
}
catch (Exception ex)
{
return ex.Message;
}
}
用Delphi2010 实现邮件附件发功能 TIdPOP3组件简介 TIdPOP3 是用来接邮件服务器的邮件信息到用户端的一个组件。它实现了RFC 1939协议。 在使用TIdPOP3组件时需设置它的几个成员属性。 Host :指定邮件服务器,一般为pop3邮件服务器的地址,如 pop3.126.com。 Username :用户名,也就是邮箱名,如billanking2002@126.com。 Password :邮箱密码,在进行发邮件时组件需要凭密码进行登录。 其它成员属性 Connected:返回它与邮件服务器的连接状态,这true表示已经连接。 CheckMessages:邮件数,如果连接服务器成功,则可以获得服务器端的邮件数。 成员函数 procedure Connect(const ATimeout: Integer = IdTimeoutDefault); 与服务器连接函数。参数为无效连接时等待的毫秒数。 function RetrieveHeader(const MsgNum: Integer; AMsg: TIdMessage): Boolean; 接邮件头信息,它有两个参数,MsgNum表示在接第几个邮件,从1开始,AMsg为邮件消息组件实例。 function Retrieve(const MsgNum: Integer; AMsg: TIdMessage): Boolean; 接邮件主体信息,它与 RetrieveHeader的参数是一样的。接的邮件内容将保存在AMsg中。 function Delete(const MsgNum: Integer): Boolean; 删除邮件服务器中第几个邮件。从1开始。 procedure Disconnect; override; 关闭连接。 TIdMessage组件简介 TIdMessage用来支持邮件消息协议,如POP3,SMTP,NNTP等。TIdMessage支持多用途Internet邮件扩展(MIME)协议。 常用的TIdMessage的属性: Subject:邮件主题,这个字符串经过BASE64编码的。所以在使用时需对它进行解码。 MessageParts:这是TIdMessageParts类的一个实例,它用来存储邮件的信息。如邮件内容及附件信息。在进行解析时需要判断它是否为附件或文本,如果为附件时,其文件名是经过BASE64编码的。判断常量分别为TIdText , TIdAttachment。 Body:这是个字符串列表,包含构成该邮件的正文内容。 Form:发送邮件者的地址信息。 Recipients:件人地址信息。 BccList:抄送地址列表。 CharSet:指示邮件信息中使用的字符集。 ContentType:指定MIME媒体数据类型,描述正文中包含的数据,使用户代理决定如何显示数据,常用的有text/html,text/xml。 TIdSMTP组件简介 TIdSMTP是TIdMessageClient派生出的一个简单邮件传输协议和SMTP客户端。 它的主要功能是发送邮件信息。 常用的属性: Host :SMTP邮件服务器的地址,如smtp.126.com。它与上面的POP3地址不一样。 AuthenticationType:服务器认证类型,它有atNone,atLogin两种,即不需要认证和需要凭用户名和密码进行认证。 Username:用户名,这里与TIdPOP3 有点不一样,即它不需要后缀,如billanking2002 Password:邮箱登录密码。如果AuthenticationType设置了atLongin则必须设置密码和用户名。

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧