sending email from GMail (or others) using Unitty

Hi Guys,

I am trying to write a script which would send me an email using my own Gmail. account. now, I’ve found loads of examples which uses the GMail account, but for whaterver reason I still have problems of authentication and/or credentials. So, is anybody of you that have a working example on how to send an email using C# ?

Kind Regards,
GC.

1 Like

Could you just use server + php mail or these,
http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page

Hey Thanks,

why that would be the better than using the System.net.mail in a C# script?

GC.

Are you using smtpclient? The name is usually “yourname@whateveraddress”, and be sure to set the port correctly, and set “EnableSSL” to true.

Note: Depending on your programs behavior, google can (and will) shut off your email account for “suspicious activity”. Happened to us at really low volume (we just send out reminders to a few account), so we just set up our own smtp server, problem solved.

Hi,
well you can see the script below. and I just copy and pasted what I 've found online. The problem is that I always get this exception:

System.Net.Mail.SmtpException: Message could not be sent. —> System.IO.IOException: The authentication or decryption has failed. —> System.InvalidOperationException: SSL authentication error: RemoteCertificateNotAvailable, RemoteCertificateChainErrors

and I am pretty sure about the authentication I provided.

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

public class SendEmail : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	
 	private void PrepareEmail() {
		MailMessage mail = new MailMessage();
		SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
		mail.From = new MailAddress("amatigianc@gmail.com");
		mail.To.Add("g.amati@ucl.ac.uk");
		mail.Subject = "Prova";
		
		SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
		SmtpServer.Port = 587;
		SmtpServer.Credentials = (ICredentialsByHost)CredentialCache.DefaultNetworkCredentials; //(ICredentialsByHost) new NetworkCredential("GMAILACCOUNT","PASSWORD");
		SmtpServer.EnableSsl = true;
		SmtpServer.Timeout = 20000;
		SmtpServer.UseDefaultCredentials = false;
		try {
			SmtpServer.Send(mail);
		} catch (SmtpException myEx) {
			Debug.Log ("Expection: \n" + myEx.ToString());
		}
		
		
		
	}
	
	void OnGUI() {
		
		if (GUI.Button(new Rect(0,0,100,100),"SendEmail") ) {
			PrepareEmail();
		}
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

As far as I know, you need to provide the actual credentials, not the default:

SmtpServer.Credentials = (ICredentialsByHost)CredentialCache.DefaultNetworkCredentials; //(ICredentialsByHost) new NetworkCredential(“GMAILACCOUNT”,“PASSWORD”);

Looks like you had that, but commented it out. What error did you get when you use your gmail credentials?

If this is just a cut and paste, and you’re not a programmer, that line above should be specifying the credentials for your gmail account:

SmtpServer.Credentials = new NetworkCredential(“Yourgmailaccount@gmail.com”,“yourpassword”);

Oh Yeah sorry, I do provide the actual credentials and the exception is at it follows:

System.Net.Mail.SmtpException: Message could not be sent. —> System.IO.IOException: The authentication or decryption has failed. —> System.InvalidOperationException: SSL authentication error: RemoteCertificateNotAvailable, RemoteCertificateChainErrors
at System.Net.Mail.SmtpClient.m__4 (System.Object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, SslPolicyErrors sslPolicyErrors) [0x00000] in :0
at System.Net.Security.SslStream+c__AnonStorey7.<>m__A (System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Int32[ ] certErrors) [0x00000] in :0
at Mono.Security.Protocol.Tls.SslClientStream.OnRemoteCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[ ] errors) [0x00000] in :0
at Mono.Security.Protocol.Tls.SslStreamBase.RaiseRemoteCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[ ] errors) [0x00000] in :0
at Mono.Security.Protocol.Tls.SslClientStream.RaiseServerCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[ ] certificateErrors) [0x00000] in :0
at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates) [0x00000] in :0
at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1 () [0x00000] in :0
at Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process () [0x00000] in :0
at (wrapper remoting-invoke-with-check) Mono.Security.Protocol.Tls.Handshake.HandshakeMessage:Process ()
at Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage (Mono.Security.Protocol.Tls.TlsStream handMsg) [0x00000] in :0
at Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult) [0x00000] in :0
— End of inner exception stack trace —
at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) [0x00000] in :0
— End of inner exception stack trace —
at System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMessage message) [0x00000] in :0
at SendEmail.PrepareEmail () [0x00081] in C:\Documents and Settings\ucwegam\My Documents\SendingEmail\Assets\SendEmail.cs:31
UnityEngine.Debug:Log(Object)

and yes, I changed with this line for the credentials.

SmtpServer.Credentials = (ICredentialsByHost) new NetworkCredential(“LOGIN”,“PASSWORD”);

I need to do the casting as otherwise I would get an error.

And strangely regarding the smtpServer.Port if I set it to 465, Unity Crashes or goes to an endless loop. If I set the value to 587, I get the exception :frowning:

I looked in our code. (C#, but not Unity). It’s very simple:

create the smtpclient for smtp.gmail.com, set EnableSsL to true. Set the port to 587. Set DeliveryMethod to Network.

Then create and send mail…

We use a google apps domain, so we specify the username as “username@ourdomain.com” (not just username) in the credentials.

If you’ve been testing sending email from this account, I suppose it’s possible that gmail locked it already. However, I don’t seem to recall that being the error we were recieving when that happened.

I guess you are right, Gmail might have blocked unity as I opened an account on Yahoo and everything works fine… can’t really understand why tho! :slight_smile:

thank you very much for your help!

with unity 4.1.2 I’ve sucseed with this code:

using UnityEngine;
using System;
using System.Net;
using System.Net.Mail;
using System.Collections;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
//TestingUnity3DEmail
public class SendEmail : MonoBehaviour {

	void Start(){
		SendEmailF();
	}
	private void SendEmailF(){
		MailMessage mail = new MailMessage();
		
		mail.From = new MailAddress("Your.Email@gmail.com");
		mail.To.Add("Their.Email@gmail.com");
		mail.Subject = "Test Mail Subject";
		mail.Body = "This is for testing SMTP mail from GMAIL";
		
		SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
		smtpServer.Port = 587;
		smtpServer.Credentials = new System.Net.NetworkCredential("Your.Email@gmail.com", "Password") as ICredentialsByHost;
		smtpServer.EnableSsl = true;
		ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors){
			return true;
		};
		smtpServer.Send(mail);
		Debug.Log("success");
	}
}

and the 1 that helped me:

Unity Answers

I looked everywhere and thanks to you it is working now. RESPECT!

Please read this who has experiencing issues with authentication
http://stackoverflow.com/questions/9801224/smtpclient-with-gmail

Friends a simple SMTP mail with attachment.

Reviving an old post, for others looking for a solution using gmail, after testing this (and it failing) check the inbox of the account you’re using, you should get an email telling you that a connection was blocked, and you have to activate smpt connections in your gmail settings. The email from gmail will link you to the setting!

Hi,

I want to send a mail in unity3d using the IMAP or POP3 or etc but not using SMTP ,
I want to send mail using IOS device in which SMTP server is not working properly.

it is working fine for Gmail

but I want this thing for the webmail

I tried but it is not working can anyone help me how to change the things

hi, the Gmail thing is working fine

how can I add my webmail

Please define “webmail”