Unity 3 - Sending Email with C#

I have been looking all over the interwebs for a solution to this problem. I am trying to send an email through Unity and this is the code I currently have:

private void SendEmaill()
        // Create a System.Net.Mail.MailMessage object
        MailMessage message = new MailMessage();

        // Add a recipient

        // Add a message subject
        message.Subject = "Email message from Curtis sent by Unity";

        // Add a message body
        message.Body = "Test email";

        // Create a System.Net.Mail.MailAddress object and set the sender email address and display name.
        message.From = new MailAddress("curtisgmurray@gmail.com", "Curtis in Unity");

        // Create a System.Net.Mail.SmtpClient object and set the SMTP host and port number
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);

        //smtp.SendAsync(message, "Testing, from Curtis");

        // Create a System.Net.NetworkCredential object and set the username and password required by your SMTP account
        smtp.Credentials = new System.Net.NetworkCredential("curtisgmurray@gmail.com", "JuicyFruit");

        //Enable Secure Socket Layer (SSL) for connection encryption
        smtp.EnableSsl = true;

        // Do not send the DefaultCredentials with requests
        smtp.UseDefaultCredentials = false;        

        // Send the message

The error that Unity 3 is throwing is:

Cannot implicitly convert type `System.Net.NetworkCredential' to`System.Net.ICredentialsByHost'. An explicit conversion exists (are you missing a cast?)

I tried sending an email without using authentication to avoid using this line but gmail requires one to provide authentication. Looking around the web, no one appears to have this issue.

Anyone know what is going on with this bug?


Note: JuicyFruit is not the actual password to my email, I changed it for posting purposes but in my actual code, I have the correct password inputted in.

I spent over a day on this so I want to share my findings to make openURL or SMTPClient email sending work on iOS devices:

  • Set API Compatibility Level in Player Settings to NET 2.0 (not NET 2.0 Subset!)
  • Set Stripping Level in Player Settings to Disabled
  • After building the XCode project from Unity with the above settings, and then trying to run the build to device, you may get an error that there are duplicate symbols as a result of disabled stripping - manually comment out those symbols in the library that is conflicting. (for me, this was NGUI)

Try something like

smtp.Credentials = CredentialCache.DefaultNetworkCredentials;

Taken from: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.credentials.aspx


Or if you want to use your own email and password you can try something like this:

NetworkCredential myCred = new NetworkCredential(

CredentialCache myCache = new CredentialCache();

myCache.Add(new Uri("www.contoso.com"), "Basic", myCred);

WebRequest wr = WebRequest.Create("www.contoso.com");
smtp.Credentials = myCache;

Taken from: http://msdn.microsoft.com/en-us/library/system.net.networkcredential.aspx

Cannot implicitly convert type System.Net.NetworkCredential’ toSystem.Net.ICredentialsByHost’. An explicit conversion exists (are you missing a cast?)

This error can be fixed by in the Player Settings by choosing “API Compatibility Level” of .NET 2.0- not the subset. The subset does not include all the classes you need. When I built to iOS, I found that I also needed to reduce the stripping level.

Since it’s not in the .NET 2.0 subset, adding an explicit cast, as suggested by tgraupmann will result in the same error.

Additionally, a few other minor problems with your code. In order to work with Gmail, I believe you need to add something like this:

	ServicePointManager.ServerCertificateValidationCallback = 
			delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
			return true;

There’s an excellent discussion about sending via gmail’s smtp here: .net - Sending email through Gmail SMTP server with C# - Stack Overflow

smtp.Credentials = (ICredentialsByHost) new System.Net.NetworkCredential(“curtisgmurray@gmail.com”, “JuicyFruit”);

Simple and superb… Even this website http://www.compiletimeerror.com/2013/03/c-sharp-send-email-from-c-program.html addresses something similar… Have a look… May help…

Hello to all,

Thank you for everyone who has ever visited this question and replied to it. Many of your suggestions were very helpful, and my mind had slipped to close this question.

For those of you who still come here in the future looking for an answer, here was my solution code (Be mindful that the email address needs to be a gmail account):

MailMessage mail = new MailMessage();

mail.From = new MailAddress("[INSERT EMAIL HERE]");
mail.To.Add("[INSERT EMAIL HERE]");
mail.Subject = "[INSERT SUBJECT HERE]";

string body = "[INSERT BODY HERE]";

mail.Body = body;

SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Port = 587;
smtpServer.Credentials = new System.Net.NetworkCredential("[INSERT EMAIL HERE]", "[INSERT PASSWORD HERE]") as ICredentialsByHost;
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

Once again, thank you to all of you who posted answers :slight_smile: