by 好大一小白
2017.7.20 09:20
一般我们使用System.Web.Mail或System.Net.Mail发送邮件,但这类命名空间支持Explicit SSL但是不支持Implicit SSL,所以如果发送使用如QQ邮箱的SSL 465端口,将会报超时错误。
System.Web.Mail代码
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
try
{
mail.To = "收件人邮箱";
mail.From = "发件人邮箱";
mail.Subject = "subject";
mail.BodyFormat = System.Web.Mail.MailFormat.Html;
mail.Body = "<font color='red'>body</font>";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "发件人邮箱"); //set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "发件人邮箱密码"); //set your password here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//set port
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//set is ssl
System.Web.Mail.SmtpMail.SmtpServer = "smtp.qq.com";
System.Web.Mail.SmtpMail.Send(mail);
//return true;
}
catch (Exception ex)
{
ex.ToString();
}
System.Net.Mail代码
using (MailMessage mail = new MailMessage()) { mail.From =
new MailAddress(BlogSettings.Instance.Email, name);
mail.ReplyTo = new MailAddress(email, name);
mail.To.Add(BlogSettings.Instance.Email);
mail.Subject = BlogSettings.Instance.EmailSubjectPrefix + " " + Resources.labels.email.ToLower() + " - " + subject;
mail.Body = "<div style=\"font: 11px verdana, arial\">";
Attachment attachment = new Attachment(txtAttachment.PostedFile.InputStream, txtAttachment.FileName);
mail.Attachments.Add(attachment);
try { message.IsBodyHtml = true;
message.BodyEncoding = Encoding.UTF8;
var smtp = new SmtpClient(BlogSettings.Instance.SmtpServer);
if (!string.IsNullOrEmpty(BlogSettings.Instance.SmtpUserName))
{ smtp.Credentials = new NetworkCredential(BlogSettings.Instance.SmtpUserName, BlogSettings.Instance.SmtpPassword); }
smtp.Port = BlogSettings.Instance.SmtpServerPort;
smtp.EnableSsl = BlogSettings.Instance.EnableSsl;
smtp.Send(message);
OnEmailSent(message); } catch (Exception ex) { } }
以上两种在遇到使用Implicit SSL的SMTP服务均会报超时错误。 解决方法,使用CDO.Message发送邮件,添加引用C:\Windows\System32\cdosys.dll
代码:
CDO.Message objMail = new CDO.Message();
try {
objMail.To = "接收邮件账号";
objMail.From = "发送邮件账号";
objMail.Subject = "subject";//邮件主题
objMail.HTMLBody = "";//邮件内容
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 465;//设置端口
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "smtp.qq.com";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = "发送邮件账号";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].Value = "发送邮件账号";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = "发送邮件账号";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = "发送邮件账号";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = "发送邮件账号登录密码";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"].Value = "true";//这一句指示是否使用ssl
objMail.Configuration.Fields.Update(); objMail.Send(); } catch (Exception ex)
{ throw ex; } finally { }
System.Runtime.InteropServices.Marshal.ReleaseComObject(objMail);
objMail = null;
以上基本可以解决邮件功能,当然如果需要发送附件,ASP.NET可以先临时保存文件,C/S架构则直接操作附件属性即可。 当然如果可以使用25端口,则不存在以上问题,但如国内阿里云封了25端口,服务器又没有端口映射,只能使用Implicit SSL 的 465端口,这中方式可以算是最简单的了,不使用系统外组件完成。
e612d1f6-d16e-423e-a4ba-ba47ba362d59|1|5.0
Tags: asp.net, Metro, WCF, web, websesrvice, web开发, Windows Phone 7, Windows8, WP7, 服务器, 客户端, 邮件
WCF | ASP.NET | 服务器 | IIS | Windows Phone 7 | Windows8