Send Emails

2 Best ways to send emails from unity

Introduction

Have you ever wanted to send emails from your Unity indie game? It’s easy. This tutorial covers the 2 best possible ways without using any third-party plugins to send emails from Unity. Many games from Unity use email features to improve their stability, robustness, and bug fixes.

During playing your game, users might want to send you a direct email to provide you with any feedback or suggestion. Most game developers want their users to see them in the portfolio and share their experience with the development team. It is also used to secretly record crash logs, error messages, or suspicious player activity.

Email is the best thing to share user experience with the development team. Now the question is how to send emails from Unity.

Displaying an email address to the user might not be the best way to encourage the user to send emails to the development team. This tutorial covers the 2 best possible ways including direct email using an SMTP server and using the default email application.

2 Best Ways to Send Emails

  1. Using default application
  2. Using SMTP server

Using Default Application

Here’s how you can do it by using the default email application on your mobile device.

  1. Create a script called MailSender.cs in your unity project.
  2. Copy and paste the following code into the MailSender.cs class
  3. Create a button in Unity and set it to click listener with the SendMail functions. Don’t forget to set your email address.
  4. Build and Run.
public void SendEmail()
{
    string email = "MY EMAIL ADDRESS";
    string subject = MyEscapeURL("My Subject");
    string body = MyEscapeURL("My Body\r\nFull of non-escaped chars");
    Application.OpenURL ("mailto:" + email + "?subject=" + subject + "&body=" + body);
}

string MyEscapeURL (string URL)
{
    return WWW.EscapeURL(URL).Replace("+","%20");
}

Replace the email string with your actual email address. You can even collect more information on mobile devices using the SystemInfo class of Unity. Here is how you can do it in the body texts of your email.

//body of the mail which consists of Device Model and its Operating System
string body = MyEscapeURL("Please Enter your message here\n\n\n\n" +
              "________" +
              "\n\nPlease Do Not Modify This\n\n" +
              "Model: " + SystemInfo.deviceModel + "\n\n" +
              "OS: " + SystemInfo.operatingSystem + "\n\n" +
              "________");

You can collect more data like battery information, memory information, graphics card information, processor information, and much more. This way of sending emails is exposing the data you collect to the user. Here is another way to send emails from the user’s mobile devices without exposing the data to the user.

Using SMTP Server

In this tutorial, we are using the Gmail SMTP server to send emails from Unity. Before going forward make sure of the following things,

  1. Turn on less secure app access in your Gmail settings (If you get an SMTP exception)
  2. Go to your account’s security page
  3. Find App Passwords and create a new password for your game
  4. Store your newly created password
  5. Create a script called SmtpMailSender.cs
  6. Create a function inside the class called SendSmtpMail.
  7. Copy and paste the following code into the SendSmtpMail function.
  8. Resolve other library dependencies.
MailMessage mail = new MailMessage();

mail.From = new MailAddress("Fromaddress@gmail.com");

mail.To.Add("Toaddress@gmail.com");

mail.Subject = "Test Smtp Mail";

mail.Body = "Testing SMTP mail from GMAIL";

// you can use others too.
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");

smtpServer.Port = 587;

smtpServer.Credentials = new System.Net.NetworkCredential("youraddress@gmail.com", "yourpassword") as ICredentialsByHost;

smtpServer.EnableSsl = true;

ServicePointManager.ServerCertificateValidationCallback =

delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)

{ return true; };

try {
        smtpServer.Send( mail );
    }
catch ( System.Exception e ) 
    {
        Debug.Log( "Email error: " + e.Message );
    }
finally {
        Debug.Log( "Email sent!" );
    }

In the NetworkCredentials make sure you used your newly generated password. Replace the required email address in the script with your actual email address.

Note: Set API Compatibility Level to “.NET 2.0” and not “.NET 2.0 Subset” in the build setting if you are using a unity version less than 2017. Prior to Unity 2018 “.NET 4.0” is the default. This method doesn’t work on WebGL games.

Leave a Comment