-
-
Save unitycoder/80cbcffcb722c13c29d2f696db63348d to your computer and use it in GitHub Desktop.
Code to send email from unity using a Google account.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel; | |
using System.Net; | |
using System.Net.Mail; | |
using System.Net.Security; | |
using System.Security.Cryptography.X509Certificates; | |
public class UnityGMail | |
{ | |
public void SendMailFromGoogle() | |
{ | |
MailMessage mail = new MailMessage(); | |
mail.From = new MailAddress("[email protected]"); | |
mail.To.Add("[email protected]"); | |
mail.Subject = "Email Subject"; | |
mail.Body = "Email Body"; | |
var attachment = new Attachment("C:\\path\\file.ext"); | |
mail.Attachments.Add(attachment); | |
SmtpClient smtp = new SmtpClient("smtp.gmail.com"); | |
smtp.Port = 587; | |
// Account Username: Usually your "from" email. | |
// App Password: If your account has a 2 step verification, you must follow these instructions | |
// to generate the password: https://support.google.com/accounts/answer/185833?hl=en. | |
smtp.Credentials = new NetworkCredential("Account Username", "App Password") as ICredentialsByHost; | |
smtp.EnableSsl = true; | |
ServicePointManager.ServerCertificateValidationCallback = | |
delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { | |
return true; | |
}; | |
smtp.Send(mail); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment