从 GoDaddy 托管的 ASP.NET MVC 应用程序发送邮件消息时出现问题

发布于 2024-10-08 02:50:41 字数 3386 浏览 5 评论 0原文

我在 GoDaddy 托管的 MVC Web 应用程序上有一个表单,用户可以填写该表单并发送给我们的办公室。我目前正在使用 Gmail 帐户和 GoDaddy 电子邮件帐户(链接到我的托管空间)对其进行测试。使用 Gmail 代码后,电子邮件将从我的本地主机正常发送,但当我将其发布到网络时,出现以下错误:

请求“System.Net.Mail.SmtpPermission”类型的权限, 系统,版本=2.0.0.0,文化=中性, PublicKeyToken=b77a5c561934e089'失败。

这是代码(借自这篇文章)我是使用,凭据已更改,密码已删除:

var fromAddress = new MailAddress("[email protected]", "FEA Drone");
var toAddress = new MailAddress("[email protected]", "ImproveIt Home Services");
const string fromPassword = "<removed>";
var subject = string.Format("Energy Evaluation Request for {0} {1}", model.FirstName, model.LastName);
var body = MailBody(results);

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}    

然后,我尝试使用我为此特定表单设置的 GoDaddy 电子邮件,并再次在本地发送。然而,当这个上传时,它只是超时,而不是给我任何有用的信息。代码如下:

var fromAddress = new MailAddress("[email protected]", "FEA Drone");
var toAddress = new MailAddress("[email protected]", "ImproveIt! Home Services");
const string fromPassword = "<removed>";
var client = new SmtpClient
{
    Host = "relay-hosting.secureserver.net",
    Port = 25,
    EnableSsl = false,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Timeout = 20000,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};

using (var msg = new MailMessage(fromAddress, toAddress)
{
    Subject = string.Format("Energy Evaluation Request for {0} {1}", model.FirstName, model.LastName),
    IsBodyHtml = false,
    Body = MailBody(results)
})
{
    client.Send(msg);
}

最初我的 GoDaddy 主机有 smtpout.secureserver.net,但我从 这篇文章我需要将其更改为relay-hosting.secureserver.net。使用更新的主机信息,脚本将运行,但邮件消息不会到达目标电子邮件收件箱(或垃圾邮件箱)。

编辑

使用Maxim的代码,似乎我已经得到了一个“功能”版本。该电子邮件不会立即出现在目标收件箱中,而是在大约 15 分钟后出现。遗憾的是,在程序化电子邮件方面,GoDaddy 似乎是一个巨大的 PITA。

以下是我得到的信息:

var emailmessage = new System.Web.Mail.MailMessage()
{
    Subject = subject,
    Body = body,
    From = fromAddress.Address,
    To = toAddress.Address,
    BodyFormat = MailFormat.Text,
    Priority = System.Web.Mail.MailPriority.High
};

SmtpMail.SmtpServer = "relay-hosting.secureserver.net";
SmtpMail.Send(emailmessage);

再次感谢您的帮助,我希望我能弄清楚如何让 GoDaddy 更好地合作。如果可以的话,我会回复更新。

I have a form on a MVC Web Application that is hosted over at GoDaddy that users can fill out and send to our office. I am currently testing it using both a Gmail account and a GoDaddy email account (linked to my hosting space). With the Gmail code in place, the email will send fine from my localhost, but when I publish it to the web I get the following error:

Request for the permission of type 'System.Net.Mail.SmtpPermission,
System, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089' failed.

Here is the code (borrowed from this post) I am using, credentials have been changed and password removed:

var fromAddress = new MailAddress("[email protected]", "FEA Drone");
var toAddress = new MailAddress("[email protected]", "ImproveIt Home Services");
const string fromPassword = "<removed>";
var subject = string.Format("Energy Evaluation Request for {0} {1}", model.FirstName, model.LastName);
var body = MailBody(results);

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}    

I then tried to use my GoDaddy email that I set up for this particular form, and again locally it sends. However, when this one is uploaded it just times out rather than give me any sort of useful information. Here is that code:

var fromAddress = new MailAddress("[email protected]", "FEA Drone");
var toAddress = new MailAddress("[email protected]", "ImproveIt! Home Services");
const string fromPassword = "<removed>";
var client = new SmtpClient
{
    Host = "relay-hosting.secureserver.net",
    Port = 25,
    EnableSsl = false,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Timeout = 20000,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};

