用C#透過Google的SMTP Server進行send email

Send Email through Google SMTP Server

SmtpClient client = new SmtpClient();

client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = TRUE;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential([Your Gmail Account], [Your Gmail Password]);

MailMessage message = new MailMessage();

message.From = fromAddress;
message.IsBodyHtml = true;
message.Subject = mailSubject;
message.Body = mailContent;
message.To.Add(new MailAddress([Email receiever]));
message.CC.Add(new MailAddress([CC Email Address]));
message.Bcc.Add(new MailAddress([Bcc Email Address]));
message.Attachments.Add(new Attachment([Attachment location(String Format)]));

client.Send();
message.Dispose();

***You may need to Enable "Apps with account access" -> Allow less secure apps to ON in Google Account Setting (Sign-in & security)

***Google is not supporting SSLv3 / RC4 Since June 16(2016)
Changes may take to keep using port 465
You may simply make changes by the "Advanced" tag in Internet Options on IE.
Google to shutter SSLv3, RC4 from SMTP servers, Gmail

SSL connections overview(On G Suite Administrator Help)
Check your .Net Framework supporting TLS
The outgoing SMTP server, smtp.gmail.com, requires TLS. Use port 465, or port 587 if your client begins with plain text before issuing the STARTTLS command.

 

Extra Information:
Extra Information about Port Number