本文介紹兩個QQ應(yīng)用中有意思的小功能,都是基于郵件對象實(shí)現(xiàn)的功能,一個是利用.NET 內(nèi)置類MailMessage來實(shí)現(xiàn)日志發(fā)送功能,一個是利用Lumisoft.NET組件類來檢測用戶是否開通了SMTP功能。
想看看兩個功能的實(shí)現(xiàn)效果。
編輯并發(fā)送QQ日志的代碼很簡單,就是簡單利用MailMessage對象來實(shí)現(xiàn)的,類似發(fā)送郵件一樣,詳細(xì)代碼如下所示:
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(string.Format("{0}@qq.com", Portal.gc.QQ));
mail.To.Add(new MailAddress(string.Format("{0}@qzone.qq.com", Portal.gc.QQ)));
string title = this.txtTitle.Text;
if (!string.IsNullOrEmpty(title))
{
title = string.Format("[{0}]{1}", this.txtCategory.Text, this.txtTitle.Text);
}
mail.Subject = title;
mail.Body = this.txtContent.Text;
SmtpClient client = new SmtpClient("smtp.qq.com", 25);
client.Credentials = new NetworkCredential(Portal.gc.QQ, Portal.gc.QQPass);
client.Send(mail);
MessageExUtil.ShowTips("QQ留言發(fā)表成功!");
this.txtTitle.Clear();
this.txtContent.Clear();
}
catch (Exception ex)
{
LogTextHelper.WriteLine(ex.ToString());
MessageExUtil.ShowError(ex.Message);
}
批量檢查開通SMTP功能的代碼如下所示:
private void StartAtClick(object obj)
{
if (this.dg.Rows.Count == 0 || this.dg.SelectedRows.Count == 0)
{
SetTips("請先輸入資料,并選定一個開始");
return;
}
string selectQQ = "";
foreach (DataGridViewRow row in this.dg.SelectedRows)
{
selectQQ = row.Cells["賬號"].Value.ToString();
break;
}
QQDict.Clear();
QQList.Clear();
foreach (DataGridViewRow row in this.dg.Rows)
{
string qq = row.Cells["賬號"].Value.ToString();
string pass = row.Cells["密碼"].Value.ToString();
if (!string.IsNullOrEmpty(qq) && !QQDict.ContainsKey(qq))
{
QQDict.Add(qq, pass);
QQList.Add(qq);
}
}
int current = QQList.IndexOf(selectQQ);
if (QQDict.Count > 0 && current >= 0)
{
isStart = true;
currentIndex = current;
StartQQAt();
}
}
private void StartQQAt()
{
if (!isStart || QQDict.Count == 0)
return;
if (currentIndex >= QQDict.Count)
{
isStart = false;
SetTips("已經(jīng)是最后一個QQ");
return;
}
string qq = QQList[currentIndex];
string pass = QQDict[qq];
if (!string.IsNullOrEmpty(qq))
{
CallCtrlWithThreadSafety.SetText(this.lblOperateTips, string.Format("正在處理QQ[{0}]...", qq), this);
CallCtrlWithThreadSafety.SetText(this.txtQQNumber, qq, this);
Application.DoEvents();
Thread.Sleep(100);
CheckSmtpOpen(qq, pass);
}
}
private void CheckSmtpOpen(string user, string pass)
{
bool success = CheckLogin(user, pass);
if (success)
{
SetTips(string.Format("QQ用戶[{0}] 測試成功", user));
if (!SuccessList.Contains(user))
{
SuccessList.Add(user);
if (failedList.Contains(user))
{
failedList.Remove(user);//移除成功的數(shù)據(jù)
}
UpdateList();
}
}
else
{
if (!failedList.Contains(user))
{
failedList.Add(user);
UpdateList();
}
SetTips(string.Format("QQ用戶[{0}] 測試失敗,可能密碼不正確或未開通SMTP", user));
}
Interlocked.Increment(ref currentIndex);
StartQQAt();
}
private bool CheckLogin(string user, string pass)
{
bool result = false;
using (SMTP_Client client = new SMTP_Client())
{
try
{
//匹配使之可以用于其它郵箱
string smtp = "smtp.qq.com";
int atIndex = user.IndexOf("@");
if (atIndex > 0)
{
smtp = "smtp." + user.Substring(atIndex + 1);
}
if(user.Contains("@163.com"))
{
user = user.Replace("@163.com", "");
}
client.Connect(smtp, WellKnownPorts.SMTP_SSL, true);
client.Authenticate(user, pass);
string helloText = client.GreetingText;
if (helloText.Contains("220"))
{
result = true;
}
}
catch (Exception ex)
{
;
}
}
return result;
}
private void SetTips(string tips)
{
CallCtrlWithThreadSafetyEx.SetText(this.lblOperateTips, tips);
CallCtrlWithThreadSafetyEx.SetText(this.tsslTipsLable, tips);
Application.DoEvents();
Thread.Sleep(20);
}
private void UpdateList()
{
try
{
this.lstFailed.Invoke(new MethodInvoker(delegate()
{
this.lstFailed.Items.Clear();
foreach (string item in failedList)
{
this.lstFailed.Items.Add(item);
}
this.lstFailed.Refresh();
}));
this.lstSuccess.Invoke(new MethodInvoker(delegate()
{
this.lstSuccess.Items.Clear();
foreach (string item in SuccessList)
{
this.lstSuccess.Items.Add(item);
}
this.lstSuccess.Refresh();
}));
}
catch (Exception ex)
{
LogTextHelper.WriteLine(ex.ToString());
}
}
其中利用了Lumisoft.NET的SMTP_Client對象來檢測是否服務(wù)器應(yīng)答的內(nèi)容,如果含有220就表示支持SMTP了,呵呵,是不是很簡單呢,其實(shí)上面的代碼最為重要的就是下面這部分而已。
using (SMTP_Client client = new SMTP_Client())
{
try
{
//匹配使之可以用于其它郵箱
string smtp = "smtp.qq.com";
int atIndex = user.IndexOf("@");
if (atIndex > 0)
{
smtp = "smtp." + user.Substring(atIndex + 1);
}
if(user.Contains("@163.com"))
{
user = user.Replace("@163.com", "");
}
client.Connect(smtp, WellKnownPorts.SMTP_SSL, true);
client.Authenticate(user, pass);
string helloText = client.GreetingText;
if (helloText.Contains("220"))
{
result = true;
}
}
catch (Exception ex)
{
;
}
}
其他部分就是為了實(shí)現(xiàn)一個循環(huán)機(jī)批量的過程而已。本隨便介紹的功能,都已經(jīng)集成的我的軟件QQ搜通天系列軟件中了,需要了解可以下載軟件測試下,有寶貴意見可以相互溝通交流。