移动服务器后ASP.Net表单问题 - “发送邮件失败”

时间:2012-03-15 22:18:16

标签: asp.net forms

经过几个小时的头部刮擦,不知道为什么我的网站代码曾经在旧服务器上工作,现在在新服务器上它只是不起作用(更改了邮件服务器的IP,使用了服务器的子域地址,更改了端口,尝试了25,26,587和服务器管理员提出的任何建议)我终于偶然发现了真正的问题(我认为),即托管需要身份验证。

我不知道如何为我的旧代码添加身份验证,支持甚至不再回复我的故障单。任何帮助表示赞赏。

这是注册文件中发送激活码的部分:

MailClass.MailGonder("info@mysite.com", TxtEMail.Text, "Activation", body, "info@mysite.com", "emailpassword", "mail.mysite.com", 587);

这是Mail_Class.cs文件,它处理所有页面的邮件发送(还有其他页面除了注册页面之外还使用它):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;

namespace Sngl
{
public class MailClass
{
    public MailClass() { }

    public static void MailGonder(string kimden, string kime, string title, string body, string senderEmail, string senderPassword, string smtpServer, int port)
    {
        try
        {
            System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage(kimden, kime, title, body);
            MyMailMessage.IsBodyHtml = true;
            MyMailMessage.Priority = System.Net.Mail.MailPriority.High;
            System.Net.NetworkCredential mailAuthentication = new
            System.Net.NetworkCredential(senderEmail, senderPassword);
            System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient(smtpServer, port);
            mailClient.EnableSsl = false;
            mailClient.UseDefaultCredentials = false;
            mailClient.Credentials = mailAuthentication;
            mailClient.Send(MyMailMessage);

            PropertyClass.Result = true;

        }
        catch (Exception ex)
        {
            PropertyClass.Result = false;
            PropertyClass.Message = ex.Message;
        }
    }
}
}

显示的错误信息是:“发送邮件失败。” 没有提供更多细节。

1 个答案:

答案 0 :(得分:2)

link you gave必须是一个非常古老的帖子,因为它提供的示例使用过时的.net类。我已在下面将它转换为c#,但我不知道它是否会起作用,因为我之前从未使用过这些东西。

using System;
using System.Web.Mail;

namespace Sngl
{
    public class MailClass
    {
        public MailClass() { }

        public static void MailGonder(string kimden, string kime, string title, string body, string senderEmail, string senderPassword, string smtpServer, int port)
        {
            try
            {
                var message = new MailMessage
                {
                    From = kimden,
                    To = kime,
                    Subject = title,
                    Body = body
                };
                message.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = smtpServer;
                message.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = port;
                message.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 1;
                message.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
                message.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = senderEmail;
                message.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = senderPassword;
                message.Fields["http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"] = 60;

                SmtpMail.SmtpServer = smtpServer;
                SmtpMail.Send(message);

                PropertyClass.Result = true;
            }
            catch (Exception ex)
            {
                PropertyClass.Result = false;
                PropertyClass.Message = ex.Message;
                throw;
            }
        }
    }
}