C# - SMTP - GoDaddy - Send Email

2020-03-31 08:50发布

问题:

What am I doing wrong? Im trying to send a email using c# with GoDaddy webhost.

SmtpClient client = new SmtpClient("relay-hosting.secureserver.net", 465);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("emailGODADDY", "password");

MailMessage message = new MailMessage("emailGODADDY", "otherEmail");
message.Subject = txtSubject.Text;
message.Body = txtContent.Value;

client.Send(message);

回答1:

Each smtp server has own credentials which is not same with other.

According to microsoft client.UseDefaultCredentials should not be used when possible.

You can try by omitting this line of code.....

client.UseDefaultCredentials = false;

If this will not work then try with

client.EnableSsl = false;

Because some server do not use secure connection.

You can check with this code also

client.DeliveryMethod = SmtpDeliveryMethod.Network;


回答2:

With a shared hosting account with Go Daddy you need to send emails on port 25 not port 465. Furthermore, relay-hosting.secureserver.net does not need you to authenticate with a username and password when you are sending from your hosting account.



回答3:

Just comment out below line than it will work fine

//client.EnableSsl = false;

also use Port 25.



回答4:

OKAY! I have figured this out.

Wow, I've spent so much time trying to get this up and running. I have Economy Windows Hosting with Plesk (Shared), with a single Office365 email account. I learned the hard way that you can't create a SMTP client that connects to smtp.office365.com, as port 587 is blocked for this shared hosting package. TXT records, SPF records don't help either. Lots of wasted time.

But alas, here's exactly what worked for me. I added the following to my web.config, though I think you can build the same info into your SMTP client object. Still, this works fine. Whatever.

<system.net>
  <mailSettings>
    <smtp from="noreply@MyDomain.com">        
      <network host="relay-hosting.secureserver.net" port="25" />        
    </smtp>
  </mailSettings>
</system.net>

In my code behind, I made sure that the FROM address used in the MailMessage exactly matched the FROM value within the web.config. Does this really matter? Not sure, but they match, and it works. Whatever.

The FROM address (noreply@) does NOT exist as an email, nor is it an alias, etc. It's just something from the same domain where the website is hosted.

My TO address is retrieved from within the web.config (AppSettings["SendTo"]). This is my real email address that lives on this domain (my Office365 email address). I'm not sure if you can send an email to something outside the domain, as I haven't tested.

... obviously the MailMessage (msg) is not complete ...

msg.To.Add(new MailAddress(ConfigurationManager.AppSettings["SendTo"].ToString()));
msg.From = new MailAddress("noreply@MyDomain.com");


var smtp = new SmtpClient 
{       
 // nothing is needed here  
};
smtp.Send(msg);

Create your client and send the message!

YAY! I had to list the noreply@ email as not spam, but now it's arriving as expected. Remember, you only have a limited number of relay emails per day, so use them wisely! Hope this helps!



标签: c# email smtp