用c#写的smtp邮件发送类__教程 |
|
日期:2007-5-20 1:21:25 人气:114 [大 中 小] |
|
|
|
message+="--=====001_Dragon255511664284_====="+CRLF; message+="Content-Type:text/html;"+CRLF+" ".PadRight(8,' ')+"charset=\""+mail.MailEncoding.ToString().ToLower()+"\""+CRLF; message+="Content-Transfer-Encoding:base64"+CRLF+CRLF; if(mail.MailBody!=null) message+=Convert.ToBase64String(mail.MailBody,0,mail.MailBody.Length)+CRLF+CRLF; for(int i=0;i<attatchmentDatas.Count;i++) { message+="--=====001_Dragon255511664284_====="+CRLF+attatchmentDatas[i]+CRLF+CRLF; } message+="--=====001_Dragon255511664284_=====--"+CRLF+CRLF+CRLF+"."+CRLF; } } //发送邮件数据 this.mailEncodingName=mail.MailEncoding.ToString(); this.sendCommand(message,true); if(this.sendIsComplete) this.sendCommand("QUIT",false); } catch { isSended=false; } finally { this.disconnect(); //输出日志文件 if(this.LogPath!=null) { FileStream fs=null; if(File.Exists(this.LogPath)) { fs=new FileStream(this.LogPath,FileMode.Append,FileAccess.Write); this.logs=CRLF+CRLF+this.logs; } else fs=new FileStream(this.LogPath,FileMode.Create,FileAccess.Write); byte[] logPath_b=Encoding.GetEncoding("gb2312").GetBytes(this.logs); fs.Write(logPath_b,0,logPath_b.Length); fs.Close(); } } return isSended; } /// <summary> /// 异步写入数据 /// </summary> /// <param name="result"></param> private void asyncCallBack(IAsyncResult result) { if(result.IsCompleted) this.sendIsComplete=true; } /// <summary> /// 关闭连接 /// </summary> private void disconnect() { try { ns.Close(); tc.Close(); } catch { ; } } /// <summary> /// 设置出现错误时的动作 /// </summary> /// <param name="errorStr"></param> private void setError(string errorStr) { this.errorMessage=errorStr; logs+=this.errorMessage+CRLF+"【邮件处理动作中止】"+CRLF; this.disconnect(); throw new ApplicationException(""); } /// <summary> ///将字符串转换为base64 /// </summary> /// <param name="str"></param> /// <param name="encodingName"></param> /// <returns></returns> private string convertBase64String(string str,string encodingName) { byte[] str_b=Encoding.GetEncoding(encodingName).GetBytes(str); return Convert.ToBase64String(str_b,0,str_b.Length); } #endregion } } |
|
出处:本站原创 作者:佚名 |
|
|