第一步:创建发送邮件的类,源码如下:
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