I have a form in an MVC web application hosted on GoDaddy that users can fill out and submit to our office. I am currently testing it using both a Gmail account and a GoDaddy email account (associated with my hosting). With a Gmail code in place, the letter will send a fine from my local host, but when I publish it on the Internet, I get the following error:
A permission request 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 use, the credentials have been changed and the password has been deleted:
var fromAddress = new MailAddress(" iihs.eval@gmail.com ", "FEA Drone"); var toAddress = new MailAddress(" improveithomeservices@gmail.com ", "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); }
Then I tried using the GoDaddy email that I configured for this particular form, and again it sends locally. However, when it loads, it just occasionally does not give me any useful information. Here is this code:
var fromAddress = new MailAddress(" fea@goimproveit.com ", "FEA Drone"); var toAddress = new MailAddress(" improveithomeservices@gmail.com ", "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); }
I initially had smtpout.secureserver.net for my GoDaddy host, but I learned from this article that I needed to change it to relay-hosting.secureserver.net . A script is executed with the updated host information, but the mail message does not get into the inbox of e-mail messages (or spam box).
Edit
Using the Maxim code, it seems that I got a βliveβ version in place. The letter does not immediately appear in the destination folder, but does so after about 15 minutes. Too bad, GoDaddy seems to be a gigantic PITA when it comes to software-based 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 your help, I hope I can figure out how to get GoDaddy to work better. If I can, I will send an update message.