Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot send email in ASP.NET through Godaddy servers

I have an ASP.NET application hosted on Godaddy that I want to send email from. When it runs, I get: Mailbox name not allowed. The server response was: sorry, relaying denied from your location. The important parts of the code and Web.config are below:

msg = new MailMessage("[email protected]", email);
        msg.Subject = "GreekTools Registration";
        msg.Body =
            "You have been invited by your organization to register for the GreekTools recruitment application.<br/><br/>" +
            url + "<br/><br/>" +
            "Sincerely,<br/>" +
            "The GreekTools Team";

        msg.IsBodyHtml = true;

        client = new SmtpClient();
        client.Host = "relay-hosting.secureserver.net";

        client.Send(msg);

<system.net>
<mailSettings>
  <smtp from="[email protected]">
    <network host="relay-hosting.secureserver.net" port="25" userName="********" password="*********" />
  </smtp>
</mailSettings>

like image 791
Jared Avatar asked Sep 01 '09 02:09

Jared


People also ask

What is SMTP relay in GoDaddy?

SMTP relays send messages through your email account using your existing email client. For example, if your Workspace Email account is set up on Outlook, you can continue to use Outlook to compose and send messages, but the actual messages are processed through our SMTP relaying services.


2 Answers

1- If your site is hosted on godaddy you may use "relay-hosting.secureserver.net" without credentials.

2- If your site is hosted outside of godaddy you may use "smtpout.secureserver.net" with you email account credentials.

PS: Please change port 3535 if you have problems with 25

Hosted On GoDaddy

    <system.net>
      <mailSettings>
       <smtp from="[email protected]">
        <network host="relay-hosting.secureserver.net"/>
       </smtp>
      </mailSettings>
    </system.net>

External

  <system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <network host="smtpout.secureserver.net" 
           userName="[email protected]" password="your_password_here" 
           port="25" />
      </smtp>
    </mailSettings>
  </system.net>
like image 63
Ozan BAYRAM Avatar answered Oct 13 '22 11:10

Ozan BAYRAM


Here's my email class:

public class Email
{
    public enum MailAddressType
    {
        From = 1,
        Bcc
    }

    private static MailAddress _from = null;

    public static void SendEmail(string to, string subject, string body)
    {
        SendEmail(to, subject, body, From, string.Empty);
    }

    public static void SendEmail(string to, string subject, string body, string from)
    {
        SendEmail(to, subject, body, from, MailAddressType.From);
    }

    public static void SendEmail(string to, string subject, string body, string addresses, MailAddressType addressType)
    {
        MailAddress from = From;
        string bcc = string.Empty;

        if (MailAddressType.From == addressType)
        {
            from = new MailAddress(addresses);
        }
        else
        {
            bcc = addresses;
        }

        SendEmail(to, subject, body, from, bcc);
    }

    private static void SendEmail(string to, string subject, string body, MailAddress from, string bcc)
    {
        MailMessage message = new MailMessage();
        message.From = From;
        message.To.Add(to);
        if (!string.IsNullOrEmpty(bcc))
        {
            message.Bcc.Add(bcc);
        }
        message.ReplyTo = from;
        message.Subject = subject;
        message.Body = HttpContext.Current.Server.HtmlEncode(body);
        SmtpClient smtp = new SmtpClient();
        smtp.Send(message);
    }

    public static MailAddress From
    {
        get
        {
            if (null == _from)
            {
                SmtpSection section = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
                string address = section.From;
                string displayName = ConfigurationManager.AppSettings["fromEmailDisplayName"];
                _from = new MailAddress(address, displayName);
            }
            return _from;
        }
    }
}

And here are the related web.config settings:

<appSettings>
    <add key="fromEmailDisplayName" value="Firstname Lastname"/>
</appSettings>

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network" from="[email protected]">
            <network host="relay-hosting.secureserver.net" />
        </smtp>
    </mailSettings>
</system.net>

For me, the key was "message.From = From" and "message.ReplyTo = from". GoDaddy seems to want the message to come from an address in your domain. So for contact pages, use your default email address as the From and set the sender as the ReplyTo. Email goes through fine after that.

like image 23
Rich Bennema Avatar answered Oct 13 '22 10:10

Rich Bennema