Skip to content

Instantly share code, notes, and snippets.

@marcotmp
Last active August 28, 2025 10:04
Show Gist options
  • Save marcotmp/f6dc6cfcd2d6fb73978479d04bd7b0e5 to your computer and use it in GitHub Desktop.
Save marcotmp/f6dc6cfcd2d6fb73978479d04bd7b0e5 to your computer and use it in GitHub Desktop.
Code to send email from unity using a Google account.
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);
}
}
@marcotmp
Copy link
Author

marcotmp commented Apr 1, 2022

Note: If you are building for android, make sure you set Internet Access to Require.

Player -> Other Settings -> Internet Access: Require.
image

@AdeelKhan-04
Copy link

Your graciousness is deeply appreciated, sir.
As a fledgling in Unity C# game development, I find myself seeking knowledge.
Might I have the privilege of inquiring whether you maintain a distinguished YouTube channel that offers valuable guidance and expertise?
Your insights would be invaluable to a learner like me, eager to embark on this journey of mastery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment