c# 收取邮件 解析_转 C#接收邮件

这是一个使用C#编写的POP3邮件收取类,包括连接邮件服务器、解码邮件标题、获取邮件数量、获取邮件内容等方法。通过示例展示了如何调用该类来接收并显示邮件数量。
摘要由CSDN通过智能技术生成

第一步:创建发送邮件的类,源码如下:

using System.Net.Sockets;

using System.Collections;

using System.IO;

using System.Net;

using System;

using System.Net.Mail;

using System.Collections.Generic;

using System.Text;

namespace MyMailSpace

{

public class POP3

{

string POPServer;

string user;

string pwd;

NetworkStream ns;

StreamReader sr;

///

/// 解码

///

///

///

private string Decode(string[] strss)  //=?utf-8?B?5Lqy54ix55qEIDExNzg3NzIxMjNxcWNv?=

{

//获取标题的编码方式

Encoding b = Encoding.GetEncoding(strss[1]);

string code = strss[3];

string decode = "";

byte[] byteCode = null;

if (strss[2].ToUpper() == "B")

{

byteCode = DecodeBase64(ref strss[3]);

}

else if (strss[2].ToUpper() == "Q")

{

byteCode = DecodeQP(ref strss[3]);

}

try

{

decode = b.GetString(byteCode);

}

catch

{

decode = code;

}

if (decode.Contains("\0"))

decode = decode.Replace("\0", "");

return decode;

}

//对邮件标题解码  quoted-printable

///

///  quoted-printable  解码

///

///

///

private byte[] DecodeBase64(ref string code)

{

string st = code + "000";//

string strcode = st.Substring(0, (st.Length / 4) * 4);

return Convert.FromBase64String(strcode);

}

//对邮件标题解码  quoted-printable

///

///  quoted-printable  解码

///

///

///

private byte[] DecodeQP(ref string code)//=E6=AC=A2=E8=BF=8E=E6=88=90=E4=B8=BA=E7=8C=AA=E5=85=AB=E6=88=92=E7=BD=91=E7=AB=99=E4=BC=9A=E5=91=98=E3=80=82

{

string[] textArray1 = code.Split(new char[] { '=' });

byte[] buf = new byte[textArray1.Length];

try

{

for (int i = 0; i < textArray1.Length; i++)

{

if (textArray1[i].Trim() != string.Empty)

{

byte[] buftest = new byte[2];

buf[i] = (byte)int.Parse(textArray1[i].Substring(0, 2), System.Globalization.NumberStyles.HexNumber);

}

}

}

catch

{

return null;

}

return buf;

}

///

/// 接收邮件服务器相关信息

///

/// 参数 pop邮件服务器地址 

/// 参数 登录到pop邮件服务器的用户名 

/// 参数  登录到pop邮件服务器的密码

/// 无返回

public POP3(string server, string _user, string _pwd)

{

POPServer = server;

user = _user;

pwd = _pwd;

}

///

/// 登陆服务器

///

private void Connect()

{

TcpClient sender = new TcpClient(POPServer, 110);

Byte[] outbytes;

string input;

string readu

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值