using (var msg = new MailMessage(fromAddress, toAddress)
{
    Subject = string.Format("Energy Evaluation Request for {0} {1}", model.FirstName, model.LastName),
    IsBodyHtml = false,
    Body = MailBody(results)
})
{
    client.Send(msg);
}

Originally I had smtpout.secureserver.net for my GoDaddy Host, but I found out from this article that I needed to change it to relay-hosting.secureserver.net. With the updated host information, the script runs but the mail message does not make it to the destination email inbox (or spam box).

Edit

Using Maxim's code, it seems I have gotten a "functioning" version in place. The email does not immediately appear in the destination inbox, but does so after about 15 minutes. Too bad it seems that GoDaddy is a giant PITA when it comes to programmatic emailing.

Here is what I got:

var emailmessage = new System.Web.Mail.MailMessage()
{
    Subject = subject,
    Body = body,
    From = fromAddress.Address,
    To = toAddress.Address,
    BodyFormat = MailFormat.Text,
    Priority = System.Web.Mail.MailPriority.High
};

SmtpMail.SmtpServer = "relay-hosting.secureserver.net";
SmtpMail.Send(emailmessage);

Thanks again for the assistance, I hope that I can figure out how to get GoDaddy to cooperate better. If I can, I will post back with updates.

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

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

发布评论

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

评论(4

盛夏尉蓝 2024-10-15 02:50:41

我也有一些托管在 GoDaddy 上的 ASP.NET MVC 应用程序,用于发送电子邮件。不幸的是,GoDaddy 电子邮件政策有些糟糕:

首先,您必须使用relay-hosting.secureserver.net - 您不能使用外部 SMTP 服务器,例如 Gmail。

其次,中继托管通常非常非常慢。根据我的经验,有些电子邮件大约需要90 分钟才能发送出去,而其他电子邮件则根本不会送达

我已多次通过电子邮件与 GoDaddy 支持人员联系解决此问题,但他们尚未解决巨大的等待时间/问题或允许外部 SMTP 服务器。


至于消息未送达的原因,您应该尝试多次运行该脚本以确保没有发生异常情况。如果它仍然不起作用,这是我的邮件代码:

var emailmessage = new System.Web.Mail.MailMessage()
                                   {
                                       Subject = "Subject",
                                       Body = "Body",
                                       From = "[email protected]",
                                       To = "[email protected]",
                                       BodyFormat = MailFormat.Text,
                                       Priority = MailPriority.High
                                   };

SmtpMail.SmtpServer = "relay-hosting.secureserver.net";
SmtpMail.Send(emailmessage);

唯一不同的事情(据我所知)是您试图向 SMTP 服务器提供凭据。实际上,relay-hosting.secureserver.net 不需要任何凭据,但只有在检测到邮件是从 GoDaddy 服务器发送时才会发送电子邮件。这可能会解决您的问题!

I have some ASP.NET MVC applications hosted on GoDaddy, too, that send out email. Unfortunately, the GoDaddy email policy is somewhat bad:

First of all, you must use relay-hosting.secureserver.net - you cannot use external SMTP servers, like Gmail.

Secondly, relay-hosting is usually very very slow. In my experience, some emails take around 90 minutes to be sent out, while others simply aren't delivered at all.

I've emailed back and forth with GoDaddy support many times about this issue but they have yet to fix the huge wait times/problems or allow external SMTP servers.


As for why your messages aren't delivering, you should try running the script multiple times to make sure that no anomalies are occuring. If it still doesn't work, here's my mail code:

var emailmessage = new System.Web.Mail.MailMessage()
                                   {
                                       Subject = "Subject",
                                       Body = "Body",
                                       From = "[email protected]",
                                       To = "[email protected]",
                                       BodyFormat = MailFormat.Text,
                                       Priority = MailPriority.High
                                   };

SmtpMail.SmtpServer = "relay-hosting.secureserver.net";
SmtpMail.Send(emailmessage);

The only thing that is different (as far as I have noticed) is that you are trying to supply credentials to the SMTP server. Actually, relay-hosting.secureserver.net does not require any credentials whatsoever, but it will only send email if it detects that the message is being sent from a GoDaddy server. This might fix your problem!

鲸落 2024-10-15 02:50:41

我收到此错误:“不允许使用邮箱名称。服务器响应是:抱歉,您所在位置的中继被拒绝”。我正在使用它进行连接:

SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "relay-hosting.secureserver.net";
smtpClient.Port = 25;
smtpClient.Timeout = 10000;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("emailAddress", "password");
String bodyText = "Hello World";
MailMessage mailMessage = new MailMessage("fromEmail", "toEmail", "Subject", bodyText);
mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
smtpClient.Send(mailMessage);

查看此答案后: https://stackoverflow.com/a/4594338/1026459 我开始意识到如果使用凭据,Host 应该不同。我将代码更改为:

SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smptout.secureserver.net";
//same code as above

电子邮件不仅正确发送,而且几秒钟内就到达了。

I received this error: "Mailbox name not allowed. The server response was: sorry, relaying denied from your location". I was using this to connect:

SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "relay-hosting.secureserver.net";
smtpClient.Port = 25;
smtpClient.Timeout = 10000;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("emailAddress", "password");
String bodyText = "Hello World";
MailMessage mailMessage = new MailMessage("fromEmail", "toEmail", "Subject", bodyText);
mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
smtpClient.Send(mailMessage);

Upon reviewing this answer: https://stackoverflow.com/a/4594338/1026459 I came to realize that the Host should be different if using credientials. I changed my code to this:

SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smptout.secureserver.net";
//same code as above

And not only did the emails send properly, but they arrived in seconds.

楠木可依 2024-10-15 02:50:41

不记得我在哪里找到此代码,但它适用于我的 GoDaddy 服务器,用于通过 google 帐户发送电子邮件:

public class GmailService : IEmailService
{
    private static int _port = 465;
    private readonly string _accountName;
    private readonly string _password;

    public GmailService(string accountName, string password)
    {
        _accountName = accountName;
        _password = password;
    }
    public void Send(string from, string to, string subject, string body, bool isHtml)
    {
        Send(from, to, subject, body, isHtml, null);
    }

    public void Send(string from, string to, string subject, string body, bool isHtml, string[] attachments)
    {
        System.Web.Mail.MailMessage mailMessage = new System.Web.Mail.MailMessage
                                                      {
                                                          From = from,
                                                          To = to,
                                                          Subject = subject,
                                                          Body = body,
                                                          BodyFormat = isHtml ? MailFormat.Html : MailFormat.Text
                                                      };


        // Add attachments
        if (attachments != null)
        {
            for (int i = 0; i < attachments.Length; i++)
            {
                mailMessage.Attachments.Add(new Attachment(attachments[i]));
            }
        }

        //  Authenticate
        mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
        // Username for gmail - [email protected] for email for Google Apps
        mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", _accountName);
        // Password for gmail account
        mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", _password);
        // Google says to use 465 or 587.  I don't get an answer on 587 and 465 works - YMMV
        mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", _port.ToString());
        // STARTTLS 
        mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);

        // assign outgoing gmail server
        SmtpMail.SmtpServer = "smtp.gmail.com";
        SmtpMail.Send(mailMessage);
    }
}

