1.实例:从qq邮箱 发送到其他地址的邮箱
>telnet smtp.qq.com 25
>helo qq.com
>auth login
>NzI3MTU0MTg3QHFxLmNvbQ== (邮箱名的base64编码 http://tool.chinaz.com/Tools/base64.aspx)
>。。。。。。。。。。。。。。。。。(邮箱密码的base64编码)
>mail from:727154187@qq.com
>rcpt to:jimei@outlook.com
>Data
From:727154187@qq.com
TO:jimei@outlook.com
Subject:test mail
. (注意最后以'.'结束)
[注]查找smtp 地址的命令: nslookup -type=mx [域名]
2.常用命令:
参数
|
作用
|
HELO
|
使用标准的SMTP,向服务器标识用户身份。发送者能进行欺骗,但一般情况下服务器都能检测到
|
EHLO
|
使用ESMTP,向服务器标识用户身份。发送者能进行欺骗,但一般情况下服务器都能检测到。
|
STARTTLS
|
启用TLS
|
MAIL FROM
|
命令中指定的地址是发件人地址
|
RCPT TO
|
标识单个的邮件接收人;可有多个 RCPT TO;常在 MAIL 命令后面
|
DATA
|
在单个或多个 RCPT 命令后,表示所有的邮件接收人已标识,并初始化数据传输,以CRLF.CRLF 结束
|
VRFY
|
用于验证指定的用户/邮箱是否存在;由于安全方面的原因,服务器常禁止此命令
|
EXPN
|
验证给定的邮箱列表是否存在,扩充邮箱列表,也常被禁用
|
HELP
|
查询服务器支持什么命令
|
NOOP
|
无操作,服务器响应 250 OK
|
RSET
|
重置会话,当前传输被取消,服务器响应 250 OK
|
QUIT
|
结束会话
|
3.常见错误码:
421 Service not available, closing transmission channel (This may be a reply to any command if the service knows it must shut down)
450 Requested mail action not taken: mailbox unavailable (E.g., mailbox busy)
451 Requested action aborted: local error in processing
452 Requested action not taken: insufficient system storage
500 Syntax error, command unrecognized (This may include errors such as command line too long)
501 Syntax error in parameters or arguments
502 Command not implemented
503 Bad sequence of commands
504 Command parameter not implemented
550 Requested action not taken: mailbox unavailable (E.g., mailbox not found, no access)
551 User not local; please try
552 Requested mail action aborted: exceeded storage allocation
553 Requested action not taken: mailbox name not allowed (E.g., mailbox syntax incorrect)
554 Transaction failedThe other codes that provide you with helpful information about what’s happening with your messages are:
211 System status, or system help reply
214 Help message (Information on how to use the receiver or the meaning of a particular non-standard command; this reply is useful
only to the human user)
220 Service ready
221 Service closing transmission channel
250 Requested mail action okay, completed
251 User not local; will forward to
354 Start mail input; end with . (a dot)
‘*************************
‘* 邮件服务返回代码含义
‘* 500 格式错误,命令不可识别(此错误也包括命令行过长)
‘* 501 参数格式错误
‘* 502 命令不可实现
‘* 503 错误的命令序列
‘* 504 命令参数不可实现
‘* 211 系统状态或系统帮助响应
‘* 214 帮助信息
‘* 220 服务就绪
‘* 221 服务关闭传输信道
‘* 421 服务未就绪,关闭传输信道(当必须关闭时,此应答可以作为对任何命令的响应)
‘* 250 要求的邮件操作完成
‘* 251 用户非本地,将转发向
‘* 450 要求的邮件操作未完成,邮箱不可用(例如,邮箱忙)
‘* 550 要求的邮件操作未完成,邮箱不可用(例如,邮箱未找到,或不可访问)
‘* 451 放弃要求的操作;处理过程中出错
‘* 551 用户非本地,请尝试
‘* 452 系统存储不足,要求的操作未执行
‘* 552 过量的存储分配,要求的操作未执行
‘* 553 邮箱名不可用,要求的操作未执行(例如邮箱格式错误)
‘* 354 开始邮件输入,以.结束
‘* 554 操作失败
‘* 535 用户验证失败
‘* 235 用户验证成功
‘* 334 等待用户输入验证信息
1 <%@ WebHandler Language="C#" Class="SendMailHandler" %> 2 3 using System; 4 using System.Linq; 5 using System.Web; 6 using System.Web.SessionState; 7 8 using System.Collections.Generic; 9 using System.Web.Script.Serialization; 10 11 12 using System.Text.RegularExpressions; 13 using System.Net; 14 using System.Net.Mail; 15 16 public class SendMailHandler : IHttpHandler 17 { 18 public void ProcessRequest(HttpContext context) 19 { 20 context.Response.ContentType = "text/plain"; 21 JavaScriptSerializer ser = new JavaScriptSerializer(); 22 var form=context.Request.Form; 23 string email =form["email"]??""; 24 25 bool status=false; 26 string msg=""; 27 string log=""; 28 try 29 { 30 31 32 var smtpSetting=new Smtp 33 { 34 Port=25 , 35 EnableSsl=false , 36 Host="smtp.qq.com" , 37 Password="******" , 38 UserName="727154187@qq.com" , 39 From="727154187@qq.com" , 40 To=new string[] { "jimei@outlook.com" } 41 }; 42 43 Regex reg = new Regex(@"^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$"); 44 45 if(smtpSetting!=null && !string.IsNullOrEmpty(email) && reg.IsMatch(email)) 46 { 47 48 var template=@"<div> 49 <p><b>Gender:</b> <span>$$gender$$</span></p> 50 <p><b>Name:</b> <span>$$name$$</span></p> 51 <p><b>Email:</b> <span>$$email$$</span></p> 52 <p><b>Topic:</b> <span>$$topic$$</span></p> 53 <p><b>Content:</b> <span>$$content$$</span></p></div>"; 54 var g=form["gender"]??"1"; 55 56 template=template.Replace("$$gender$$" ,g=="1"?"male":"female"); 57 template=template.Replace("$$name$$" , form["name"]??""); 58 template=template.Replace("$$email$$" , form["email"]??""); 59 template=template.Replace("$$topic$$" , form["topic"]??""); 60 template=template.Replace("$$content$$" ,form["content"]??""); 61 62 status=SendEmail(smtpSetting , "ContactUs" , template,out log); 63 64 } 65 } 66 catch(Exception e) 67 { 68 msg=e.Message; 69 status=false; 70 } 71 var objRtn = new { result=status.ToString(),Msg=msg,Log=log}; 72 context.Response.Write(ser.Serialize(objRtn)); 73 context.Response.End(); 74 } 75 76 private bool SendEmail(Smtp smtpSetting,string subject , string body,out string result) 77 { 78 SmtpClient smtpClient = new SmtpClient(); 79 result=""; 80 if(smtpSetting != null) 81 { 82 smtpClient.Host = smtpSetting.Host; 83 smtpClient.Port = smtpSetting.Port; 84 smtpClient.EnableSsl = smtpSetting.EnableSsl; 85 86 87 if(!String.IsNullOrEmpty(smtpSetting.UserName) && !String.IsNullOrEmpty(smtpSetting.Password)) 88 { 89 try 90 { 91 smtpClient.UseDefaultCredentials = false; 92 smtpClient.Credentials = new NetworkCredential(smtpSetting.UserName , smtpSetting.Password); 93 94 if(!string.IsNullOrEmpty(smtpSetting.From) && smtpSetting.To!=null && smtpSetting.To.Length>0) 95 { 96 MailMessage message = new MailMessage(); 97 message.From = new MailAddress(smtpSetting.From , smtpSetting.From); 98 foreach(string item in smtpSetting.To) 99 { 100 message.To.Add(new MailAddress(item)); 101 } 102 message.Subject = subject; 103 message.Body = body; 104 message.IsBodyHtml = true; 105 106 smtpClient.Send(message); 107 return true; 108 } 109 } 110 catch(Exception e) 111 { 112 result=e.Message; 113 return false; 114 } 115 116 } 117 118 } 119 return false; 120 } 121 122 public bool IsReusable 123 { 124 get 125 { 126 return false; 127 } 128 } 129 }
1 public class Smtp 2 { 3 public Smtp() { } 4 5 public bool EnableSsl { get; set; } 6 public string From { get; set; } 7 public string Host { get; set; } 8 public string Password { get; set; } 9 public int Port { get; set; } 10 public string[] To { get; set; } 11 public string UserName { get; set; } 12 }