POP接收邮件+附件代码
private List<emailMessage> GetEmailsFromSMTPServer(string server, string username, string password, int port, bool ssl)
{
Messaging.EMail.POP3.Pop3MailClient pop = new Pop3MailClient(server, port, ssl, username, password);
List<emailMessage> messages = new List<emailMessage>();
try
{
Regex date = new Regex(@"(?:Date:(?<date>.+))");
Regex from = new Regex(@"(?:From:(?<from>.+))");
Regex to = new Regex(@"(?:To:(?<to>.+))");
Regex subject = new Regex(@"(?:Subject:(?<subject>.+))");
Regex dateRegex = new Regex(@"\w+,(?<date>[^\+\-]+)[\+\-].+");
Regex matchSubject = new Regex(@"\#+(?<name>[^\#]+)\#+(?<id>[^\#]+)\#\#+");
string message = "";
pop.Connect();
pop.IsAutoReconnect = true;
pop.ReadTimeout =180000;
int NumberOfMails, MailboxSize;
pop.GetMailboxStats(out NumberOfMails, out MailboxSize);
if (NumberOfMails > 0)
{
for (int i = 1; i <= NumberOfMails; i++)
{
emailMessage m = new emailMessage();
pop.GetRawEmail(i, out message);
m.mailindex = i;
m.subject = subject.Match(message).Groups[1].Value;
try
{
m.date = Convert.ToDateTime(dateRegex.Match(date.Match(message).Groups[1].Value).Groups[1].Value);
}
catch
{
m.date = DateTime.Now;
}
m.body = GetEmailBody(message);
try
{
m.attachments = GetEmailAttachments(message, out m.attachmentName);
}
catch
{ }
parsFrom(from.Match(message).Groups[1].Value, out m.displayname, out m.from);
parsTo(to.Match(message).Groups[1].Value, out m.displaytoname, out m.to);
messages.Add(m);
}
pop.DeleteEmail(1);
}
pop.Disconnect();
return messages;
}
catch (Exception err)
{
pop.Disconnect();
return messages;
}
}
private string GetEmailBody(string message)
{
string body = "";
string code = "";
string codeType = "7bit";
try
{
body = message.Substring(message.IndexOf("Content-Transfer-Encoding:"));
code = body.Substring(0, body.IndexOf("\r\n"));
if (code.IndexOf("base64") >= 0)
codeType = "base64";
body = body.Substring(body.IndexOf("\r\n\r\n") + 4);
//if (body.IndexOf("\r\n\r\n--") > 0)
// body = body.Substring(0, body.IndexOf("\r\n\r\n--"));
//else if (body.LastIndexOf("\r\n.\r\n") > 0)
// body = body.Substring(0, body.LastIndexOf("\r\n.\r\n"));
if (body.IndexOf("\r\n--") > 0)
body = body.Substring(0, body.IndexOf("\r\n--"));
else if (body.LastIndexOf("\r\n.") > 0)
body = body.Substring(0, body.LastIndexOf("\r\n."));
body = body.Replace("Content-Disposition: inline\r\n", "");
if (codeType == "base64" || message.IndexOf("charset=\"GB2312\"") > 0)
{
return decodemail(body.Trim(), "gb2312");
}
else
return body;
}
catch (Exception err)
{
return "";
}
return message;
}
private string GetEmailAttachments(string message,out string name)
{
string body = "";
string code = "";
string codeType = "7bit";
string fileName = "";
try
{
//string str = message.Substring(message.IndexOf("Content-Type: text/plain; name="));
string str = message.Substring(message.IndexOf("Content-Type: text/plain;"));
if (str.Substring(str.IndexOf("Content-Transfer-Encoding:")).IndexOf("base64") >= 0)
{
codeType = "base64";
}
body = message.Substring(message.IndexOf("Content-Disposition: attachment;"));
fileName = message.Substring(message.IndexOf("Content-Disposition: attachment;"));
fileName = fileName.Substring(fileName.IndexOf('=')+1, fileName.IndexOf("\r\n\r\n")-fileName.IndexOf('=')-1);
fileName = fileName.Replace("\"", "");
name = fileName;
code = body.Substring(0, body.IndexOf("\r\n"));
if (code.IndexOf("base64") >= 0)
codeType = "base64";
body = body.Substring(body.IndexOf("\r\n\r\n") + 4);
//if (body.IndexOf("\r\n\r\n--") > 0)
// body = body.Substring(0, body.IndexOf("\r\n\r\n--"));
//else if (body.LastIndexOf("\r\n.\r\n") > 0)
// body = body.Substring(0, body.LastIndexOf("\r\n.\r\n"));
if (body.IndexOf("\r\n--") > 0)
body = body.Substring(0, body.IndexOf("\r\n--"));
else if (body.LastIndexOf("\r\n.") > 0)
body = body.Substring(0, body.LastIndexOf("\r\n."));
body = body.Replace(code, "");
if (codeType == "base64" || message.IndexOf("charset=\"GB2312\"") > 0)
{
//return decodemail(body.Trim(), "gb2312");
return body.Trim();
}
else
return body;
}
catch (Exception err)
{
name = fileName;
return " ";
}
return message;
}
private void parsFrom(string from, out string displayname, out string email)
{
Regex r = new Regex(@"(?<name>[^""\<]+)[^\<]*\<(?<from>[^\>]+)\>");
Match m = r.Match(from);
if (r.IsMatch(from))
{
displayname = m.Groups["name"].Value;
email = m.Groups["from"].Value;
}
else
{
if (from != "")
{
displayname = "";
email = from;
}
else
{
displayname = "";
email = "";
}
}
}
private void parsTo(string to, out string displayname, out string email)
{
Regex r = new Regex(@"(?<name>[^""\<]+)[^\<]*\<(?<to>[^\>]+)\>");
Match m = r.Match(to);
if (r.IsMatch(to))
{
displayname = m.Groups["name"].Value;
email = m.Groups["to"].Value;
}
else
{
if (to != "")
{
email = to;
displayname = "";
}
else
{
displayname = "";
email = "";
}
}
}
private string decodemail(string body, string encode)
{
string s = body;
try
{
if (encode != "")
{
byte[] bytes = Convert.FromBase64String(body.Trim());
s = System.Text.Encoding.GetEncoding(encode).GetString(bytes);
}
}
catch { }
return s;
}
private string decodemail(byte[] bytes, string encode)
{
string s ="";//= body;
try
{
if (encode != "")
{
//byte[] bytes = Convert.FromBase64String(body.Trim());
s = System.Text.Encoding.GetEncoding(encode).GetString(bytes);
}
}
catch { }
return s;
}
class emailMessage
{
public string subject = "";
public DateTime date = DateTime.Now;
public string displayname = "";
public string from = "";
public string body = "";
public int mailindex = -1;
public string displaytoname = "";
public string to = "";
public string attachments = "";
public byte[] attachmentByte = null;
public string attachmentName = "";
}
class Attachment
{
public byte[] attachmentByte = null;
public string attachmentName = "";
public int length;
}