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).
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.
To host your ASP.NET MVC website on GoDaddy, you must have Windows plan. Let's start step by step. Go to GoDaddy and login with your account credentials. Now, your Account page will open where you will get WEB HOSTING.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With