📄 server.cs
字号:
}
return true;
}
/// <summary>
/// 接收SMTP服务器回应
/// </summary>
private string RecvResponse() {
int StreamSize;
string Returnvalue = String.Empty;
byte[] ReadBuffer = new byte[1024] ;
try {
StreamSize=networkStream.Read(ReadBuffer,0,ReadBuffer.Length);
}
catch {
errmsg="网络连接错误";
return "false";
}
if (StreamSize==0) {
return Returnvalue ;
}
else {
Returnvalue = Encoding.Default.GetString(ReadBuffer).Substring(0,StreamSize);
logs+=Returnvalue+this.CRLF;
return Returnvalue;
}
}
/// <summary>
/// 与服务器交互,发送一条命令并接收回应。
/// </summary>
/// <param name="str">一个要发送的命令</param>
/// <param name="errstr">如果错误,要反馈的信息</param>
private bool Dialog(string str,string errstr) {
if(str==null||str.Trim()==string.Empty) {
return true;
}
if(SendCommand(str)) {
string RR=RecvResponse();
if(RR=="false") {
return false;
}
//检查返回的代码,根据[RFC 821]返回代码为3位数字代码如220
string RRCode=RR.Substring(0,3);
if(RightCodeHT[RRCode]!=null) {
return true;
}
else {
if(ErrCodeHT[RRCode]!=null) {
errmsg+=(RRCode+ErrCodeHT[RRCode].ToString());
errmsg+=CRLF;
}else {
errmsg+=RR;
}
errmsg+=errstr;
return false;
}
}else {
return false;
}
}
/// <summary>
/// 与服务器交互,发送一组命令并接收回应。
/// </summary>
private bool Dialog(string[] str,string errstr) {
for(int i=0;i<str.Length;i++) {
if(!Dialog(str[i],"")) {
errmsg+=CRLF;
errmsg+=errstr;
return false;
}
}
return true;
}
//连接服务器
private bool Connect(string smtpServer,int port) {
//创建Tcp连接
try {
tcpClient=new TcpClient(smtpServer,port);
}
catch(Exception e) {
errmsg=e.ToString();
return false;
}
networkStream=tcpClient.GetStream();
//验证网络连接是否正确
if(RightCodeHT[RecvResponse().Substring(0,3)]==null) {
errmsg="网络连接失败";
return false;
}
return true;
}
private string GetPriorityString(MailPriority mailPriority) {
string priority="Normal";
if (mailPriority==MailPriority.Low) {
priority="Low";
}
else if (mailPriority==MailPriority.High) {
priority="High";
}
return priority;
}
/// <summary>
/// 发送电子邮件,SMTP服务器不需要身份验证
/// </summary>
/// <param name="smtpServer"></param>
/// <param name="port"></param>
/// <param name="mailMessage"></param>
/// <returns></returns>
public bool SendEmail(string smtpServer,int port,MailMessage mailMessage) {
return SendEmail(smtpServer,port,false,"","",mailMessage);
}
/// <summary>
/// 发送电子邮件,SMTP服务器需要身份验证
/// </summary>
/// <param name="smtpServer"></param>
/// <param name="port"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="mailMessage"></param>
/// <returns></returns>
public bool SendEmail(string smtpServer,int port,string username,string password,MailMessage mailMessage) {
return SendEmail(smtpServer,port,false,username,password,mailMessage);
}
private bool SendEmail(string smtpServer,int port,bool ESmtp,string username,string password,MailMessage mailMessage) {
if (Connect(smtpServer,port)==false)//测试连接服务器是否成功
return false;
string priority=GetPriorityString(mailMessage.Priority);
bool Html=(mailMessage.BodyFormat==MailFormat.HTML);
string[] SendBuffer;
string SendBufferstr;
//进行SMTP验证,现在大部分SMTP服务器都要认证
if(ESmtp) {
SendBuffer=new String[4];
SendBuffer[0]="EHLO " + smtpServer + CRLF;
SendBuffer[1]="AUTH LOGIN" + CRLF;
SendBuffer[2]=Base64Encode(username) + CRLF;
SendBuffer[3]=Base64Encode(password) + CRLF;
if(!Dialog(SendBuffer,"SMTP服务器验证失败,请核对用户名和密码。"))
return false;
}else {
//不需要身份认证
SendBufferstr="HELO " + smtpServer + CRLF;
if(!Dialog(SendBufferstr,""))
return false;
}
//发件人地址
SendBufferstr="MAIL FROM:<" + mailMessage.From + ">" + CRLF;
if(!Dialog(SendBufferstr,"发件人地址错误,或不能为空"))
return false;
//收件人地址
SendBuffer=new string[mailMessage.Recipients.Count];
for(int i=0;i<mailMessage.Recipients.Count;i++) {
SendBuffer[i]="RCPT TO:<" +(string)mailMessage.Recipients[i] +">" + CRLF;
}
if(!Dialog(SendBuffer,"收件人地址有误"))
return false;
/*
SendBuffer=new string[10];
for(int i=0;i<RecipientBCC.Count;i++)
{
SendBuffer[i]="RCPT TO:<" + RecipientBCC[i].ToString() +">" + CRLF;
}
if(!Dialog(SendBuffer,"密件收件人地址有误"))
return false;
*/
SendBufferstr="DATA" + CRLF;
if(!Dialog(SendBufferstr,""))
return false;
//发件人姓名
SendBufferstr="From:" + mailMessage.FromName + "<" +mailMessage.From +">" +CRLF;
//if(ReplyTo.Trim()!="")
//{
// SendBufferstr+="Reply-To: " + ReplyTo + CRLF;
//}
//SendBufferstr+="To:" + ToName + "<" + Recipient[0] +">" +CRLF;
//至少要有一个收件人
if (mailMessage.Recipients.Count==0) {
return false;
}
else {
SendBufferstr += "To:=?"+mailMessage.Charset.ToUpper()+"?B?"+
Base64Encode((string)mailMessage.Recipients[0])+"?="+"<"+(string)mailMessage.Recipients[0]+">"+CRLF;
}
//SendBufferstr+="CC:";
//for(int i=0;i<Recipient.Count;i++)
//{
// SendBufferstr+=Recipient[i].ToString() + "<" + Recipient[i].ToString() +">,";
//}
//SendBufferstr+=CRLF;
SendBufferstr+=
((mailMessage.Subject==String.Empty || mailMessage.Subject==null)?"Subject:":((mailMessage.Charset=="")?("Subject:" +
mailMessage.Subject):("Subject:" + "=?" + mailMessage.Charset.ToUpper() + "?B?" +
Base64Encode(mailMessage.Subject) +"?="))) + CRLF;
SendBufferstr+="X-Priority:" + priority + CRLF;
SendBufferstr+="X-MSMail-Priority:" + priority + CRLF;
SendBufferstr+="Importance:" + priority + CRLF;
SendBufferstr+="X-Mailer: Lion.Web.Mail.SmtpMail Pubclass [cn]" + CRLF;
SendBufferstr+="MIME-Version: 1.0" + CRLF;
if(mailMessage.Attachments.Count!=0) {
SendBufferstr+="Content-Type: multipart/mixed;" + CRLF;
SendBufferstr += " boundary=\"====="+
(Html?"001_Dragon520636771063_":"001_Dragon303406132050_")+"=====\""+CRLF+CRLF;
}
if(Html) {
if(mailMessage.Attachments.Count==0) {
SendBufferstr += "Content-Type: multipart/alternative;"+CRLF;//内容格式和分隔符
SendBufferstr += " boundary=\"=====003_Dragon520636771063_=====\""+CRLF+CRLF;
SendBufferstr += "This is a multi-part message in MIME format."+CRLF+CRLF;
}else {
SendBufferstr +="This is a multi-part message in MIME format."+CRLF+CRLF;
SendBufferstr += "--=====001_Dragon520636771063_====="+CRLF;
SendBufferstr += "Content-Type: multipart/alternative;"+CRLF;//内容格式和分隔符
SendBufferstr += " boundary=\"=====003_Dragon520636771063_=====\""+CRLF+CRLF;
}
SendBufferstr += "--=====003_Dragon520636771063_====="+CRLF;
SendBufferstr += "Content-Type: text/plain;"+ CRLF;
SendBufferstr += ((mailMessage.Charset=="")?(" charset=\"iso-8859-1\""):(" charset=\"" +
mailMessage.Charset.ToLower() + "\"")) + CRLF;
SendBufferstr+="Content-Transfer-Encoding: base64" + CRLF + CRLF;
SendBufferstr+= Base64Encode("邮件内容为HTML格式,请选择HTML方式查看") + CRLF + CRLF;
SendBufferstr += "--=====003_Dragon520636771063_====="+CRLF;
SendBufferstr+="Content-Type: text/html;" + CRLF;
SendBufferstr+=((mailMessage.Charset=="")?(" charset=\"iso-8859-1\""):(" charset=\"" +
mailMessage.Charset.ToLower() + "\"")) + CRLF;
SendBufferstr+="Content-Transfer-Encoding: base64" + CRLF + CRLF;
SendBufferstr+= Base64Encode(mailMessage.Body) + CRLF + CRLF;
SendBufferstr += "--=====003_Dragon520636771063_=====--"+CRLF;
}else {
if(mailMessage.Attachments.Count!=0) {
SendBufferstr += "--=====001_Dragon303406132050_====="+CRLF;
}
SendBufferstr+="Content-Type: text/plain;" + CRLF;
SendBufferstr+=((mailMessage.Charset=="")?(" charset=\"iso-8859-1\""):(" charset=\"" +
mailMessage.Charset.ToLower() + "\"")) + CRLF;
SendBufferstr+="Content-Transfer-Encoding: base64" + CRLF + CRLF;
SendBufferstr+= Base64Encode(mailMessage.Body) + CRLF;
}
//SendBufferstr += "Content-Transfer-Encoding: base64"+CRLF;
if(mailMessage.Attachments.Count!=0) {
for(int i=0;i<mailMessage.Attachments.Count;i++) {
string filepath = (string)mailMessage.Attachments[i];
SendBufferstr += "--====="+
(Html?"001_Dragon520636771063_":"001_Dragon303406132050_") +"====="+CRLF;
//SendBufferstr += "Content-Type: application/octet-stream"+CRLF;
SendBufferstr += "Content-Type: text/plain;"+CRLF;
SendBufferstr += " name=\"=?"+mailMessage.Charset.ToUpper()+"?B?"+
Base64Encode(filepath.Substring(filepath.LastIndexOf("\\")+1))+"?=\""+CRLF;
SendBufferstr += "Content-Transfer-Encoding: base64"+CRLF;
SendBufferstr += "Content-Disposition: attachment;"+CRLF;
SendBufferstr += " filename=\"=?"+mailMessage.Charset.ToUpper()+"?B?"+
Base64Encode(filepath.Substring(filepath.LastIndexOf("\\")+1))+"?=\""+CRLF+CRLF;
SendBufferstr += GetStream(filepath)+CRLF+CRLF;
}
SendBufferstr += "--====="+
(Html?"001_Dragon520636771063_":"001_Dragon303406132050_")+"=====--"+CRLF+CRLF;
}
SendBufferstr += CRLF + "." + CRLF;//内容结束
if(!Dialog(SendBufferstr,"错误信件信息"))
return false;
SendBufferstr="QUIT" + CRLF;
if(!Dialog(SendBufferstr,"断开连接时错误"))
return false;
networkStream.Close();
tcpClient.Close();
return true;
}
}
public class SmtpMail {
private static string _SmtpServer;
/// <summary>
/// 格式:SmtpAccount:Password@SmtpServerAddress<br>
/// 或者:SmtpServerAddress<br>
/// <code>
/// SmtpMail.SmtpServer="user:12345678@smtp.126.com";
/// //或者:
/// SmtpMail.SmtpServer="smtp.126.com";
/// 或者:
/// SmtpMail.SmtpServer=SmtpServerHelper.GetSmtpServer("user","12345678","smtp.126.com");
/// </code>
/// </summary>
public static string SmtpServer {
set { _SmtpServer=value;}
get { return _SmtpServer;}
}
public static bool Send(MailMessage mailMessage,string username,string password) {
SmtpServerHelper helper=new SmtpServerHelper();
return helper.SendEmail(_SmtpServer,25,username,password,mailMessage);
}
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -