public void ImapGetEmailInfo()
{
richEmailsInfo.Clear();
string annex = "";
ReceiveDecode rd = new ReceiveDecode();
foreach (var item in lists)
{
richEmailsInfo.AppendText("======================邮件内容======================\n");
richEmailsInfo.AppendText("当前第:" + count++ + "封信");
richEmailsInfo.AppendText("发件人: " + item.From + "\n");
richEmailsInfo.AppendText("时间: " + item.Date + "\n");
richEmailsInfo.AppendText("收件人: " + item.To + "\n");
richEmailsInfo.AppendText("抄送人: " + item.Cc + "\n");
richEmailsInfo.AppendText("邮件大小: " + item.MessageContext + "\n");
richEmailsInfo.AppendText("主题: " + item.Subject + "\n");
richEmailsInfo.AppendText("邮件内容:\n" + rd.ParseEmailText(item.Body.Entity.ToString()) + "\n");
for (int i = 0; i < item.Attachments.Length; i++)
annex += item.Attachments[i].ContentType.Param_Name.ToString();
richEmailsInfo.AppendText("附件内容: " + annex + "\n");
richEmailsInfo.AppendText("\n====================邮件接收完成====================\n\n"); ;
}
}
public void GetMails()
{
IMAP_Client IMAPServer = new IMAP_Client();
try
{
//连接邮件服务器通过传入邮件服务器地址和用于IMAP协议的端口号
IMAPServer.Connect(@"mail.sohu.net", 993, true);
//登陆邮箱,前者帐号后者密码
IMAPServer.Login("账号", @"密码");
IMAP_r_u_List[] l = IMAPServer.GetFolders(null);//调试后你会看到l的值
foreach (var item in l)
{
IMAPServer.SelectFolder(item.FolderName.ToString());
//IMAPServer.SelectFolder("垃圾箱");
//取出收件箱
if (item.FolderName == "INBOX" || item.FolderName == "垃圾邮件")//item.FolderName == "Junk")
{
var folder = IMAPServer.SelectedFolder;
//收件箱邮件总数
folder.MessagesCount.ToString();
//收件箱未读邮件总数
folder.RecentMessagesCount.ToString();
string str = folder.Name.ToString();
var seqSet = LumiSoft.Net.IMAP.IMAP_t_SeqSet.Parse("1:*");
var imap_t_Fetch_i = new IMAP_t_Fetch_i[]{
new IMAP_t_Fetch_i_Envelope(),
new IMAP_t_Fetch_i_Flags(),//
new IMAP_t_Fetch_i_InternalDate(),//
new IMAP_t_Fetch_i_Rfc822(),//
new IMAP_t_Fetch_i_Uid()//
};
EventHandler<LumiSoft.Net.EventArgs> lumisoftHandler = new EventHandler<LumiSoft.Net.EventArgs>(Fetchcallback);
IMAPServer.Fetch(false, seqSet, imap_t_Fetch_i, lumisoftHandler);
}
}
}
catch (Exception)
{
throw;
}
finally
{
IMAPServer.Disconnect();
}
}
public static void Fetchcallback(object sender, LumiSoft.Net.EventArgs eventArgs)
{
//把传入参数重新封装,用于取出邮件的相关信息
IMAP_r_u_Fetch x = eventArgs.Value as IMAP_r_u_Fetch;
string subject = x.Envelope.Subject;
//var st = y.Value.Rfc822.Stream;
var st = x.Rfc822.Stream;
st.Position = 0;
LumiSoft.Net.Mail.Mail_Message mime = LumiSoft.Net.Mail.Mail_Message.ParseFromStream(st);
lists.Add(mime);
}
IMAP邮件接收
最新推荐文章于 2025-06-24 03:07:13 发布