Utilisation du système;
Utilisation de System.Text;
Utilisation de System.io;
Utilisation de System.net;
Utilisation de System.net.Sockets;
espace de noms sang.com.classlib
{
/// <résumé>
/// tcpclient 派生类 , 用来进行 Smtp 服务器的连接工作
/// </summary>
classe publique SMTPClient: TCPClient
{
public bool isConned ()
{
retour actif;
}
public void SendCommandToServer (String Commande)
{
NetworkStream ns = this.getStream ();
octet [] writeBuffer;
writeBuffer = nouveau octet [1024];
writeBuffer = coding.default.getBytes (commande);
ns.write (writeBuffer, 0, writeBuffer.length);
retour;
}
public String getServerResponse ()
{
int streamSize;
String returnValue =;
octet [] readbuffer;
NetworkStream ns = this.getStream ();
readbuffer = new octet [1024];
streamSize = ns.read (readbuffer, 0, readbuffer.length);
if (streamSize == 0)
{
retour returnValue;
}
autre
{
returnValue = coding.default.getString (readbuffer);
RETOUR-RETOURVALUX;
}
}
public bool doessStringContainsmtpcode (String S, String smtpcode)
{
return (s.indexof (smtpcode, 0,10) == - 1)? false: true;
}
} // 结束类
/// <résumé>
/// 发送邮件类
/// </summary>
classe publique smtpmail
{
/// <résumé>
/// 错误反馈信息
/// </summary>
String privé STRERRMESSAGE = NULL;
/// <résumé>
/// smtp 服务器反馈的信息
/// </summary>
String Private StrResponse;
/// <résumé>
/// 构造函数
/// </summary>
public smtpmail ()
{
StrerrMessage =;
strresponse =;
}
/// <résumé>
/// 取得错误反馈信息
/// </summary>
ErrorMessage de chaîne publique
{
obtenir
{
Retour StrgerMessage;
}
}
/// <résumé>
/// 取得 smtp 服务器反馈的信息
/// </summary>
Public String ServerResponse
{
obtenir
{
Retour Sttrresponse;
}
}
/// <résumé>
/// 邮件发送优先级
/// </summary>
priorités d'énumération publiques
{
/// <résumé>
/// 最高级别
/// </summary>
élevé = 1,
/// <résumé>
/// 默认级别
/// </summary>
normal = 3,
/// <résumé>
/// 最低级别
/// </summary>
bas = 5
}
public void Sendmail (String smtPhost, int, string from, string affichefromName, string to, string displayToname, prioritys priority, bool html, string base, string suject, string message)
{
essayer
{
String SttrSResponSenumber;
smtpClient smtpcmail = new smtpClient ();
smtpcmail.connect (smtphost, port);
bool bolconnect = smtpcmail.isconnected ();
// 判断是否进行了连接
if (! bolconnect)
{
STRERRMESSAGE = SMTP 服务器连接失败 ...;
retour;
}
// 读取反馈信息
strArSponSenumber = smtpcmail.getServerResponse ();
if (smtpcmail.doesStringContainsmtpcode (strAsponSenumber, 220))
{
this.strResponse + = strResponSenumber;
}
autre
{
this.StrerrMessage = 连接失败 + strAsponSenumber;
retour ;
}
String [] strSendBuffer = new String [6];
String [] strAsponSECODE = {220,250,251,354 221}; // codes de réussite du serveur SMTP
String strdata =;
strdata = string.concat (heLo, smtphost);
strdata = string.concat (strdata, / r / n);
strSendBuffer [0] = strdata;
strdata =;
strdata = string.concat (mail from :, <+ from +>);
strdata = string.concat (strdata, / r / n);
strsendbuffer [1] = strdata;
strdata =;
strdata = string.concat (rcpt to:, <+ to +>);
strdata = string.concat (strdata, / r / n);
strsendbuffer [2] = strdata;
strdata =;
strdata = string.concat (data, / r / n);
strsendbuffer [3] = strdata;
strdata =;
strdata = string.concat (de:, affichefromName + <+ from +>);
strdata = string.concat (strdata, / r / n);
strdata = string.concat (strdata, to:);
strdata = string.concat (strdata, displaytoname + <+ to +>);
strdata = string.concat (strdata, / r / n);
strdata = string.concat (strdata, sujet :);
strdata = string.concat (strdata, sujet);
strdata = string.concat (strdata, / r / n);
strdata = string.concat (strdata, mime-version: 1.0);
strdata = string.concat (strdata, / r / n);
strdata = string.concat (strdata, x priority: + priority);
strdata = string.concat (strdata, / r / n);
strdata = string.concat (strdata, x-msmail-priority: + priority);
strdata = string.concat (strdata, / r / n);
if (html == true)
{
strdata = string.concat (strdata, contenu-type: text / html;);
}
autre
{
strdata = string.concat (strdata, contenu-type: text / plain;);
}
strdata = string.concat (strdata, / r / n);
strdata = string.concat (strdata, charset = / iso-8859-1 /);
strdata = string.concat (strdata, / r / n);
if (html == true)
{
strdata = string.concat (strdata, contenu-transfer-coding: text / html;);
}
autre
{
strdata = string.concat (strdata, contenu-transfert-codage: text / plaine;);
}
strdata = string.concat (strdata, / r / n);
strdata = string.concat (strdata, contenu-base: / + base + /);
strdata = string.concat (strdata, / r / n + / r / n);
strdata = string.concat (strdata, message);
strdata = string.concat (strdata, / r / n. / r / n);
strsendbuffer [4] = strdata;
strdata =;
strdata = string.concat (strdata, quit / r / n);
strsendbuffer [5] = strdata;
int i = 0;
while (i <strsendbuffer.length)
{
smtpcmail.sendCommandToServer (strSendBuffer [i]);
strArSponSenumber = smtpcmail.getServerResponse ();
pour (int j = 0; j <strResponSEcode.length; j ++)
{
if (smtpcmail.doesStringContainsmtpcode (strArSponSenumber, strAsPonSECODE [J]))
{
this.strResponse + = strResponSenumber;
this.strResponse + = <br>;
casser;
}
autre
{
if (j == strAsponSECODE.Length-1)
{
this.StrerrMessage + = strSResponSenumber;
this.StrerrMessage + = strSendBuffer [i];
retour;
}
}
}
i ++;
} // 结束循环
}
Catch (socketException err)
{
this.StrerrMessage + = err.message + + err.stackTrace;
}
Catch (exception e)
{
this.StrerrMessage + = E.Message + + E.StackTrace;
}
} // 结束邮件发送方法
} // 结束类
} // 结束 Espace de noms