spire.email组件支持通过pop3和imap接收邮件。本文将分别介绍如何使用spire.email通过pop3和imap接收邮件并将邮件 保存到磁盘。
pop3
c#
//创建pop3client实例
pop3client pop3 = new pop3client();
//设置邮件服务器、端口、连接协议、邮箱和密码
pop3.host = "outlook.office365.com";
pop3.username = "recipient @e-iceblue.com";
pop3.password = "password";
pop3.port = 995;
pop3.enablessl = true;
//连接服务器
pop3.connect();
//获取第一封邮件
mailmessage message = pop3.getmessage(1);
//解析邮件内容
console.writeline("------------------ headers ---------------");
console.writeline("发件人: " message.from.tostring());
console.writeline("收件人: " message.to.tostring());
console.writeline("日期 : " message.date.tostring(cultureinfo.invariantculture));
console.writeline("主题 : " message.subject);
console.writeline("------------------- body -----------------");
console.writeline(message.bodytext);
console.writeline("------------------- end ------------------");
//保存邮件并以邮件的主题作为文件名
message.save(message.subject ".eml", mailmessageformat.eml);
console.writeline("保存成功!");
vb.net
'创建pop3client实例
dim pop3 as new pop3client()
'设置邮件服务器、端口、连接协议、邮箱和密码
pop3.host = "outlook.office365.com"
pop3.username = "recipient @e-iceblue.com"
pop3.password = "password"
pop3.port = 995
pop3.enablessl = true
'连接服务器
pop3.connect()
'获取第一封邮件
dim message as mailmessage = pop3.getmessage(1)
'解析邮件内容
console.writeline("------------------ headers ---------------")
console.writeline("发件人: " message.from.tostring())
console.writeline("收件人: " message.[to].tostring())
console.writeline("日期 : " message.[date].tostring(cultureinfo.invariantculture))
console.writeline("主题 : " message.subject)
console.writeline("------------------- body -----------------")
console.writeline(message.bodytext)
console.writeline("------------------- end ------------------")
'保存邮件并以邮件的主题作为文件名
message.save(message.subject ".eml", mailmessageformat.eml)
console.writeline("保存成功!")
imap
c#
//创建imapclient实例
imapclient imap = new imapclient();
//设置邮件服务器、端口、连接协议、邮箱和密码
imap.host = "outlook.office365.com";
imap.port = 143;
imap.username = "recipient @e-iceblue.com";
imap.password = "password";
imap.connectionprotocols = connectionprotocols.ssl;
//连接服务器
imap.connect();
//选择inbox文件夹
imap.select("inbox");
//获取第一封邮件
mailmessage message = imap.getfullmessage(1);
//解析邮件内容
console.writeline("------------------ headers ---------------");
console.writeline("发件人: " message.from.tostring());
console.writeline("收件人: " message.to.tostring());
console.writeline("日期 : " message.date.tostring(cultureinfo.invariantculture));
console.writeline("主题 : " message.subject);
console.writeline("------------------- body -----------------");
console.writeline(message.bodytext);
console.writeline("------------------- end ------------------");
//保存邮件并以邮件的主题作为文件名
message.save(message.subject ".eml", mailmessageformat.eml);
console.writeline("保存成功!");
vb.net
'创建imapclient实例
dim imap as new imapclient()
'设置邮件服务器、端口、连接协议、邮箱和密码
imap.host = "outlook.office365.com"
imap.port = 143
imap.username = "recipient @e-iceblue.com"
imap.password = "password"
imap.connectionprotocols = connectionprotocols.ssl
'连接服务器
imap.connect()
'选择inbox文件夹
imap.[select]("inbox")
'获取第一封邮件
dim message as mailmessage = imap.getfullmessage(1)
'解析邮件内容
console.writeline("------------------ headers ---------------")
console.writeline("发件人: " message.from.tostring())
console.writeline("收件人: " message.[to].tostring())
console.writeline("日期 : " message.[date].tostring(cultureinfo.invariantculture))
console.writeline("主题 : " message.subject)
console.writeline("------------------- body -----------------")
console.writeline(message.bodytext)
console.writeline("------------------- end ------------------")
'保存邮件并以邮件的主题作为文件名
message.save(message.subject ".eml", mailmessageformat.eml)
console.writeline("保存成功!")