asp.net发送邮件失败问题

原创 时间:2009-05-18 阅读: 佚名
众所周知,在asp.net2.0中发邮件 很简单,首先我定义一个Mail的类(便于以后重用) using System; using System.Net.Mail; namespace Jumbot.Common { /// <summary> /// 发送邮件类 /// </summary> public static class Mail { /// <summary> /// 发送邮件 /// </summar

众所周知,在asp.net2.0中发邮件 很简单,首先我定义一个Mail的类(便于以后重用)

using System;
using System.Net.Mail;
namespace Jumbot.Common
{
    /// <summary>
    /// 发送邮件类
    /// </summary>
    public static class Mail
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="MailTo">接收人用户名</param>
        /// <param name="MailSubject">邮件主题</param>
        /// <param name="MailBody">邮件内容</param>
        /// <param name="IsHtml">邮件正文是否为HTML格式</param>
        /// <param name="MailFrom">发件人邮箱地址</param>
        /// <param name="MailFromName">发件人署名</param>
        /// <param name="MailPwd">发件人邮箱密码</param>
        /// <param name="MailFromHost">发件人邮箱Host,如"smtp.126.com"</param>
        public static bool Send(string MailTo, string MailSubject, string MailBody, bool IsHtml, string MailFrom, string MailFromName, string MailPwd, string MailFromHost)
        {
            try
            {
                MailMessage message = new MailMessage();
                message.From = new MailAddress(MailFrom, MailFromName);
                message.To.Add(new MailAddress(MailTo));
                message.Subject = MailSubject;
                message.Body = MailBody;
                message.IsBodyHtml = IsHtml;
                SmtpClient mailClient = new SmtpClient(MailFromHost);
                mailClient.Credentials = new System.Net.NetworkCredential(MailFrom, MailPwd);
                mailClient.Send(message);
            }
            catch (FormatException)
            {
                return false;
            }
            return true;
        }
    }
}

然后我又将默认的发件人信息保存在mail.config中,

<?xml version="1.0" encoding="utf-8" ?>
<Mails>
  <System>
    <Address>jumbot114@126.com</Address>
    <!-- 系统邮箱地址 -->
    <NickName>系统</NickName>
    <!-- 系统署名 -->
    <Password>************</Password>
    <!-- 系统邮箱密码 -->
    <MailHost>smtp.126.com</MailHost>
    <!-- 系统邮箱服务器 -->
  </System>
  <!-- 系统邮箱,不是所有的信箱都能正确发送,需要测试 -->
</Mails>

之后我就定义一个函数

 

        /// <summary>
        /// 系统发邮件
        /// </summary>
        /// <param name="_To">收件人</param>
        /// <param name="_Title">标题</param>
        /// <param name="_Body">内容</param>
        /// <param name="_IsHtml">是否支持html</param>
        /// <returns></returns>
        public bool SendMail(string _To, string _Title, string _Body, bool _IsHtml)
        {
            string strXmlFile = HttpContext.Current.Server.MapPath("/cmsfile/config/mail.config");
            Jumbot.DataOper.XmlControl XmlTool = new Jumbot.DataOper.XmlControl(strXmlFile);
            string _MailFrom = XmlTool.GetText("Mails/System/Address");
            string _MailFromName = XmlTool.GetText("Mails/System/NickName");
            string _MailPwd = XmlTool.GetText("Mails/System/Password");
            string _MailFromHost = XmlTool.GetText("Mails/System/MailHost");
            _Body += "<br /><a href='" + site.Url + "'>" + site.Name + "</a><br>" + DateTime.Now.ToString("yyyy.MM.dd");
            return Jumbot.Common.Mail.Send(_To, _Title, _Body, _IsHtml, _MailFrom, _MailFromName, _MailPwd, _MailFromHost);
        }

 

如此一来,我就可以通过设置的邮箱和密码作为发信人给会员发邮件了,基本上程序是没有问题的,然而我试了多个邮箱,发现不是所有的都成功,至今未找到原因。后来百度一下,出次问题的还不止我一人,但都没最终找到答案,或许是邮箱的某个过滤设置吧,没搞懂。

 

PS,错误提示如下:邮箱不可用。 服务器响应为: Óû§±»Ëø¶¨

[责任编辑:jumbot]

[发表评论] [收藏本页]