How to send email from unity3d

Send Email from unity

How to send email from unity? Don’t know. It’s too easy. Many games from unity use email features to improve its stability, robustness and bug fixes.

User’s feedback or suggestions are very important nowadays. Most of the game developers want their user to see them in portfolio and share their experience with the development team.

Email is the best thing to share user experience with the development team. Now again the question is how to send email from unity?.

We can display our email address to the user, but do we get feedback by just displaying email address? The answer is no. So, what can we do to get regular feedback from a user?.

Do something that user easily send email regularly without interrupts to their actual game play. Is it possible to send email from unity by just one click? Yes. You can do this by following two methods.

  1. Open default email application and send email
  2. Send email using SMTP server

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

Send email using default application

  1. Create an empty unity project.
  2. Generate a Script called MailSender.cs
  3. Create a function called SendEmail in Mailsender Class.
  4. Copy and paste the following code into the SendEmail function.
  5. Create a button in unity and set on click listener with the SendMail functions. Don’t forget to set your email address.
  6. 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");
}

Send email using SMTP server

Send Email using SMTP is really simple and you can do it in unity using System.Net.Mail library.

  1. Create an Empty Unity project.
  2. Generate a script called SmtpMailSender.cs
  3. Create a function inside the class called SendSmtpMail.
  4. Copy and paste the following code into the SendSmtpMail function.
  5. 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; };

smtpServer.Send(mail);

Note: Set API Compatibility Level to “.NET 2.0” and not “.NET 2.0 Subset” in build setting is you are using unity version less than 2017. Prior to unity 2018 “.NET 4.0” is the default.

Leave a Reply