Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

How to Send Mail From Unity

Discussion in 'Scripting' started by Alexander21, Nov 8, 2018.

  1. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    220
    Hi All

    I am using unity 2018. In that i have to send mail from unity. I am using below coding for sending mail .

    But it is not sending mail

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Net;
    5. using System.Net.Mail;
    6.  
    7. public class Mail8 : MonoBehaviour {
    8.  
    9. // Use this for initialization
    10. void Start () {
    11.  
    12. MailMessage mail = new MailMessage();
    13. SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    14. mail.From = new MailAddress("MyMailId1982@gmail.com");
    15. mail.To.Add("ReceiverId@gmail.com");
    16. mail.Subject = "Test Mail 25";
    17. mail.Body = "mail with attachment";
    18.  
    19. //System.Net.Mail.Attachment attachment;
    20. //attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
    21. //mail.Attachments.Add(attachment);
    22.  
    23. SmtpServer.Port = 587;
    24. SmtpServer.Credentials = new System.Net.NetworkCredential("mygmail.com", "MyGmailPassword");
    25. SmtpServer.EnableSsl = true;
    26.  
    27. SmtpServer.Send(mail);
    28.  
    29. }
    30.  
    31.  
    32. // Update is called once per frame
    33. void Update () {
    34.  
    35. }
    36. }

    It Says. Socket error :timed Out... Whats wrong in that code.... What are the Ways we can send mail from unity ...

    I have also enabled the "Allow secure less apps" in my Gmail account.


    Thanks in Advance..
     
  2. boorb

    boorb

    Joined:
    Mar 18, 2016
    Posts:
    22
    Socket error timed out indicates it couldn't connect to the socket. It might be because the socket doesn't exist (wrong port number), is busy or you didn't use the right SSL configuration (there's probably more reasons as to why the socket timed out).

    1. I suggest you first try changing the port to 25 or 465, because that's what gmail uses (and not 587).
    2. If that doesn't work, I suggest you use telnet to connect to the socket and see if you can connect through that. In this case it'd look something like:
      openssl s_client -connect smtp.gmail.com:25 -starttls smtp or
      openssl s_client -connect smtp.gmail.com:465 -starttls smtp (because gmail uses that one)

    The last line of the response should be "250 SMTPUTF8", then you know the server is working and there's something wrong in your configuration.

    I'm assuming you edited the "mygmail.com" and "MyGmailPassword" to your own gmail and corresponding password.

    Good luck.
     
  3. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    220
    Thanks boorb for your reply. i have changed port no to 25. The actually the mail has been send from unity editor....I have tried in ipad. the mail is not send from ipad... Whats wrong in the code or settings. Here is my Code...

    Code (CSharp):
    1. public void Start() {
    2.  
    3. using (var mail = new MailMessage {
    4. From = new MailAddress(sender),
    5. Subject = "test subject",
    6. Body = "Hello there!"
    7. }) {
    8.  
    9. mail.To.Add(receiver);
    10.  
    11. var smtpServer = new SmtpClient(smtpHost) {
    12. Port = 25,
    13. DeliveryMethod = SmtpDeliveryMethod.Network,
    14. EnableSsl = true,
    15. UseDefaultCredentials = false,
    16.  
    17. Credentials = (ICredentialsByHost)new NetworkCredential(sender, smtpPassword)
    18. };
    19. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    20. smtpServer.Send(mail);
    21. }
    22. }
    23. }

    Sender and smtpPassword is my gmail id and my gmail password....
     
    Last edited: Nov 9, 2018
  4. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    220
    @boorb the above coding is working in android build when i changed the settings

    These are the things to try if SmtpClient is not working on Android.

    Go to File --> Build Settings... --> Select Android. Now, click on Player Settings.

    1.On the Internet Access, change it from Auto to Require.

    2.Make sure that API Compatible Level is set to .NET 2.0 not .NET 2.0 Subset.

    3.Make sure that Stripping Level is set to Disabled.

    4.Go to Player settings --> Android and change Internet Access from Auto to Require


    Mail is sending in Android Build. But ios build mail is not received.

    Internet Access & Stripping level Settings is not there in Unity Player Settings...


    For ios settings ... Internet Access & Stripping level is not there For that what i have to change.....
     
    Hypekraft likes this.
  5. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    220
    Finally I have found the solution for sending mail from unity ios...

    These are the things to try if SmtpClient is not working on Android.

    create a link.xml file and add the following code....


    <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>

    and execute the code... It will send the mail from unity App....
     
  6. xsocomx

    xsocomx

    Joined:
    Apr 18, 2018
    Posts:
    30
  7. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    197
    JeffDUnity3D likes this.
  8. unnanego

    unnanego

    Joined:
    May 8, 2018
    Posts:
    103
    somehow the solution stopped working for me...
    It worked when I created it, but now the function is called, I get a callback from SendAsync that the mail has been sent, but the mail never arrives and I get no errors. Running debugger doesn't help and it works fine in the editor...
    Please, advise, what could this be?
     
  9. unnanego

    unnanego

    Joined:
    May 8, 2018
    Posts:
    103
    It only works with managed stripping set to Low, Medium breaks SMTP (I still get a positive callback for some reason). Please, help! How do I understand what to add to the link file? Because the above lines are added and it still doesn't work.
     
  10. unnanego

    unnanego

    Joined:
    May 8, 2018
    Posts:
    103
  11. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    6,078
    Stopped working? What did you change? It works correctly with stripping set to Low? Do you require Medium stripping? What do you mean by a "link file"?
     
  12. unnanego

    unnanego

    Joined:
    May 8, 2018
    Posts:
    103
    I have the link.xml file with the necessary assemblies linked (see comments above). It works fine if managed stripping is set to Low, but it doesn't send the mail if stripping is set to Medium or High. At medium my app is more than 100 MB and less than 50 at Low, which is a pain( Obviously, something gets stripped away, but I am not getting any errors and I get the successfully sent callback (no errors of any kind) that comes immediately.
     
  13. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    6,078
    Might need to use a Proguard file.
     
  14. unnanego

    unnanego

    Joined:
    May 8, 2018
    Posts:
    103
    what's that? we are talking about iOS
     
  15. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    6,078
    Google it! And you never mentioned iOS, good to know.
     
  16. unnanego

    unnanego

    Joined:
    May 8, 2018
    Posts:
    103
    sorry, thought it was the iOS post, there's a number of them

    googling it gives me the anti piracy app, is it what you mean?
     
  17. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    6,078
    unnanego likes this.
  18. unnanego

    unnanego

    Joined:
    May 8, 2018
    Posts:
    103
  19. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    6,078
    Yes, you already made that point. Proguard is Android only.
     
  20. unnanego

    unnanego

    Joined:
    May 8, 2018
    Posts:
    103
    so no solution for iOS?
     
  21. xsocomx

    xsocomx

    Joined:
    Apr 18, 2018
    Posts:
    30
    I had this exact same problem and it was security on googles end make sure you allow access and clear any security warnings for locations.
     
  22. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    220
    @unnanego

    Did you test in Android different mobiles. I have faced this problem. in some mobiles i cant send mail. In Some mobiles the mail is not send from personal data. but it will work on wifi connection.

    in iphone also few iphone mobiles are not send mail ..

    90% mail is send from the code i have posted. because i have tested in various mobiles.

    so test with many mobiles.

    the code 100% not working.but 90% it is working. some issues are there. but i don't know how to find that.
     
  23. unnanego

    unnanego

    Joined:
    May 8, 2018
    Posts:
    103
    No, I only tested on my iPhone and a ZTE Nubia, works all the time, if stripping is set to low