check the message boundary and stop when you reach the attachment part, for example after you read "Content-Type: application/..."also see
A POP3 Client in C# .NEThttp://www.codeproject.com/csharp/despop3client.asp
A POP3 Client in C# .NEThttp://www.codeproject.com/csharp/despop3client.asp
解决方案 »
- ***请教:有什么好办法asp.net页面head区批量添加内容?***
- 问一个关于petshop4 中sqlhelper的问题!
- DataListItemEventArgs e 这里面的e是什么东西?
- cuteeditor的问题(在线等)
- 开发一个类似于51JOB这样的网站,一个6个人的团,需要多久?
- 网站做好了,上传到服务商主机上部署出现点问题,高手过来指点一下把
- 我在aa.aspx文件的aa.cs中创建了public void aa()的方法,请问,我在类中能不能调用这aa()的方法?
- 请问webform中如何调用系统自带的计算器程序?
- 问一个serv-U的问题。
- 百分求一个句话?关于电子商务传值的问题?
- 怪事了,为什么我的dropdownlist的值明明选上了,可是为什么读出来显示给lable还是为空呢?
- 如何让同一主机上运行的asp.net web service与windows form共享数据,推者有分
用imap4就可以在服务端分析他的内容,然后确定下载哪个部分
BaseEntity class
------------------------------------------------------------------
using System;namespace Mail.Pop
{
public class BaseEntity
{
protected string header; //all header
public string contentType;
public string contentTransferEncoding;
public string charset; public BaseEntity(string buffer)
{
GetHeader(buffer);
this.contentType =this.GetValueFromHeader("Content-Type: ");
this.contentTransferEncoding=this.GetValueFromHeader("Content-Transfer-Encoding: "); if (contentType.IndexOf("text/plain")!=-1 || contentType.IndexOf("text/html")!=-1)
this.charset=GetValueEqualFromHeader("charset=");
} protected void GetHeader(string buffer)
{
int end=buffer.IndexOf("\r\n\r\n"); if (end==-1)
throw new Exception("can't get header");
header=buffer.Substring(0, end);
header+="\r\n\r\n";
} protected string GetValueFromHeader(string key)
{
int start=header.IndexOf(key);
if(start<0)
return "";
start=header.IndexOf(' ',start);
if (start<0)
return "";
int end=header.IndexOf("\r\n",start);
if (end<=0)
return "";
return QuotedCoding.Decode(header.Substring(start+1, end-start-1).Trim());
} /// <summary>
/// z 'ala="123"'
/// </summary>
/// <param name="key">ala=</param>
/// <returns>123</returns>
protected string GetValueEqualFromHeader(string key)
{
int start=header.IndexOf(key);
if (start==-1)
return "";
start=header.IndexOf('"',start);
int end=header.IndexOf('"',start+1);
if (start==-1 || end==-1)
return "";
return QuotedCoding.Decode(header.Substring(start+1,end-start-1));
} }
}
---------------------------------------------
using System;
using System.Collections;namespace Mail.Pop
{
public class Pop3Message : BaseEntity
{
public string from;
public string replyTo;
public string subject;
public string date;
public string boundary;
public bool hasAttachments=false;
public string body;
public ArrayList attachments=new ArrayList();
//buffer ma na poczatku "+OK"
//onlyHeader mowi czy tylko header czy cala wiadomosc
public Pop3Message(string buffer,bool onlyHeader) : base(buffer)
{
this.from =this.GetValueFromHeader("From: ");
this.replyTo =this.GetValueFromHeader("Reply-To: ");
this.subject =this.GetValueFromHeader("Subject: ");
this.date =this.GetValueFromHeader("Date: ");
if(contentType.IndexOf("multipart")!=-1)
{
boundary="--"+GetValueEqualFromHeader("boundary=");
hasAttachments=true;
if (onlyHeader==false)
ParseMixedMessage(buffer);
}
else
{
hasAttachments=false;
if (onlyHeader==false)
this.body=GetTextMessage(buffer);
}
} internal string GetTextMessage(string buffer)
{
// find "\r\n\r\n" denoting end of header
int start=buffer.IndexOf("\r\n\r\n")+4;
int end=buffer.LastIndexOf("\r\n.\r\n");
//change charset if contentTransferEncoding is 8bit
if (this.contentTransferEncoding.IndexOf("8bit")!=-1)
return StringOperations.Change(buffer.Substring(start,end-start),charset);
else
return buffer.Substring(start,end-start);
} internal bool ParseMixedMessage(string buffer)
{
int start=0,end;
for(;;)
{
start=buffer.IndexOf(boundary,start);
if (start==-1)
break;
end=buffer.IndexOf(boundary,start+1); if (end==-1)
break; // remove boundary from buffer
start += boundary.Length; Attachment a=new Attachment( buffer.Substring(start,end-start-2) ); this.attachments.Add(a);
}
return true;
} /// <summary>
/// zwraca mail pomiedzy '<' i '>'
/// </summary>
static public string GetMail(string s)
{
int start=s.IndexOf('<')+1;
int end=s.LastIndexOf('>')-1;
if (start==-1 || end==-1)
return "";
return s.Substring(start,end-start+1);
}
}
}