仿照上文《.Net中通过需要认证的SMTP服务器发送邮件》,将代码修改一下,就可以用ASP实现需要验证SMTP服务器邮件的发送,代码如下:
mail.asp
<%
if request.Form("Submit")<>"" then
Dim msg
Dim config
Dim ofields
set msg = Server.CreateObject("CDO.Message")
set config = Server.CreateObject("CDO.Configuration")
set ofields = config.Fields
With ofields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "用户名"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "密码"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/languagecode") = "0x0804"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.163.com"
'可以换成别的,跟".From "里的地址对应就可以了
.Update
End With
With msg
Set .Configuration = config
.From = "用户名@163.com"
.To = request.Form("to")
.Subject = request.Form("subject")
.HTMLBody = "<html><body>"&request.Form("body")&"</html></body>"
.Send
End With
Set msg = Nothing
Set config = Nothing
Set ofields = Nothing
Response.Write("<script>alert('发送成功!');location.href='mail.asp';</script>")
else
%>
<html>
<head>
<title>发送邮件测试</title>
</head>
<body>
<form name="form1" method="post" action="mail.asp">
<table width="500" border="0">
<tr>
<td width="69" height="30">收信人</td>
<td width="421"><input name="to" type="text" id="to"></td>
</tr>
<tr>
<td height="30">主题</td>
<td><input name="subject" type="text" id="subject"></td>
</tr>
<tr>
<td height="30">正文</td>
<td><textarea name="body" cols="55" rows="8" id="body"></textarea></td>
</tr>
<tr>
<td height="30" colspan="2" align="center"><input type="submit" name="Submit" value="发送"></td>
</tr>
</table>
</form>
</body>
</html>
<%end if%>
(本人电脑上测试通过)