本文介绍两个QQ应用中有意思的小功能,都是基于邮件对象实现的功能,一个是利用.NET 内置类MailMessage来实现日志发送功能,一个是利用Lumisoft.NET组件类来检测用户是否开通了SMTP功能。
想看看两个功能的实现效果。
编辑并发送QQ日志的代码很简单,就是简单利用MailMessage对象来实现的,类似发送邮件一样,详细代码如下所示:
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留言发表成功! " );
this .txtTitle.Clear();
this .txtContent.Clear();
}
catch (Exception ex)
{
LogTextHelper.WriteLine(ex.ToString());
MessageExUtil.ShowError(ex.Message);
}
{
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留言发表成功! " );
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( " 已经是最后一个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); // 移除成功的数据
}
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());
}
}
{
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( " 已经是最后一个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); // 移除成功的数据
}
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对象来检测是否服务器应答的内容,如果含有220就表示支持SMTP了,呵呵,是不是很简单呢,其实上面的代码最为重要的就是下面这部分而已。
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)
{
;
}
}
{
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)
{
;
}
}
其他部分就是为了实现一个循环机批量的过程而已。本随便介绍的功能,都已经集成的我的软件QQ搜通天系列软件中了,需要了解可以下载软件测试下,有宝贵意见可以相互沟通交流。