mit System verwenden;
mit System.text;
mit System.io;
mit System.net;
mit System.net.sockets;
Namespace Blood.com.Classlib
{
/// <summary>
/// tcpclient 派生类 , 用来进行 SMTP 服务器的连接工作
/// </summary>
öffentliche Klasse SMTPClient: TCPClient
{
public bool isconnected ()
{
aktiv zurückkehren;
}
public void sendCommandtoServer (String -Befehl)
{
networkStream ns = this.getStream ();
byte [] writeBuffer;
writeBuffer = neues Byte [1024];
writeBuffer = coding.default.getBytes (Befehl);
Ns.Write (writeBuffer, 0, writeBuffer.length);
zurückkehren;
}
public String getServerResponse ()
{
int streamSize;
String returnValue =;
Byte [] ReadBuffer;
networkStream ns = this.getStream ();
ReadBuffer = neues Byte [1024];
streamSize = ns.read (ReadBuffer, 0, ReadBuffer.length);
if (streamSize == 0)
{
return returnValue;
}
anders
{
returnValue = coding.default.getString (ReadBuffer);
returnReturnValue;
}
}
public bool DoessstringContainsMTPCode (String S, String SMTPCode)
{
return (S.Indexof (SMTPCode, 0,10) ==-1)? Falsch: Richtig;
}
} // 结束类
/// <summary>
/// 发送邮件类
/// </summary>
öffentliche Klasse SMTPMAIL
{
/// <summary>
/// 错误反馈信息
/// </summary>
private String strerrmessage = null;
/// <summary>
/// SMTP 服务器反馈的信息
/// </summary>
private String Strpresponse;
/// <summary>
/// 构造函数
/// </summary>
öffentliche SMTPMail ()
{
Strerrmessage =;
Strresponse =;
}
/// <summary>
/// 取得错误反馈信息
/// </summary>
öffentliche String -Errormessage
{
erhalten
{
return Strerrmessage;
}
}
/// <summary>
/// 取得 SMTP 服务器反馈的信息
/// </summary>
öffentliche String ServerResponse
{
erhalten
{
return Strresponse;
}
}
/// <summary>
/// 邮件发送优先级
/// </summary>
Prioritäten der Öffentlichkeit
{
/// <summary>
/// 最高级别
/// </summary>
hoch = 1,,
/// <summary>
/// 默认级别
/// </summary>
Normal = 3,
/// <summary>
/// 最低级别
/// </summary>
niedrig = 5
}
public void sendmail (String SMTPHOST, INT -Port, String von, String displayFromName, String to, String DisplayToname, Priorität Priorität, Bool HTML, String -Basis, String -Betreff, String -Nachricht)
{
versuchen
{
String Strresponsenumber;
SMTPCLIENT SMTPCMAIL = new SMTPClient ();
SMTPCMAIL.CONNECT (SMTPHOST, Port);
bool bolconnect = SMTPCMail.isconnected ();
// 判断是否进行了连接
if (! bolconnect)
{
Strerrmessage = SMTP 服务器连接失败 ...;
zurückkehren;
}
// 读取反馈信息
Strresponsenumber = SMTPCMAIL.getServerResponse ();
if (SMTPCMAIL.DOESSTRINGINGCONTINGAINSMTPCODE (strlesponsenumber, 220))
{
this.strPResponse += strlesponsenumber;
}
anders
{
this.strerrmessage = 连接失败 + strlesponsenumber;
zurückkehren ;
}
String [] strsendbuffer = new String [6];
String [] strlesponsecode = {220,250,251,354,221}; // Erfolgscodes vom SMTP -Server
String strdata =;
strdata = string.concat (Helo, SMTPhost);
strdata = string.concat (strdata,/r/n);
StrsendBuffer [0] = strdata;
strdata =;
strdata = string.concat (Mail von:, < + von +>);
strdata = string.concat (strdata,/r/n);
StrsendBuffer [1] = strdata;
strdata =;
strdata = string.concat (rcpt to:, < + bis +>);
strdata = string.concat (strdata,/r/n);
StrsendBuffer [2] = strdata;
strdata =;
strdata = string.concat (Daten,/r/n);
StrsendBuffer [3] = strdata;
strdata =;
strdata = string.concat (aus:, displayFromName + < + von +>);
strdata = string.concat (strdata,/r/n);
strdata = string.concat (strdata, to :);
strdata = string.concat (strdata, displayToname + < + bis +>);
strdata = string.concat (strdata,/r/n);
strdata = string.concat (strdata, Subjekt :);
strdata = string.concat (strdata, Subjekt);
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-teurity: + priority);
strdata = string.concat (strdata,/r/n);
strdata = string.concat (strdata, x-msmail-priorität: + priority);
strdata = string.concat (strdata,/r/n);
if (html == true)
{
strdata = string.concat (strdata, content-type: text/html;);
}
anders
{
strdata = string.concat (strdata, content-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, content-transfer-kodierender: text/html;);
}
anders
{
strdata = string.concat (strdata, content-transfer-kodierender: text/plain;);
}
strdata = string.concat (strdata,/r/n);
strdata = string.concat (strdata, content-base: / + base + /);
strdata = string.concat (strdata,/r/n +/r/n);
strdata = string.concat (strdata, meldung);
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;
während (i <strsendbuffer.length)
{
SMTPCMAIL.SendCommandtoServer (StrsendBuffer [i]);
Strresponsenumber = SMTPCMAIL.getServerResponse ();
für (int j = 0; j <strlesponsecode.length; j ++)
{
if (SMTPCMAIL.DoesStringContainsMTPCode (strlesponsenumber, strlesponsecode [j]))
{
this.strPResponse += strlesponsenumber;
this.strResponse += <br>;
brechen;
}
anders
{
if (j == strlesponsecode.length-1)
{
this.strerrmessage += strlesponsenumber;
this.strerrmessage += strsendBuffer [i];
zurückkehren;
}
}
}
i ++;
} // 结束循环
}
fangen (SocketException err)
{
this.strerrmessage + = err.message + + err.stacktrace;
}
fangen (Ausnahme E)
{
this.strerrmessage + = e.Message + + E.Stacktrace;
}
} // 结束邮件发送方法
} // 结束类
} // 结束 Namespace