作者: 浏览量:268 更新时间: 2018.01.31
因服务器的25端口默认封闭,需要使用SSL加密端口(通常是465)来对外发信,以下样例适用于程序调用外部邮箱发信的场景,调用的邮箱服务器需要支持SSL加密。这里介绍.NET和PHP的样例,其他语言实现方法思路基本相同。
通过连接外部邮箱的发信服务器,并通过程序配置的账号密码鉴权验证来发送邮件,而不是服务器本身来发送邮件。基本原理与本地电脑使用outlook等客户端连接邮箱服务器发送邮件一致。
基本实现方式与连接25端口发送邮件一致,但改为SSL加密协议后,需要特别注意:
截取部分源代码样例如下:
MailMessage mmsg = new MailMessage();//邮件主题mmsg.Subject = "主题";mmsg.BodyFormat = MailFormat.Html;//邮件正文mmsg.Body = "正文";//正文编码mmsg.BodyEncoding = Encoding.UTF8;//优先级mmsg.Priority = MailPriority.High;//发件者邮箱地址mmsg.From = "xxxxxx";//收件人收箱地址mmsg.To = "xxxxxx";mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//用户名mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "xxxxxx");//密码mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "xxxxxx");//端口mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//使用SSL mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//Smtp服务器System.Web.Mail.SmtpMail.SmtpServer = "smtp.xx.com";SmtpMail.Send(mmsg);
可将以上Demo上传至服务器测试发信是否能够成功。
截取部分源代码样例如下
<?phprequire '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.xxx.com"; // SMTP 服务器$mail->Port = 465; // SMTP服务器的端口号$mail->Username = "xxx@xxx.com"; // SMTP服务器用户名$mail->Password = "xxx"; // SMTP服务器密码$mail->SetFrom('xxx@xxx.com', 'qq'); // 设置发件人地址和名称$mail->AddReplyTo("xxx@xxx.com","xxx@xxx.com");// 设置邮件回复人地址和名称$mail->Subject = 'xxx'; // 设置邮件标题$mail->AltBody = "为了查看该邮件,请切换到支持 HTML 的邮件客户端";// 可选项,向下兼容考虑$mail->MsgHTML('<html>helo</html>'); // 设置邮件内容$mail->AddAddress('xxx@xxx.com', "xxx@xxx.com");//$mail->AddAttachment("images/phpmailer.gif"); // 附件if(!$mail->Send()) {echo "发送失败:" . $mail->ErrorInfo;} else {echo "恭喜,邮件发送成功!";}?>
可将以上Demo上传至服务器测试发信是否能够成功。
<%Set Mail = CreateObject("CDO.Message")Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="smtp.126.com"Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="xxx@126.com"Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="xxxxx"Mail.Configuration.Fields.UpdateMail.Subject="Email subject"Mail.From="xxxxx"Mail.To="xxxx"Mail.TextBody="This is an email message."Mail.SendSet Mail = Nothing%><%="发送成功!!!"%>
在本地电脑使用客户端软件(Outlook,Foxmail等),使用上述拿到的配置来设置本地客户端,并发信测试,如果不能发送,也需要联系邮箱服务商。具体配置方法略。这步主要是验证第1步拿到的配置是否正确,如果配置不正确,本地发信就会报错。
把程序中的邮箱服务器配置项按照上述测试通过的配置来编写,并实际发信测试。
工作时间5-10分钟将由1对1顾问为您提供尊享服务