设置BlogEngine.NET的邮件选项,Godaddy的SMTP配置方法

by shinichi_wtn 2011-02-08 15:37

昨天晚上配置BlogEngine.NET的邮件选项的时候遇到了几个问题,由于Godaddy禁止第三方SMTP服务,所以要让程序通过Godaddy主机发送邮件,必须使用Godaddy自己的SMTP服务。BlogEngine.NET的邮件设置也不例外,必须在Godaddy先开通自己域名的邮箱,然后才能使用Godaddy的SMTP和POP服务,并且这其中有些设置上的方法,现在写这篇文章来总结一下Godaddy的SMTP配置方法,从而让BlogEngine.NET能够正常发送邮件

Godaddy个人邮箱(或邮局)开通方法

购买Godaddy主机后,会附送一个1G的个人邮箱和5个100M的企业邮箱,它们默认是没有激活的,需要我们选择并激活,可以在Email面板中激活,在"To apply a free Email credit, click on the "Use Credit" link for the account type you want to activate. "这句话下面,就有两种邮箱可以申请,如果只是要收发邮件,就激活1G的个人Email即可。激活后就可点击管理这个Email(如下图,点击Manage Account管理个人邮局),管理时新建一个自己的域名邮箱就能使用了。

alt

有时候,我们希望通过SMTP来自动发送邮件,比如BlogEngine.NET中的“联系页面”和“有新评论通知我”这样的功能,需要一个可以用.NET的发送邮件的SMTP服务器,直觉上我们应该在个人邮局的控制面板上找到这样的信息,果然,面板上的Domains菜单里有Server Adresses这样的图标,点击后弹出一个对话框,显示了邮局的SMTP,POP等服务器地址。所以我们认为用这上面的信息就能成功配置BlogEngine.NET的邮件选项,事实上是不行的。

alt

错误一:Unable to connect to the remote server

按照邮局控制面板的SMTP地址填上,端口填写25,不启用SSL,按照这样的设置,点击测试邮件配置后,等一段时间会显示如下错误:

Error sending email in SendMailMessage: Failure sending mail. Unable to connect to the remote server A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

意思就是无法连接到SMTP服务器

产生原因:主机开启了SSL,开启SSL和未开启SSL的SMTP端口号是不一样的,开启SSL的主机应该填写465,未开启SSL的主机应该填写25。将端口改为465后,不会出现上述错误,而是立即出现错误二。

错误二:Request for the permission of type 'System.Net.Mail.SmtpPermission

当连接SMTP服务器没有问题后,出现了新的问题,完整的错误信息如下:

Error sending email in SendMailMessage: Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

可以看到,BlogEngine.NET的邮件发送程序不能调用System.Net.Mail.SmtpPermission,这个错误产生的原因就是Godaddy主机权限的设置问题。Godaddy主机限制了ASP.NET程序运行的Trust Level,并且不允许在web.config里更改。

这可郁闷了,也就是即使能连接服务器,也没有权限通过ASP.NET程序来发送邮件,只能用在线邮箱收发,或者Outlook这样的客户端来发送邮件。

Godaddy的SMTP正确设置方法

通过互联网的搜索,最终找到了答案,我们只能通过Godaddy提供的一个被信任的SMTP服务器来登录并发送邮件,是在ASP.NET的论坛上找到的,帖子名为Correct Settings for using SMTP on GoDaddy?。即SMTP服务器必须设置为relay-hosting.secureserver.net 才能使用ASP.NET程序来发送邮件(MailMessage类),并且为了垃圾群发,每个邮箱24小时内最多可以发送250封邮件。

打开BlogEngine.NET的源码,找到发送邮件的代码,果然也是用的MailMessage类

public static string SendMailMessage(MailMessage message)
{
    if (message == null)
    {
        throw new ArgumentNullException("message");
    }

    StringBuilder errorMsg = new StringBuilder();

    try
    {
        message.IsBodyHtml = true;
        message.BodyEncoding = Encoding.UTF8;
        var smtp = new SmtpClient(BlogSettings.Instance.SmtpServer);

        // don't send credentials if a server doesn't require it,
        // linux smtp servers don't like that 
        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)
    {
        OnEmailFailed(message);

        errorMsg.Append("Error sending email in SendMailMessage: ");
        Exception current = ex;

        while (current != null)
        {
            if (errorMsg.Length > 0) { errorMsg.Append(" "); }
            errorMsg.Append(current.Message);
            current = current.InnerException;
        }

        Utils.Log(errorMsg.ToString());
    }
    finally
    {
        // Remove the pointer to the message object so the GC can close the thread.
        message.Dispose();
    }

    return errorMsg.ToString();
}

var smtp = new SmtpClient(BlogSettings.Instance.SmtpServer)里面的BlogSettings.Instance.SmtpServer就是我们在控制面板设置的SMTP地址,因此最终正确的邮件配置方案如下图:

alt

这时再点击测试邮件配置,则成功了,在Godaddy的邮箱中会收到一封如下的测试邮件

alt

总结

虽然折腾了不少时间,但是最终还是弄明白了这其中的许多东西。不得不说Godaddy权限控制非常全面,虽然带来了麻烦,但是安全性和稳定性却得以保证。况且一天通过SMTP发送250封邮件对于个人用户肯定足够了。并且直接通过网页邮箱发送的邮件并不参与统计。

有任何疑问欢迎留言,也欢迎大家讨论其他可行的解决方案。比如我在搜索过程中看到的一篇博文,也是解决同样的问题《当GoDaddy碰到SmtpPermission

Comments (7) -

liyunfan1992 People's Republic of China
2/10/2011 4:57:28 PM #

学长现在在搞.net开发?

Reply

shinichi_wtn People's Republic of China
2/10/2011 5:42:13 PM #

是啊,一直对.NET比较感兴趣,呵呵!平时做的一些项目里也会用到。

Reply

liyunfan1992 People's Republic of China
2/10/2011 5:48:50 PM #

哦~~我是用PHP做项目,不吃微软那一套,嘿嘿

Reply

wan
4/7/2011 11:06:16 PM #

lz,你的blogengine是裝在主域名下吗?我装在附加域名的子目录里 url老是出问题,lz遇到没

Reply

shinichi_wtn
4/21/2011 9:02:25 PM #

对,我的BlogEngine是在主域名下,子目录可能要麻烦些,Godaddy将域名绑定到子目录实际上是转发到如下网址

http://主域名/子目录名

所以在子目录下可能需要一定的机制重写URL才行。

Reply

牙牙
6/24/2011 5:00:09 PM #

php的可以怎么解决呢.换了几种方法都唔work啊.

Reply

shinichi_wtn
6/25/2011 11:24:09 PM #

PHP应该有类似的解决方法吧,Godaddy禁止使用第三方SMTP服务,只能通过Godaddy提供的一个被信任的SMTP服务器来登录并发送邮件,即relay-hosting.secureserver.net,并且每个邮箱24小时内最多可以发送250封邮件!

Reply

(仅用于Gavatar)

  Country flag

biuquote
  • Comment
  • Preview
Loading

About

shinichi_wtnI'm Shinichi_wtn

Software Engineering Manager at Microsoft

[More...]


Month List