How To send Email with C# on Unity 3D 4.1.2?

first I came across this article:

Dave On C# Programming tutorials

I got few errors and I start searching:

Unity 3 - Sending Email with C#

but I just cannot figure out how to solve this:

using UnityEngine;
using System.Net;
using System.Net.Mail;
using System.Collections;

public class SendEmail : MonoBehaviour {
	void Start () {
	private void SendEmailF(){
		// Create a System.Net.Mail.MailMessage object
		MailMessage message = new MailMessage();
		// Add a recipient
		// Add a message subject
		message.Subject = "Test Email Subject message";
		// Add a message body
		message.Body = "Test email Body message.";
		// Create a System.Net.Mail.MailAddress object and 
		// set the sender email address and display name.
		message.From = new MailAddress("", "John Smith");
		// Create a System.Net.Mail.SmtpClient object
		// and set the SMTP host and port number
		SmtpClient smtp = new SmtpClient("", 587);
		// If your server requires authentication add the below code
		// =========================================================
		// Enable Secure Socket Layer (SSL) for connection encryption
		smtp.EnableSsl = true;
		// Do not send the DefaultCredentials with requests
		smtp.UseDefaultCredentials = false;
		// Create a System.Net.NetworkCredential object and set
		// the username and password required by your SMTP account


//		smtp.Credentials = new NetworkCredential("", "Password");
				// Assets/SendEmail.cs(43,22): error CS0266: Cannot implicitly convert type `System.Net.NetworkCredential' to `System.Net.ICredentialsByHost'. An explicit conversion exists (are you missing a cast?)


//		smtp.Credentials = (ICredentialsByHost) new NetworkCredential("", "Password");
				// wow no error but when I run the code:
				// InvalidOperationException: SSL authentication error: RemoteCertificateNotAvailable, RemoteCertificateChainErrors
				// System.Net.Mail.SmtpClient.<callback>m__4 (System.Object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, SslPolicyErrors sslPolicyErrors)
				// System.Net.Security.SslStream+<BeginAuthenticateAsClient>c__AnonStorey7.<>m__A (System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Int32[] certErrors)
				// Mono.Security.Protocol.Tls.SslClientStream.OnRemoteCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[] errors)
				// Mono.Security.Protocol.Tls.SslStreamBase.RaiseRemoteCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[] errors)
				// Mono.Security.Protocol.Tls.SslClientStream.RaiseServerCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[] certificateErrors)
				// Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates)
				// Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1 ()
				// Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process ()
				// (wrapper remoting-invoke-with-check) Mono.Security.Protocol.Tls.Handshake.HandshakeMessage:Process ()
				// Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage (Mono.Security.Protocol.Tls.TlsStream handMsg)
				// Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult)
				// Rethrow as IOException: The authentication or decryption has failed.
				// Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult)
				// Rethrow as SmtpException: Message could not be sent.
				// System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMessage message)
				// SendEmail.SendEmailF () (at Assets/SendEmail.cs:59)
				// SendEmail.Start () (at Assets/SendEmail.cs:9)


//		NetworkCredential myCred = new NetworkCredential("","Password");
//		CredentialCache myCache = new CredentialCache();
//		myCache.Add(new Uri(""), "Basic", myCred);
//		WebRequest wr = WebRequest.Create("");
//		smtp.Credentials = myCache;
				// Assets/SendEmail.cs(69,33): error CS0246: The type or namespace name `Uri' could not be found. Are you missing a using directive or an assembly reference?
				// Assets/SendEmail.cs(69,25): error CS1502: The best overloaded method match for `System.Net.CredentialCache.Add(System.Uri, string, System.Net.NetworkCredential)' has some invalid arguments
				// Assets/SendEmail.cs(69,25): error CS1503: Argument `#1' cannot convert `object' expression to type `System.Uri'

the rest of the code

		// =========================================================
		// Send the message

it’s all in 1 BIG script but I only uncomment a line after 1,2,3 try and get an error that’s commented

I made it like this to look a little bit more organized

and I’m using Unity 3D 4.1.2

Adapted from c# - authentication or decryption has failed when sending mail to GMail using SSL - Stack Overflow

using UnityEngine;
using System.Collections;
using System;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

public class mono_gmail : MonoBehaviour {

        void Main ()
            MailMessage mail = new MailMessage();

            mail.From = new MailAddress("");
            mail.Subject = "Test Mail";
            mail.Body = "This is for testing SMTP mail from GMAIL";

            SmtpClient smtpServer = new SmtpClient("");
            smtpServer.Port = 587;
            smtpServer.Credentials = new System.Net.NetworkCredential("", "yourpassword") as ICredentialsByHost;
            smtpServer.EnableSsl = true;
            ServicePointManager.ServerCertificateValidationCallback = 
                delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
                    { return true; };

This will send an email to a gmail account.
Create a c# file in unity and name it ‘mono_gmail’.
Don’t forget to replace the email address with your own and of course add your password :slight_smile:

Better use

client.SendAsync(message , "");

especially if you want to send some more stuff like attachments and so in :wink:

A simple smtp email

            mail.From = new MailAddress("");
            mail.Subject = "Test Mail";
            mail.Body = "This is for testing SMTP mail from GMAIL";

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
            SmtpServer.EnableSsl = true;

source: How to send email from C# c# smtp


the above code by # merry_christmas works in standalone as well as in editor but it doesn’t have any effect when build on webGL. can anyone tell what change has to be done to make it work?

Unfortunately, Unity uses an outdated and buggy Mono 2.0 SmtpClient, which doesn’t fully support STARTTLS protocol used my many modern SMTP servers. We’ve recently published a new asset UTMail - Email Composition and Sending Plugin, which doesn’t have that issue and so works well with most SMTP servers (we’ve tested it with number of popular ones). It also allows not only directly sending emails with SMTP, but also composing emails using a system email client, with a content defined by your app. It works on multiple platforms and supports attachments and HTML / text messages.

Best regards,

Yuriy, Universal Tools team.

the accepted answer partially works now what needs to be added is:


the Google doesn’t let the less secure apps to connect to it unless you change it in this page.

hope someone needs this

My Updated version:

public void SendMail(string mailBody = "Test Mail Body", string mailSubject = "Report Mail", string 
         sendTo = "", string sendFrom = "", string 
         sendFromPass = "account2's password")
        MailMessage mail = new MailMessage {
            From = new MailAddress(sendFrom)

        mail.Subject = mailSubject;
        mail.Body = mailBody;

        SmtpClient smtpServer = new SmtpClient("")
            Port = 587,
            Credentials = new NetworkCredential(sendFrom, sendFromPass),
            EnableSsl = true
        ServicePointManager.ServerCertificateValidationCallback =
            delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            { return true; };

        Debug.Log("Succesfully Sent! >>>" + sendTo + " Body: " + mailBody);