Sending email through a Google Apps account is wor

2020-07-09 05:50发布

问题:

Related:

Send Email via C# through Google Apps account

My question is the same so I implemented Koistya's solution as follows. The heartbreak is that it works beautifully on my dev laptop but when ported to web server it times out with no explanation.

My SMTP config is in my web.config file. I made mods based on Koistya's answer:

<mailSettings>
    **Changed**
      <smtp from="my@email.com">
        <network host="smtp.gmail.com" password="[password]" port="587" 
          userName="my@email.com"/>
      </smtp>
    **Original**
      <!--<smtp from="my@email.com">
        <network host="mail.domain.com" password="[password]" port="25"
          userName="my@email.com"/>
      </smtp>-->
</mailSettings>

My .Net C# code (before & after removed):

  SmtpClient mSmtpClient = new SmtpClient();
  mSmtpClient.EnableSsl = true;
  mSmtpClient.Send(message);

As I said this works great on my dev environment but not on web. Can anyone help? Thanks.

回答1:

Your settings are correct. We use gmail for sending mail all the time in our web apps. Your server is probably blocking outgoing traffic on port 587. I would contact your host and see if they can help otherwise you will need new mail or a new host.



回答2:

Thanks to everyone's help on this site as well as the Google apps forum (although I like this one better) I finally put together all the pieces of the puzzle. For whatever reason port 465 and port 587 would not work. This is what worked for me:

Web.config:

  <smtp from="pwretrieve@mydomain.com">
    <network host="smtp.gmail.com" password="[password]" port="25" 
      userName="pwretrieve@mydomain.com"/>
  </smtp>

from the aspx.cs file:

SmtpClient mSmtpClient = new SmtpClient();
mSmtpClient.EnableSsl = true;

Thanks again!



回答3:

I had the same problem for my form. My website is running on a Plesk control panel. All I did was to login to my panel and disable email hosting on my web server. After that my form started sending to a Google apps account. Try out what I did, I am sure you will get a difference.



回答4:

Failing a response from your hosting company and if you have another server you could send test requests to, try requesting connections to other ports and see what happens.



回答5:

Could be that the smtp client is not able to access the smtp server (might be disabled by the web host).

Ask the web host if they have a specific smtp server you should be using to send emails with.



回答6:

If your webhost doesn't allow you to send outgoing SMTP mail from their servers, that would cause this problem. For example, GoDaddy only allows you to send outgoing mail through smtpout.secureserver.net from your server, so any attempt to send mail through another host (in this case, smtp.gmail.com) would fail. If your ISP does not block outgoing SMTP (like Qwest does not, for example), then that's why this would work locally.

Check the FAQ with your webhost to see what they have to say about it. Most hosting companies do allow outgoing SMTP, but limit it to a certain number of relays/day, to prevent accidental exploitation for spam forwarding.

You're correct that the MX record on your domain only affects incoming mail. When somebody tries to send mail to you@yourdomain.com, they hand it to an SMTP server (most likely, the one their ISP gives them), and then SMTP server looks up your MX record to see who handles your email. It will resolve to smtp.gmail.com, so that's who will get your mail, and you get it from them. When you send outgoing mail, it can go through anybody, since you only care about the MX record for the destination domain (where the mail will eventually end up).

Does that make sense? If you'd like some clarification, I can find some tutorials and other explanations to help make sense of it.