更新:这是我使用此代码的方式:

GmailService gmail = new GmailService("[email protected]", "the_password");
const string from = "Email Service <[email protected]>";
const string to = "my_email_address";
const string subject = "Contact Form";
string body = "your message";
gmail.Send(from, to, subject, body, false);

这可能会有所不同...我有一个 Google应用程序帐户(免费)。因此,我可以使发件人地址与我的服务器具有相同的域。

Don't recall where I found this code, but it works for me on my GoDaddy server for sending email via a google account:

public class GmailService : IEmailService
{
    private static int _port = 465;
    private readonly string _accountName;
    private readonly string _password;

    public GmailService(string accountName, string password)
    {
        _accountName = accountName;
        _password = password;
    }
    public void Send(string from, string to, string subject, string body, bool isHtml)
    {
        Send(from, to, subject, body, isHtml, null);
    }

    public void Send(string from, string to, string subject, string body, bool isHtml, string[] attachments)
    {
        System.Web.Mail.MailMessage mailMessage = new System.Web.Mail.MailMessage
                                                      {
                                                          From = from,
                                                          To = to,
                                                          Subject = subject,
                                                          Body = body,
                                                          BodyFormat = isHtml ? MailFormat.Html : MailFormat.Text
                                                      };


        // Add attachments
        if (attachments != null)
        {
            for (int i = 0; i < attachments.Length; i++)
            {
                mailMessage.Attachments.Add(new Attachment(attachments[i]));
            }
        }

        //  Authenticate
        mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
        // Username for gmail - [email protected] for email for Google Apps
        mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", _accountName);
        // Password for gmail account
        mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", _password);
        // Google says to use 465 or 587.  I don't get an answer on 587 and 465 works - YMMV
        mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", _port.ToString());
        // STARTTLS 
        mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);

