因云虚拟主机的25端口默认封闭,需要使用SSL加密端口(通常是465端口)来对外进行发送邮件。本文通过提供.NET、PHP和ASP样例来介绍使用SSL加密端口发送邮件的方法,其他语言的实现思路与本文介绍的方法基本相同。
背景信息
基本原理
使用SSL加密端口发送邮件的基本原理与本地主机使用客户端(例如Outlook等)连接邮箱服务器发送邮件一致。即主机通过连接外部邮箱的发邮件服务器,并通过程序配置的账号密码鉴权验证来发送邮件,而不是服务器本身来发送邮件。
实现要点
使用SSL加密端口发送邮件的基本实现方式与使用25端口发送邮件一致,但改为SSL加密协议后,需要特别注意以下几点:
编写好的程序调用外部邮箱发送邮件时,邮箱服务器需支持SSL加密功能。
主机连接邮箱服务器时,请在编写好的程序中启用SSL加密协议。
将连接外部邮箱服务器的端口改为SSL加密端口(465端口),具体配置信息可以向邮箱服务商咨询。
操作步骤
联系邮箱服务商获取使用SSL加密方式发送邮件的配置信息。
通常包含以下配置信息:
发邮件服务器地址:例如,a***.example.com。
发邮件服务器端口号:加密端口一般是465端口。
邮箱用户名:可能是Email地址,也可能是Email地址前缀,具体可咨询邮箱服务商。
邮箱客户端密码:部分邮箱服务商Web浏览器登录界面的登录密码和客户端密码并不相同,可能需要单独设置,具体可咨询邮箱服务商。
在本地主机使用客户端软件(Outlook、Foxmail等),使用已获取的邮件配置信息设置本地客户端,并发送邮件测试。
说明如果邮件发送失败,表示获取的配置信息不正确,您需要联系邮箱服务商解决。
根据实际的开发语言选择对应的程序样例,并将程序样例下载到本地。
程序样例如下所示:
使用已通过测试的配置信息,编写程序样例中的邮箱服务器配置项。
将编写好的程序通过Filezilla工具上传到云虚拟主机上,并实际发送邮件测试。
程序上传到云虚拟主机,更多信息,请参见使用FileZilla管理文件。
.NET源程序样例
下载路径:.NET源程序样例
截取部分源程序样例如下:
MailMessage mmsg = new MailMessage();
mmsg.Subject = "主题"; //邮件主题
mmsg.BodyFormat = MailFormat.Html;
mmsg.Body = "正文"; //邮件正文
mmsg.BodyEncoding = Encoding.UTF8; //正文编码
mmsg.Priority = MailPriority.High; //优先级
mmsg.From = "a***@example.com"; //发件人邮箱地址
mmsg.To = "b***@example.com"; //收件人邮箱地址
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "test01"); //用户名
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123****"); //密码
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465); //端口
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true"); //使用SSL
System.Web.Mail.SmtpMail.SmtpServer = "smtp.****.com"; //smtp服务器
SmtpMail.Send(mmsg);
PHP源程序样例
下载路径:PHP源程序样例
截取部分源程序样例如下:
<?php
require 'PHPMailerAutoload.php';
require_once('class.phpmailer.php');
require_once("class.smtp.php");
$mail = new PHPMailer();
$mail->CharSet="UTF-8"; //设定邮件编码,默认ISO-8859-1,如果发送中文此项必须设置为 UTF-8
$mail->IsSMTP(); //设定使用SMTP服务
$mail->SMTPAuth = true; //启用SMTP验证功能
$mail->SMTPSecure = "ssl"; //启用SSL
$mail->SMTPDebug = 2;
$mail->Host = "smtp.****.com"; //SMTP服务器
$mail->Port = 465; //SMTP服务器的端口号
$mail->Username = "test01"; //SMTP服务器用户名
$mail->Password = "123****"; //SMTP服务器密码
$mail->SetFrom('a***@example.com', 'a***'); //设置发件人地址和名称
$mail->AddReplyTo("b***@example.com","b***"); //设置邮件回复人地址和名称
$mail->Subject = '邮件标题'; //设置邮件标题
$mail->AltBody = "为了查看该邮件,请切换到支持HTML的邮件客户端"; //可选项,向下兼容考虑
$mail->MsgHTML('<html>helo</html>'); //设置邮件内容
$mail->AddAddress('c***@example.com', "c***");
$mail->AddAttachment("images/phpmailer.gif"); //附件
if(!$mail->Send()) {
echo "发送失败:" . $mail->ErrorInfo;
} else {
echo "恭喜,邮件发送成功!";
}
?>
ASP源程序样例
下载路径:ASP源程序样例
截取部分源程序样例如下:
<%
Set Mail = CreateObject("CDO.Message")
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="smtp.****.com"
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="test01"
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="123****"
Mail.Configuration.Fields.Update
Mail.Subject="Email subject"
Mail.From="a***@example.com"
Mail.To="b***@example.com"
Mail.TextBody="This is an email message."
Mail.Send
Set Mail = Nothing
%>
<%="发送成功!!!"%>