简体   繁体   中英

Sending email with google SMTP in .NET

I'm trying to send a couple of emails using google SMTP server. I've been researching how to and have found plenty of articles on stackoverflow and other resources where people successfuly send email - I haven't been able to do so. Here's the code I've been using trying to contact the SMTP server:

var secureString = new SecureString();
foreach (char c in "password")
{
      secureString.AppendChar(c);
}

var client = new SmtpClient("smtp.gmail.com", 587)
{
      UseDefaultCredentials = false,
      DeliveryMethod = SmtpDeliveryMethod.Network,
      Credentials = new NetworkCredential("myaddress@gmail.com", secureString),
      EnableSsl = true
};

client.Send("myaddress@gmail.com", "myaddress@gmail.com", asdf", "asdf");

The exception I'm getting is saying that it's unable to contact the remote server. The innermost exception says:

"An attempt was made to access a socket in a way forbidden by its access permissions 64.233.161.109:587"

Any suggestions on what else to try?

I've tried this on three different network to ensure that this isn't a firewall issue. Using my cell phone as a hotspot I've had the same problen, which should mean that it isn't a company firewall.

It is about a configuration on your gmail account to allow access with your @dress and your password from other application. So you have to activate it.

I am answering my own question, which might be a specific answer for my machine but could help others in a similar situation.

My issue was my antivirus software, Macfee, which helpfully decided that trying to send email trafic is probably a virus and thus quietly dropped the trafic from my application. I checked pretty much everything else as I thought that an antivirus would never care about ports (typically a firewall feature imo) but I guess some do...

Anyway the issue is resolved and if anyone else has got the same issue and can't explain it, check your anti virus software...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM