邮件发送错误解决一例

北京网站建设-恒动时空

网站中有用到通过程序发送邮件的功能,之前一直使用的是using System.Web.Mail,邮箱用的是126的邮箱,发邮件很正常。今天客户要求使用万网的云邮箱,按照格式设置了云邮箱的发件账户后,居然出现了“óê?t?T·¨·¢?íμ? SMTP ·t???÷?£′?ê?′í?ó′ú???a 0x80040217?£·t???÷?ìó|?a not available”的错误。百度了一下,发现遇到这种错误的很多,但解决办法基本是抄来抄去的。其中有一篇文章提到,System.Web.Mail已经过时了,所以会出错,应该使用System.Net.Mail来发送邮件。查了下MSDN,确实是这样。

此命名空间中的类已被否决。改用 System.Net.Mail 命名空间。System.Web.Mail 命名空间包含使您可以使用 CDOSYS(Windows 2000 的协作数据对象)消息组件来构造和发送消息的类。邮件消息是通过内置在 Microsoft Windows 2000 中的 SMTP 邮件服务或任意的 SMTP 服务器来传送的。此命名空间中的类可以在 ASP.NET 或任何托管应用程序中使用。 


上面这段话来自MSDN,意思很明显,System.Web.Mail已经被微软否决了,既然如此,那就听微软的,改用System.Net.Mail来发送吧。System.Net.Mail发送邮件并不复杂,网上的相关代码也有很多。按System.Net.Mail配置好了邮件发送程序,结果在发送邮件时又出现了错误。以下是错误提示:

不允许使用邮箱名称。 服务器响应为: authentication is required

怪了,为什么使用System.Net.Mail还是出错呢,继续百度,有很多文章提到要改smtp的设置,也有文章提到说用不同的邮箱用户名部分不一样。难道是这个原因,试下吧。

原来用126的邮箱,用户名部分是@前边的内容,既然可能是问题,那就都写全了吧,如:***@126.com,发送测试,居然成功了。既然成功了,会不会是之前的System.Web.Mail也是这个原因造成的呢?把代码恢复到初始的时候,使用System.Web.Mail发送邮件,只是还是把用户名写全,发送测试,也成功了。

由此,原因找到,问题解决。因此,很多时候问题解决还是要多想一下,网上的答案未必就是你要的答案,更何况现在更多的天下文章一大抄,你抄我来我抄你,但这些往往不能解决你的问题。

顺带附上分别使用两种方式发送邮件的代码:

使用System.Web.Mail

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using System.Net.Mail;
using HengCms.Common;

/// <summary>
/// SendMail 的摘要说明
/// </summary>
public class OperaterMail
{
    public static void SendMail(string tomail, string subject, string emailcon)
    {
        MailMessage objMailMessage;

        // 创建邮件消息 
        objMailMessage = new MailMessage();
        objMailMessage.From = rwWebConfig.readConfigValue("formemail");
        objMailMessage.To = tomail;
        objMailMessage.Subject = subject;//发送邮件的标题 
        objMailMessage.BodyFormat = MailFormat.Html;
        objMailMessage.Body = emailcon;

        objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
        //用户名 
        objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", rwWebConfig.readConfigValue("username"));
        //密码 
        objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", rwWebConfig.readConfigValue("password"));

        //SMTP地址 
        SmtpMail.SmtpServer = rwWebConfig.readConfigValue("smtp");
        //开始发送邮件 
        SmtpMail.Send(objMailMessage);
    }
}

使用System.Net.Mail

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using HengCms.Common;
using System.Net;

/// <summary>
/// SendMail 的摘要说明
/// </summary>
public class OperaterMail
{
    public static void SendMail(string tomail, string subject, string emailcon)
    {
    }

    static string strHost = rwWebConfig.readConfigValue("smtp");
    static string strAccount = rwWebConfig.readConfigValue("username");
    static string strPwd = rwWebConfig.readConfigValue("password");
    static string strFrom = rwWebConfig.readConfigValue("formemail");

    public static string sendMail(string to, string title, string content)
    {
        SmtpClient _smtpClient = new SmtpClient();
        _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
        _smtpClient.Host = strHost; ;//指定SMTP服务器
        _smtpClient.Credentials = new System.Net.NetworkCredential(strAccount, strPwd);//用户名和密码

        MailMessage _mailMessage = new MailMessage(strFrom, to);
        _mailMessage.Subject = title;//主题
        _mailMessage.Body = content;//内容
        _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
        _mailMessage.IsBodyHtml = true;//设置为HTML格式
        _mailMessage.Priority = MailPriority.High;//优先级

        try
        {
            _smtpClient.Send(_mailMessage);
            return "正常";
        }
        catch(Exception e)
        {
            return e.Message;
        }
    }
}

Web.config中的邮件设置

关键点在于username,使用126的邮箱是*****,使用万网的云邮箱则是*****@****.com

    <add key="formemail" value="*****@*******.com" />
    <add key="username" value="******" />//这个不同的邮箱不同设置
    <add key="password" value="******" />
    <add key="smtp" value="smtp.126.com" />

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值