1 首先需要用NuGet将LumiSoft.Net.dll添加到引用
2 顺便给出官方的例子链接
3 接收邮件的代码,需要的库都是系统可以提示的,就不多提了。
代码简要说明:传入结构体之前,需要将mailserver,mailserverport,userName(收件人邮箱地址),userPass(收件人邮箱密码)初始化,程序中未做判断是否为空,使用时需要注意。邮件信息都在结构体内,附件信息在maliAttachMents里面,存放的是附件接收后的本地文件目录,一般默认为当前目录下的mailInfo/AttachMents里面。
注意:此例子接收了所有邮件,并向邮件服务器发送了接收完邮件并删除邮件的指令!具体代码: message.MarkForDeletion();
struct MailInfo
{
public string mailServer;
public int mailServerPort;
public string userName;
public string userPass;
public string mailSender;
public string mailTittle;
public string mailSubject;
public string mailBodyText;
public List<string> mailAttachMents;
public MailInfo(string mails,int mailp, string un,string up)
{
mailServer = mails;
mailServerPort = mailp;
userName = un;
userPass = up;
mailSender = "";
mailTittle = "";
mailSubject = "";
mailBodyText = "";
mailAttachMents = new List<string>();
}
};
void MailReceive(MailInfo mailInfo)
{
using (POP3_Client pop3 = new POP3_Client())
{
try
{
pop3.Connect(mailInfo.mailServer, mailInfo.mailServerPort, false);
pop3.Login(mailInfo.userName,mailInfo.userPass);
try
{
int msgNum = pop3.Messages.Count;
foreach (POP3_ClientMessage message in pop3.Messages)
{
Mail_Message mime = Mail_Message.ParseFromByte(message.HeaderToByte());
//邮件发送者
if (mime.From != null)
{
mailInfo.mailSender = mime.From[0].Address.ToString();
}
//邮件主题
if (string.IsNullOrEmpty(mime.Subject))
{
mailInfo.mailSubject = "无主题";
}
else
{
mailInfo.mailSubject = mime.Subject;
}
mime = Mail_Message.ParseFromByte(message.MessageToByte());
//邮件正文
if (mime.BodyText != null)
{
mailInfo.mailBodyText = mime.BodyText;
}
else
{
if (!string.IsNullOrEmpty(mime.BodyHtmlText))
{
mailInfo.mailBodyText = mime.BodyHtmlText;
}
}
//邮件附件
foreach (MIME_Entity entity in mime.Attachments)
{
if (entity.ContentDisposition != null && entity.ContentDisposition.Param_FileName != null)
{
string fileName = entity.ContentDisposition.Param_FileName;
mailInfo.mailAttachMents.Add(fileName);
string filePath = Directory.GetCurrentDirectory() + @"\MailInfo\Attachments";//本机邮件内容保存路径
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
string str_PathAttachMent = filePath + "\\" + fileName;
File.WriteAllBytes(str_PathAttachMent, ((MIME_b_SinglepartBase)entity.Body).Data);
}
}
//删除邮件服务器上的邮件,使用删除标志位
message.MarkForDeletion();
}
}
catch (Exception x)
{
// ShowError.MsgBox("Error: " + x.Message);
}
}
catch (Exception ex)
{
//ShowError.MsgBox(ex.Message);
}
}
}
//调用
private void button_Receive_Click(object sender, EventArgs e)
{
MailInfo mi = new MailInfo("192.168.1.1",110, "111@qq.com", "111111");
MailReceive(mi);
}