Sending email from within Unity using smtp service

Hi am trying to send an email from inside unity on smartphones using the SmtpClient class. I have achieved this quite simple task on iOS but the exact same code does not work on android. Here’s the code:

private IEnumerator sendAutoMail (string textBody,string title)
		{
				MailMessage mail = new MailMessage();

				mail.From = new MailAddress(sender);
				mail.To.Add(receiver);
				mail.Subject = "R4UL " + title;
				mail.Body = textBody;

				Debug.Log("Connecting to SMTP server");
				SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
				smtpServer.Port = 587;
				smtpServer.Credentials = new System.Net.NetworkCredential(sender, password) as ICredentialsByHost;

				smtpServer.EnableSsl = true;
				ServicePointManager.ServerCertificateValidationCallback = 
						delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
				{ return true; };
				Debug.Log("Sending message");

				smtpServer.Send(mail);


				yield return null;
		}

where sender, password and receiver are my gmail account, my gmail password and the person I wish to email respectively.

So this works perfectly on iOS but on android throws the following error:

I/Unity ( 6256): SocketException: No such host is known
I/Unity ( 6256): at System.Net.Dns.GetHostByName (System.String hostName) [0
x00000] in :0
I/Unity ( 6256): at System.Net.Dns.GetHostEntry (System.String hostNameOrAdd
ress) [0x00000] in :0
I/Unity ( 6256): at System.Net.Dns.GetHostAddresses (System.String hostNameO
rAddress) [0x00000] in :0
I/Unity ( 6256): at System.Net.Sockets.TcpClient.Connect (System.String host
name, Int32 port) [0x00000] in :0
I/Unity ( 6256): at System.Net.Sockets.TcpClient…ctor (System.String hostna
me, Int32 port) [0x00000] in :0
I/Unity ( 6256): at System.Net.Mail.SmtpClient.SendInternal (System.Net.Mail
.MailMessage message) [0x00000] in :0
I/Unity ( 6256): at System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMes
sage message) [0x00000] in :0
I/Unity ( 6256): Rethrow as SmtpException: Message could not be sent.
I/Unity ( 6256): at System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMes
sage message) [0x00000] in :0
I/Unity ( 6256): at FeedbackSender+c__Iterator1.MoveNext ()

I have managed to pintpoint the error location at the line: smtpServer.Send(mail);

Any ideas as to what might be causing this issue? I have both android and iOS pro versions.

Cheers

I think I had the same problem some time, make sure your android build asks for Internet Access permission.

Go to Android Player Settings, “Other settings” and look for Internet Access. It’s “Auto” by default, set it to “Require”.