- Home /
How to send email using smtp to IOS?
I've tried everything I've seen that is somehow related to my question (in this forum) and I can't manage to send an email in an IOS device. It works in the Unity Editor and on Android devices.
Below is my code->
public void Send() { MailMessage mail = new MailMessage();
mail.From = new MailAddress(senderEmail);
mail.To.Add(myEmail);
mail.Subject = "Test Mail";
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(senderEmail, senderPassword) as ICredentialsByHost;
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback =
delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
smtpServer.Send(mail);
Debug.Log("success");
}
Why is it not working in IOS?
Answer by catiejojo · Oct 29, 2018 at 01:25 PM
Apologies for poking an old thread, but in case anyone gets here from search, the mono scripting backend isn't supported from iOS 11 and up. You should still set the API to .NET 2.0 (not subset), and add a link.xml file to your Assets folder with the following text:
<linker>
<assembly fullname="System">
<type fullname="System.Net.Configuration.MailSettingsSectionGroup" preserve="all"/>
<type fullname="System.Net.Configuration.SmtpSection" preserve="all"/>
<type fullname="System.Net.Configuration.SmtpNetworkElement" preserve="all"/>
<type fullname="System.Net.Configuration.SmtpSpecifiedPickupDirectoryElement" preserve="all"/>
</assembly>
</linker>
This is because the IL2CPP scripting backend strips this out of the build, so you need to manually link it. Hope this helps!
Hello @catiejojo!
So all I have to do is create a new text file, change its extension to .xml, copy paste the text inside it and the place it in the assets folder?
You need to create a new xml file (.xml), not a text file (.txt), but yes then it should work!
Hi. I made a small iOS app in which I desperately try to send mail via S$$anonymous$$TP (works fine in Unity editor). I tried your solution. But when I build in Xcode, I obtain errors and the build fails ; cannot install and run on my iPhone. Seems like it doesn't want of the API change. I'm using Unity 2018.3 and Xcode 10.1. Thanx for any help
Answer by Devin_Stewart · Jun 16, 2017 at 10:48 PM
Hey Naharadir,
Not sure if you're still looking for an answer to this, but I'll post my solution here just in case.
I am having success with SMTP emails on IOS by changing the Scripting backend and Api compatibility level options in Edit --> Project Settings --> Player --> Settings for IOS tab --> Other Settings.
Change the scripting backend to Mono2x.
And Change the Api Compatibilty Level to .NET 2.0.
Hope that helps!
-Devin Stewart