用c#写的smtp邮件发送类__教程 |
|
日期:2007-5-20 1:21:25 人气:114 [大 中 小] |
|
|
|
return response; } else { return null; } } /// <summary> /// 通过auth login方式登陆smtp服务器 /// </summary> private void landingByLogin() { string base64UserId=this.convertBase64String(this.UserID,"ASCII"); string base64Pass=this.convertBase64String(this.Password,"ASCII"); //握手 this.sendCommand("helo "+this.serverName,false); //开始登陆 this.sendCommand("auth login",false); this.sendCommand(base64UserId,false); this.sendCommand(base64Pass,false); } /// <summary> /// 通过auth plain方式登陆服务器 /// </summary> private void landingByPlain() { string NULL=((char)0).ToString(); string loginStr=NULL+this.UserID+NULL+this.Password; string base64LoginStr=this.convertBase64String(loginStr,"ASCII"); //握手 this.sendCommand("helo "+this.serverName,false); //登陆 this.sendCommand(base64LoginStr,false); } /// <summary> /// 通过auth CRAM-MD5方式登陆 /// </summary> private void landingByCRAMMD5() { //握手 this.sendCommand("helo "+this.serverName,false); //登陆 string response=this.sendCommand("auth CRAM-MD5",false); //得到服务器返回的标识 string identifier=response.Remove(0,4); //用MD5加密标识 KSN_MACTripleDES mac=new KSN_MACTripleDES(); mac.Key=this.Password; mac.Data=Encoding.ASCII.GetBytes(identifier); string hash=mac.GetHashValue(); //发送用户帐号信息 this.sendCommand(this.UserID+" "+hash,false); } /// <summary> /// 发送邮件 /// </summary> /// <returns></returns> public bool SendMail(MailMessage mail) { bool isSended=true; try { //检测发送邮件的必要条件 if(mail.Sender==null) { this.setError("没有设置发信人"); } if(mail.Receivers.Count==0) { this.setError("至少要有一个收件人"); } if(this.SmtpValidateType!=SmtpValidateTypes.None) { if(this.userid==null || this.password==null) { this.setError("当前设置需要smtp验证,但是没有给出登陆帐号"); } } //开始登陆 switch(this.SmtpValidateType) { case SmtpValidateTypes.None: this.sendCommand("helo "+this.serverName,false); break; case SmtpValidateTypes.Login: this.landingByLogin(); break; case SmtpValidateTypes.Plain: this.landingByPlain(); break; case SmtpValidateTypes.CRAMMD5: this.landingByCRAMMD5(); break; default: break; } //初始化邮件会话(对应SMTP命令mail) this.sendCommand("mail from:<"+mail.Sender+">",false); //标识收件人(对应SMTP命令Rcpt) foreach(string receive in mail.Receivers) { this.sendCommand("rcpt to:<"+receive+">",false); } //标识开始输入邮件内容(Data) this.sendCommand("data",false); //开始编写邮件内容 string message="Subject:"+mail.Subject+CRLF; message+="X-mailer:"+mail.XMailer+CRLF; message+="MIME-Version:1.0"+CRLF; if(mail.Attachments.Count==0)//没有附件 { if(mail.MailType==MailTypes.Text) //文本格式 { message+="Content-Type:text/plain;"+CRLF+" ".PadRight(8,' ')+"charset=\""+ |
|
出处:本站原创 作者:佚名 |
|
|