باستخدام النظام ؛
باستخدام system.text ؛
باستخدام System.io ؛
باستخدام system.net ؛
باستخدام system.net.sockets ؛
مساحة الاسم Blood.com.classlib
{
/// <summary>
/// tcpclient 派生类 , 用来进行 smtp 服务器的连接工作
/// </summary>
الطبقة العامة smtpclient: tcpclient
{
Bool Public Isconnected ()
{
العودة نشطة.
}
الفراغ العام sendCommandToServer (أمر سلسلة)
{
networkstream ns = this.getStream () ؛
بايت [] trintbuffer ؛
WriteBuffer = New Byte [1024] ؛
liteBuffer = encoding.default.getBytes (command) ؛
ns.write (trintbuffer ، 0 ، writebuffer.length) ؛
يعود؛
}
السلسلة العامة getServerResponse ()
{
int تدفقات
سلسلة returnvalue = ؛
بايت [] readbuffer ؛
networkstream ns = this.getStream () ؛
readbuffer = new byte [1024] ؛
StreamSize = ns.read (readbuffer ، 0 ، readbuffer.length) ؛
إذا (تدفق == 0)
{
إرجاع القيمة ؛
}
آخر
{
returnValue = encoding.default.getString (readbuffer) ؛
ReturnReturnValue ؛
}
}
BOOL BOOL DOESSTRINGCONTINSMTPCODE (سلسلة S ، سلسلة SMTPCode)
{
Return (S.Indexof (SmtpCode ، 0،10) ==-1)؟ false: true ؛
}
} // 结束类
/// <summary>
/// 发送邮件类
/// </summary>
الطبقة العامة smtpmail
{
/// <summary>
/// 错误反馈信息
/// </summary>
سلسلة خاصة strerrmessage = فارغة ؛
/// <summary>
/// SMTP 服务器反馈的信息
/// </summary>
سلسلة خاصة
/// <summary>
/// 构造函数
/// </summary>
smtpmail العامة ()
{
strerrmessage = ؛
strresponse = ؛
}
/// <summary>
/// 取得错误反馈信息
/// </summary>
خيط السلسلة العامة
{
يحصل
{
إرجاع strerrmessage.
}
}
/// <summary>
/// 取得 smtp 服务器反馈的信息
/// </summary>
السلسلة العامة ServerResponse
{
يحصل
{
إرجاع strresponse.
}
}
/// <summary>
/// 邮件发送优先级
/// </summary>
أولوية التعداد العام
{
/// <summary>
/// 最高级别
/// </summary>
عالية = 1 ،
/// <summary>
/// 默认级别
/// </summary>
عادي = 3 ،
/// <summary>
/// 最低级别
/// </summary>
منخفض = 5
}
sendmail public void (String smtphost ، منفذ int ، السلسلة من ، سلسلة displayfromname ، سلسلة إلى ، string displaytoname ، أولوية الأولوية ، bool html ، قاعدة السلسلة ، موضوع السلسلة ، رسالة السلسلة)
{
يحاول
{
سلسلة strresponsenumber.
smtpclient smtpcmail = new smtpclient () ؛
smtpcmail.connect (smtphost ، port) ؛
bool bolconnect = smtpcmail.isconnected () ؛
// 判断是否进行了连接
إذا (! bolconnect)
{
strerrmessage = smtp 服务器连接失败 ... ؛
يعود؛
}
// 读取反馈信息
strresponsenumber = smtpcmail.getServerResponse () ؛
if (smtpcmail.doesstringContainsMtpCode (Strresponsenumber ، 220))
{
this.strresponse += strresponsenumber ؛
}
آخر
{
this.strerrmessage = 连接失败 + strresponsenumber ؛
يعود ؛
}
String [] StrSendBuffer = سلسلة جديدة [6] ؛
String [] strresponsecode = {220،250،251،354،221} ؛ // رموز النجاح من SMTP Server
سلسلة strdata = ؛
strdata = string.concat (helo ، smtphost) ؛
strdata = string.concat (strdata ،/r/n) ؛
strsendbuffer [0] = strdata ؛
strdata = ؛
strdata = string.concat (البريد من: ، < + من +>) ؛
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 (البيانات ،/r/n) ؛
strsendbuffer [3] = strdata ؛
strdata = ؛
strdata = string.concat (من: ، displayfromName + < + 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 ، الموضوع :) ؛
strdata = string.concat (strdata ، الموضوع) ؛
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) ؛
إذا (html == صحيح)
{
strdata = string.concat (strdata ، نوع المحتوى: text/html ؛) ؛
}
آخر
{
strdata = string.concat (strdata ، نوع المحتوى: text/plain ؛) ؛
}
strdata = string.concat (strdata ،/r/n) ؛
strdata = string.concat (strdata ، charset =/ iso-8859-1/) ؛
strdata = string.concat (strdata ،/r/n) ؛
إذا (html == صحيح)
{
strdata = string.concat (strdata ، ترميز النقل للمحتوى: text/html ؛) ؛
}
آخر
{
strdata = string.concat (strdata ، ترميز النقل للمحتوى: 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 ، 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 ؛
بينما (أنا <strsendbuffer.length)
{
smtpcmail.sendCommandToserver (StrsendBuffer [i]) ؛
strresponsenumber = smtpcmail.getServerResponse () ؛
لـ (int j = 0 ؛ j <strresponsecode.length ؛ j ++)
{
if (smtpcmail.doesstringContainsMtpCode (strresponsenumber ، strresponsecode [j]))
{
this.strresponse += strresponsenumber ؛
this.strresponse += <br> ؛
استراحة؛
}
آخر
{
إذا (j == strresponsecode.length-1)
{
this.strerrmessage += strresponsenumber ؛
this.strerrmessage += strsendbuffer [i] ؛
يعود؛
}
}
}
i ++ ؛
} // 结束循环
}
Catch (SocketException err)
{
this.strerrmessage + = err.message + + err.stacktrace ؛
}
الصيد (استثناء ه)
{
this.strerrmessage + = E.Message + + E.StackTrace ؛
}
} // 结束邮件发送方法
} // 结束类
} // 结束 مساحة الاسم