        // assign outgoing gmail server
        SmtpMail.SmtpServer = "smtp.gmail.com";
        SmtpMail.Send(mailMessage);
    }
}

Update: Here is how I am using this code:

GmailService gmail = new GmailService("[email protected]", "the_password");
const string from = "Email Service <[email protected]>";
const string to = "my_email_address";
const string subject = "Contact Form";
string body = "your message";
gmail.Send(from, to, subject, body, false);

This may make a difference...I have a Google Apps account (free). Therefore, I am able to have the from address the same domain as my server.

却一份温柔 2024-10-15 02:50:41

效果很好,按照我的步骤

  1. 首先从 Godaddy Webmail 检查您的电子邮件计划

    a.登录您的客户经理。

    b.单击“工作区电子邮件”。

    c.在您要使用的帐户旁边,单击“管理”。

    d.对于包含您要使用的电子邮件地址的帐户,单击(显示地址)。

    e.单击您要使用的电子邮件地址,然后转到“高级”选项卡。它会显示电子邮件计划。

    [1]: https://i.sstatic.net/AJAoB.png< /p>

    根据电子邮件计划,您选择主机名。示例:如果电子邮件计划 AP(2) 表示 smtp.Host="smtpout.asia.secureserver.net",或者如果电子邮件计划 EU(2) 表示 smtp.Host="smtpout.europe.secureserver.net",或者如果电子邮件计划 US表示 smtp.Host="smtpout.secureserver.net"。

  2. 确认您的网站是否使用 SSL。因为您根据 SSL 选择 portNo
    示例:如果没有 SSL 则表示 smtp.EnableSsl = false 出站端口号为 25、80、3535。
    如果使用 SSL 则意味着 mtp.EnableSsl = true 出站端口号为 465。

    这些是使用 Godaddy 邮件配置时需要记住的所有要点。

    下面提到代码,

    using (MailMessage mail = new MailMessage(from, mailmodel.To))
                        {
    
                            mail.Subject = mailmodel.Subject;
                            mail.IsBodyHtml = true;
                            mail.Body = mailmodel.Body;
                            mail.Priority = MailPriority.High;
                            SmtpClient smtp = new SmtpClient();
                            smtp.Host = 主机;
                            smtp.EnableSsl = false;
                            smtp.UseDefaultCredentials = false;
                            NetworkCredential networkCredential = new NetworkCredential(来自, 密码);
                            smtp.Credentials = networkCredential;
                            smtp.端口 = 端口号;
                            smtp.超时= 100000; smtp.发送(邮件);}
    

It works fine, follow my steps

  1. First check your Email plan from Godaddy Webmail

    a. Log in to your Account Manager.

    b. Click Workspace Email.

    c. Next to the account you want to use, click Manage.

    d. For the account with the email address you want to use, click (Show addresses).

    e. Click the email address you want to use, and then go to the Advanced tab.it shows the Email Plan.

    [1]: https://i.sstatic.net/AJAoB.png

    Based on Email Plan, You choose the Host Name. Example: If Email Plan AP(2) means smtp.Host="smtpout.asia.secureserver.net"or if Email plan EU(2)means smtp.Host="smtpout.europe.secureserver.net" or if Email Plan US means smtp.Host="smtpout.secureserver.net".

  2. Confirm your site is with SSL or without SSL. Because you choose the portNo based on SSLs
    Example : if Without SSL means smtp.EnableSsl = false Out going PortNo is 25, 80, 3535.
    In case If With SSL means mtp.EnableSsl = true Out going PortNo is 465.

    These are all the main points to stay in mind to work in Godaddy mail configuration.

    Below mention the code,

    using (MailMessage mail = new MailMessage(from, mailmodel.To))
                        {
    
                            mail.Subject = mailmodel.Subject;
                            mail.IsBodyHtml = true;
                            mail.Body = mailmodel.Body;
                            mail.Priority = MailPriority.High;
                            SmtpClient smtp = new SmtpClient();
                            smtp.Host = host;
                            smtp.EnableSsl = false;
                            smtp.UseDefaultCredentials = false;
                            NetworkCredential networkCredential = new NetworkCredential(from, Password);
                            smtp.Credentials = networkCredential;
                            smtp.Port = portNo;
                            smtp.Timeout = 100000;                                smtp.Send(mail);}
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文