将 System.Web.Mail 功能移植到 System.Net.Mail

发布于 2024-07-14 10:03:55 字数 2198 浏览 9 评论 0原文

以下函数完美运行:

    protected void SendWebMailMessage(string DisplayName, string From, string ReplyTo,
        string To, string Subject, string Body, string Attachments)
    {
        System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver",
             "smtpout.secureserver.net");
        msg.Fields.Add(
             "http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 
             2);
        msg.Fields.Add(
             "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
        msg.Fields.Add(
             "http://schemas.microsoft.com/cdo/configuration/sendusername", From);
        msg.Fields.Add(
             "http://schemas.microsoft.com/cdo/configuration/sendpassword",
             mailpassword);
        msg.To = To;
        msg.From = DisplayName + "<" + From + ">";
        msg.BodyFormat = MailFormat.Html;
        msg.Subject = Subject;
        msg.Body = Body;
        msg.Headers.Add("Reply-To", ReplyTo);
        SmtpMail.SmtpServer = "smtpout.secureserver.net";
        SmtpMail.Send(msg);
    }

但是,我收到构建警告,告诉我 System.Web.Mail 已过时,并且 我应该使用 System.Net.Mail。 所以我使用了 System.Net.Mail,然后我想出了 下面的函数来替换我的旧函数:

    protected void SendNetMailMessage(string DisplayName, string From, string ReplyTo, 
        string To, string Subject, string Body, string Attachments)
    {
        MailAddress addrfrom = new MailAddress(From, DisplayName);
        MailAddress addrto = new MailAddress(To);
        MailAddress replytoaddr = new MailAddress(ReplyTo);
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.From = addrfrom;
        msg.To.Add(addrto);
        msg.ReplyTo = replytoaddr;
        msg.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient("smtpout.secureserver.net");
        smtp.Credentials = new NetworkCredential(From, mailpassword);
        smtp.Send(msg);
    }

我没有收到任何异常或错误,但我的消息永远不会通过。 任何人都可以 告诉我我可能做错了什么? 提前致谢。

The following function works perfectly:

    protected void SendWebMailMessage(string DisplayName, string From, string ReplyTo,
        string To, string Subject, string Body, string Attachments)
    {
        System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver",
             "smtpout.secureserver.net");
        msg.Fields.Add(
             "http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 
             2);
        msg.Fields.Add(
             "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
        msg.Fields.Add(
             "http://schemas.microsoft.com/cdo/configuration/sendusername", From);
        msg.Fields.Add(
             "http://schemas.microsoft.com/cdo/configuration/sendpassword",
             mailpassword);
        msg.To = To;
        msg.From = DisplayName + "<" + From + ">";
        msg.BodyFormat = MailFormat.Html;
        msg.Subject = Subject;
        msg.Body = Body;
        msg.Headers.Add("Reply-To", ReplyTo);
        SmtpMail.SmtpServer = "smtpout.secureserver.net";
        SmtpMail.Send(msg);
    }

However, I get a build warning telling me that System.Web.Mail is obsolete, and
that I should be using System.Net.Mail. So I used System.Net.Mail, and I came up with
the following function to replace my old one:

    protected void SendNetMailMessage(string DisplayName, string From, string ReplyTo, 
        string To, string Subject, string Body, string Attachments)
    {
        MailAddress addrfrom = new MailAddress(From, DisplayName);
        MailAddress addrto = new MailAddress(To);
        MailAddress replytoaddr = new MailAddress(ReplyTo);
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.From = addrfrom;
        msg.To.Add(addrto);
        msg.ReplyTo = replytoaddr;
        msg.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient("smtpout.secureserver.net");
        smtp.Credentials = new NetworkCredential(From, mailpassword);
        smtp.Send(msg);
    }

I don't get any exceptions or errors, but my message never goes through. Can anyone
tell me what I might be doing wrong? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花桑 2024-07-21 10:03:55

如果我没记错的话...您正在使用 Goddady 托管。
我也在使用它...并且我有 **relay-hosting.secureserver.net** 作为 SMTP 服务器。
我不知道...可能是这样的...或者您没有在控制台中设置继电器。

If I'm not wrong... you are using the Goddady hosting.
I'm also using it... and I have **relay-hosting.secureserver.net** as the SMTP server.
I don't know... may be can be something like that... or you have not set the Relay in your console.

再见回来 2024-07-21 10:03:55

我测试了你上面的代码,它工作正常。 我必须添加正文和主题属性,但即使没有它们,我仍然收到一封空白电子邮件。 我也尝试过提供和不提供凭据,两者都有效。

最初,我没有更改您的主机字符串,并且确实收到了一条异常消息“发送邮件失败”。

I tested you code above an it works fine. I had to add the body and subject properties, but even without them I still received a blank email. I also tried with and without providing credentials, both worked.

Initially I hadn't changed your host string and did receive an exception with the message "Failure sending mail."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文