You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.5 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MailAnalyzeTools.Common;
using OpenPop.Pop3;
using System.Windows.Forms;
using MailAnalyzeTools.Models;
namespace MailAnalyzeTools
{
class ReceiveMailTool
{
/// <summary>
/// 连接邮件服务器
/// </summary>
/// <param name="pop3Client"></param>
/// <param name="txtMailServer"></param>
/// <param name="iPort"></param>
/// <param name="chkSSL"></param>
/// <param name="txtUserName"></param>
/// <param name="txtPassword"></param>
/// <param name="showErrTip"></param>
/// <returns></returns>
private static bool ConnectMailServer(Pop3Client pop3Client, string txtMailServer, int iPort, bool chkSSL, string txtUserName, string txtPassword, bool showErrTip = false)
{
try
{
pop3Client.Connect(txtMailServer, iPort, chkSSL);
pop3Client.Authenticate(txtUserName, txtPassword);
}
catch (Exception ex)
{
LogHelper.WriteLog(typeof(ReceiveMailTool), ex);
//MessageBox.Show(String.Format(info, ex.Message));
return false;
}
return true;
}
/// <summary>
///
/// </summary>
/// <param name="lblInfo"></param>
public static void DeleteOver3daysMail(Label lblInfo, bool showPrompt = true, int para_ioverdays = 3)
{
bool conOk;
Pop3Client pop3Client = new Pop3Client();
conOk = frmAnalyzeMail.GetOpenClient(pop3Client);
if (!conOk)
{
// MessageBox.Show("无法连接到服务器");
return;
}
//至少保留2天的数据
int ioverdays= para_ioverdays<=2?2:para_ioverdays;
int iend = BinarySearchOver3days(pop3Client, ioverdays);
if (iend > 0)
{ //查询到位置)
OpenPop.Mime.Header.MessageHeader mlhd = pop3Client.GetMessageHeaders(iend);
if(showPrompt)
{
string info = string.Format("将要删除3天以上的历史邮件{0}封,要删除的最近的一封邮件信息是:{1}\n您确信要删除这些邮件吗?",
iend, (string.IsNullOrEmpty(mlhd.Subject) ? "[无标题]" : mlhd.Subject) + mlhd.DateSent.ToLocalTime().ToString());
if (MessageBox.Show(null, info, "信息提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
{
return;
}
}
if (lblInfo != null)
lblInfo.Visible = true;
List<string> uidsTodelList = new List<string>();
//倒序删除
string lstinfo = "正在删除第{0}/{1}封邮件关键字息...";
int ipos = iend;
for (; ipos >= 1; ipos--)
{
Application.DoEvents();
if (lblInfo != null)
lblInfo.Text = string.Format(lstinfo, iend - ipos, iend);
pop3Client.DeleteMessage(ipos);
}
System.Threading.Thread.Sleep(500);
pop3Client.Disconnect();
}
}
/// <summary>
/// 二分法查找
/// </summary>
/// <param name="arr"></param>
/// <param name="key">要查找的对象</param>
public static int BinarySearchOver3days(Pop3Client pop3Client, int overdays = 3)
{
int low = 1;
int high = pop3Client.GetMessageCount();
if (high < 1)
return 0;
DateTime dtsent = pop3Client.GetMessageHeaders(1).DateSent.ToLocalTime();
if (StrUtill.DateDiff(DateTime.Today, dtsent.Date) < overdays)//第一封信不超过3天则退出
return 0;
int imid = (low + high) / 2;
while (imid < high && imid >= low)
{
int middle = (low + high) / 2;
dtsent = pop3Client.GetMessageHeaders(middle).DateSent.ToLocalTime();
int days = StrUtill.DateDiff(DateTime.Today, dtsent.Date);
if (days < overdays)//3天以内
{
imid = middle - 1;
dtsent = pop3Client.GetMessageHeaders(imid).DateSent.ToLocalTime();
days = StrUtill.DateDiff(DateTime.Today, dtsent.Date);
if (days >= overdays) //符合3天以外
{
return imid;//如果找到了就直接返回这个元素的索引
}
high = middle - 1;
imid = (low + high) / 2;
}
else //超过3天
{
imid = middle + 1;
dtsent = pop3Client.GetMessageHeaders(imid).DateSent.ToLocalTime();
days = StrUtill.DateDiff(DateTime.Today, dtsent.Date);
if (days < overdays) //符合3天内
{
return middle; //如果找到了就直接返回这个元素的索引
}
else
low = imid;//超过3天
}
}
return -1;//如果找不到就返回-1
}
}
}