6

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("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);
}    

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("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);
}

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.

Community
  • 1
  • 1
Anders
  • 12,088
  • 34
  • 98
  • 146

4 Answers4

7

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 = "myFromAddress@domain.com",
                                       To = "myToAddress@someotherdomain.com",
                                       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!

Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173
  • Ok, I will try this code out and report back. Basically, bottom line is GoDaddy is a giant PITA when it comes to emailing programmatically, huh? – Anders Dec 14 '10 at 21:46
  • Well, I added your code and it does the same thing as earlier: reports the email sent but no email has appeared in the destination inbox. Is there a log on my GoDaddy account somewhere I can access to see if something is messing up on that end? Although, that 90-minute relay delay you mentioned could be the culprit now. – Anders Dec 14 '10 at 21:59
  • Be certain that your 'From' email has your domain. For example my domain is 'bobcravens.com'. GoDaddy's email server will only relay emails from 'something@bobcravens.com'. – rcravens Dec 14 '10 at 22:06
  • Yes it is. The email just came through to the account so there was about a 10-15 minute delay. I will continue testing, though to ensure it is functioning "properly". – Anders Dec 14 '10 at 22:14
  • I wonder, if you added Gmail's MX records to GoDaddy's account via this url: https://www.godaddy.com/gdshop/google/gmail_login.asp, if sending the mail through Gmail would work. Who knows how long the MX update would take to propagate, though. – Anders Dec 14 '10 at 22:21
  • @anders sorry for such a late response, was away. Sometimes the 90 minutes are much shorter, like 15 minutes, but it's still kinda long! I don't adding MX records would help, though, because MX handles incoming mail. Also, the domains/applications where I use the mailing code have their MX records set to Google Apps, but I'm still unable to send email via Gmail's SMTP servers. – Maxim Zaslavsky Dec 14 '10 at 23:56
3

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.

Community
  • 1
  • 1
Travis J
  • 81,153
  • 41
  • 202
  • 273
  • Hey thanks for commenting on this dead thread, I am sure someone will find your addition useful. I will also reference this if I need to programatically email again from godaddy – Anders Jan 17 '13 at 03:04
2

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@domain.com 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("do_not_reply@bobcravens.com", "the_password");
const string from = "Email Service <do_not_reply@bobcravens.com>";
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.

rcravens
  • 8,320
  • 2
  • 33
  • 26
  • BTW...here is a link to how I used to send email via GoDaddy's email servers: http://blog.bobcravens.com/2009/08/send-email-programmatically-on-godaddy/ – rcravens Dec 14 '10 at 21:29
  • So it looks like the IsSsl is not present, the port has changed, and there are additional fields. Is the IEmailService the class inherits from something specific that your wrote? – Anders Dec 14 '10 at 21:35
  • The IEmailService interface just defines the two 'Send' methods. Nothing else. You can either create the interface, or remove the interface from the class definition. – rcravens Dec 14 '10 at 21:38
  • Also looks like this uses the deprecated `System.Web.Mail` rather than the newer `System.Net.Mail`. – Anders Dec 14 '10 at 21:39
  • I did notice that awhile ago. However, since the code is working for me, I have not updated. – rcravens Dec 14 '10 at 21:42
  • Ok, I implemented your code. I get this error `The transport failed to connect to the server.`. The only changes I made were removing the Interface from the Class definition then created this line after instantiating the GmailService object to send the mail: `test.Send(fromAddress.Address,toAddress.Address,subject,body,false);` – Anders Dec 14 '10 at 21:46
  • See the updated response. This could be working for me because I have a google apps account. Forgot about that. – rcravens Dec 14 '10 at 22:02
  • I think I have that set up as well. You have to upload a generated html file to the root of your webserver, correct? – Anders Dec 14 '10 at 22:37
  • I believe that is one way of google confirming you own the site. I chose modifying some dns entries. When you go to your google apps control panel, does it indicate that email is activated? – rcravens Dec 14 '10 at 22:40
  • @rcravens hmm... I have Google Apps for my domains/applications, too, but I'm still unable to send email via Gmail. Are you sure this works? If so, I really want to implement this, because GoDaddy's SMTP servers are horrendously slow. :) – Maxim Zaslavsky Dec 14 '10 at 23:58
  • I am using this exact code on my server (vps). Do you have the dns mail exchange (mx) records set up? http://www.google.com/support/a/bin/answer.py?hl=en&answer=33353 – rcravens Dec 15 '10 at 01:06
  • @rcravens yup, the MX are set up and have been working for a long time. I'll try your code out - thanks! – Maxim Zaslavsky Dec 15 '10 at 02:44
0

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.stack.imgur.com/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);}
    
Reeno
  • 5,720
  • 11
  • 37
  • 50