Here are the 7 steps guide to send an email from your unity game using Gmail service. With Gmail mail service you can send email in unity3d.
2. Go to at this location https://myaccount.google.com/security
3. Go to the Less secure app access section and Turn on Allow less secure app access (see image below)
5. Attach the script with any gameobject in your hierarchy.
6. Provide the credential in the inspector in the script (like your account and password and receiver account, message body, etc)
7. Run the game, it will send the email to the desired address in the start function
Note: If your system is behind a proxy then, email may not be sent and throw an exception.
Steps for Gmail Account:
1. Login at Gmail Account2. Go to at this location https://myaccount.google.com/security
3. Go to the Less secure app access section and Turn on Allow less secure app access (see image below)
Steps for Unity
4. Create a Unity project (or in your existing project) Make a script with SendEmail name and Copy the Below code.
This file contains 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.Net; | |
using System.Net.Mail; | |
using System.Net.Security; | |
using System.Security.Cryptography.X509Certificates; | |
using UnityEngine; | |
public class SendEmail : MonoBehaviour | |
{ | |
public string fromEmail = "yourGmailAccountFromWhereYouWantToSendEmail"; | |
public string toEmail = "WhomYouWantToSendEmail"; | |
public string subject = "SubjectName"; | |
public string body = "Body of the email"; | |
public string password = "YourGmailAccountPassword"; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
EmailSending(); | |
} | |
// Update is called once per frame | |
void EmailSending() | |
{ | |
MailMessage mail = new MailMessage(); | |
mail.From = new MailAddress(fromEmail); | |
mail.To.Add(toEmail); | |
mail.Subject = subject; | |
mail.Body = body; | |
// you can use others too. | |
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com",587); | |
//smtpServer.Port = 587; | |
smtpServer.Credentials = new System.Net.NetworkCredential(fromEmail, password) as ICredentialsByHost; | |
smtpServer.EnableSsl = true; | |
ServicePointManager.ServerCertificateValidationCallback = | |
delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) | |
{ return true; }; | |
smtpServer.Send(mail); | |
} | |
} |
6. Provide the credential in the inspector in the script (like your account and password and receiver account, message body, etc)
7. Run the game, it will send the email to the desired address in the start function
Note: If your system is behind a proxy then, email may not be sent and throw an exception.
Unity Tips:
6 Comments
Thanks You
ReplyDeleteWelcome! Happy that it is helpful post!
DeleteThank you !!!
ReplyDeleteThis also works on Android devices?
Incredible, a question, if I want to send a image with the email?
ReplyDeleteHy, I followed the same step but its not working in android phone. how can I resolve this issue.
ReplyDeleteThank you !!
ReplyDelete