Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Sending email on android device

Discussion in 'Scripting' started by singlecoder, Sep 3, 2013.

  1. singlecoder

    singlecoder

    Joined:
    Jul 19, 2013
    Posts:
    16
    Hi,

    I try to send email using c#.net.mail library. Emails can be sent to my email address seamlessly when i working with unityeditor but cannot be sent if i push the project to the android device. I'm using free version of unity3d. What the problem may be related to?

    My code is below;

    Code (csharp):
    1. using System;
    2. using System.Net;
    3. using System.Net.Mail;
    4. using System.Net.Security;
    5. using System.Security.Cryptography.X509Certificates;
    6.  
    7. public class EMail {
    8.  
    9.     public static void Send(string message) {
    10.  
    11.         MailMessage mail = new MailMessage();
    12.         mail.From = new MailAddress("mymailaddress@gmail.com");
    13.         mail.To.Add("mymailaddress@gmail.com");
    14.         mail.Subject = "Subject";
    15.         mail.Body = message;
    16.  
    17.         SmtpClient smtp = new SmtpClient("smtp.gmail.com");
    18.         smtp.Port = 587;
    19.         smtp.Credentials = new System.Net.NetworkCredential("mymailaddress@gmail.com", "mypassword") as ICredentialsByHost;
    20.         smtp.EnableSsl = true;
    21.  
    22.         ServicePointManager.ServerCertificateValidationCallback =
    23.                 delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
    24.                     return true;
    25.                 };
    26.         smtp.Send(mail);
    27.     }
    28. }
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    883
  3. singlecoder

    singlecoder

    Joined:
    Jul 19, 2013
    Posts:
    16
    thansk for reply :)

    I checked ".NET2.0" instead of ".NET2.0 Subset" in the player settings and problem is solved.
     
  4. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    883
    It's great!
     
  5. rjr

    rjr

    Joined:
    Jan 10, 2014
    Posts:
    7
    Above code i have copy pasted, even after changing to ".NET2.0" i'm not able to send mails through android device, it works fine in unity Editor. What else changes i have to do in player settings?
     
  6. RafayAli

    RafayAli

    Joined:
    Mar 31, 2015
    Posts:
    6
    I have tried the same above code and changed the assembly to .NET 2.0 from .NET 2.0 Subset, but still i am unable to send from android where as i can send using unity editor without problems. Can anyone help what am i missing?
     
  7. zeynepp

    zeynepp

    Joined:
    Nov 23, 2014
    Posts:
    1
    I have also had the same problem, changed the assembly to .NET 2.0 and was unsuccessful at first. But, when i changed to development build, then it worked suddenly. I am not sure why this is working in development build but not the final release, but i suppose that it is something about internal android security. It is needed to be investigated, but if you will not publish your application but will use on a limited hardware then this may be a work around.
     
    saikrishna-dasari likes this.
  8. Mikenekro

    Mikenekro

    Joined:
    May 11, 2014
    Posts:
    13
    Even though this is an old post, I am going to post my solution for anybody else having this problem that needs a fix for a full release.

    I got this to work without development build in .NET 2.0. It was such a simple fix, yet so complicated hah.

    so the problem was in actually sending the mail message in
    Code (CSharp):
    1. smtp.Send(mail);
    but this was not why it didn't send.

    I believe, but i'm not 100% sure, it didn't send because
    Code (CSharp):
    1. SmtpClient smtp = new SmtpClient("smtp.gmail.com");
    2. smtp.Port = 587;
    will setup the
    Code (CSharp):
    1. smtp.gmail.com
    host in the editor, but when built for android, somehow it is skipping over that bit of information and doesn't know which host to connect too.

    With that said, the only thing I needed to change to make this work for Android full release was change that last bit of code to:
    Code (CSharp):
    1. SmtpClient smtp = new SmtpClient();
    2. smtp.Host = "smtp.gmail.com";
    3. smtp.Port = 587;
    It works for me if anybody else wants to try this.
    along with the .NET 2.0 under player settings, I selected to option to require internet access

    Also I added this bit of code to make sure it was sending the email over the network.
    Code (CSharp):
    1. smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    Please correct me if I am wrong anywhere.
     
  9. Pollibitas

    Pollibitas

    Joined:
    Mar 19, 2015
    Posts:
    1
    Hello Mike, I try to do what you said, but it won't work, any tips ? best

    here is my code:

    Code (CSharp):
    1. void sendEmail () {
    2.         MailMessage mail = new MailMessage();
    3.        
    4.         mail.From = new MailAddress("xxxxxx@gmail.com");
    5.         mail.To.Add("xxxxxxx@gmail.com");
    6.         mail.Subject = System.DateTime.Now.ToString("MM/dd/yyyy");
    7.         mail.Body = SaveClientInformation.getClientsInString ();
    8.        
    9.         SmtpClient smtpServer = new SmtpClient();
    10.         smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
    11.         smtpServer.Host = "smtp.gmail.com";
    12.         smtpServer.Port = 587;
    13.         smtpServer.Credentials = new System.Net.NetworkCredential("icadunas@gmail.com", "xxxxxxx") as ICredentialsByHost;
    14.         smtpServer.EnableSsl = true;
    15.         ServicePointManager.ServerCertificateValidationCallback =
    16.             delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    17.         { return true; };
    18.         smtpServer.Send(mail);
    19.         SaveClientInformation.clearSavedClients();
    20.         Debug.Log("success");
    21.     }
     
  10. mark-pollard

    mark-pollard

    Joined:
    Apr 2, 2013
    Posts:
    14
    I did get this to work on my android build using the code above and the .net ticked. Now i want to send an attachment from the android build. But i cannot access the atatchment file. I think you can use WWW but i am unsure how to do this.

    in unity i can use

    System.Net.Mail.Attachmentattachment;
    attachment=new System.Net.Mail.Attachment(Application.dataPath+"/StreamingAssets/test.rtf");
    mail.Attachments.Add(attachment);

    it works but i need to alter it to work in android.

    Can Anybody advise?
     
    Last edited: Dec 20, 2015
  11. inakiktp

    inakiktp

    Joined:
    Nov 6, 2014
    Posts:
    26
    Hey @mark pollard

    I use the following method to attach images to an email which is sent from an Android device, hope it helps you. :)

    Code (CSharp):
    1. private void AddAttachments (List<string> attachmentsPath) {
    2.         // Clear Attachments List
    3.         _email.Attachments.Clear ();
    4.         // "Attachments" is a AttachmentCollection that cannot be SET
    5.         for (int index = 0; index < attachmentsPath.Count; index++) {
    6.             try {
    7.                 System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment (attachmentsPath[index]);
    8.                 _email.Attachments.Add (attachment);
    9.             } catch (Exception ex) {
    10.                 Debug.Log("Exception Error: " + ex);
    11.                 //break;
    12.             }
    13.         }
    14.     }
    The contents of "attachmentsPath" is the strings obtained after using:
    Code (CSharp):
    1. string _attachmentPath = Application.persistentDataPath + "/YourPathToYourAttachment/";
    Note the previous line of code returns the path to one attachment, not all of them. If you want to get all of them you should be reading the folder where your attachments are and adding them to a List<string>.
     
    Last edited: Jan 25, 2016
  12. saikrishna-dasari

    saikrishna-dasari

    Joined:
    Mar 22, 2016
    Posts:
    1
    Dear patico,
    I've changed ".NET2.0" instead of ".NET2.0 Subset" in the player settings,
    Still the problem was continues.
    Can u tell me What i've to change
     

    Attached Files:

  13. MayerBryan

    MayerBryan

    Joined:
    Jan 17, 2015
    Posts:
    64
    I have the same problem, send email only work on android if the game is on development build.. someone have a solution?
     
  14. OrangeCanine

    OrangeCanine

    Joined:
    Oct 29, 2016
    Posts:
    2
    Here are some things that I ran into with this. You are probably missing the Android Manifest File for permissions because developer's version doesn't need it while releasing does.

    1) .Net 2.0
    2) stripping to disabled.
    3) change the Android manifest (xml document) to include permissions for internet and WIFI (if you do not change the manifest it will not have the ability to send because you didnt give the phone the ability too)

    with that being said. With those setting I am able to send an email on many android systems, such as Google Pixel, Amazon Kindle, LGE 4G, and Samsung SM, but certain android devices fail to deliver a message everytime on devices like the Galaxy 6. If anyone has anything else let me know but those are things I found out that helped.
     
  15. Mouledoux

    Mouledoux

    Joined:
    Oct 10, 2016
    Posts:
    1
    I know it's a bit late but I thought I would still thank OrangeCanine for their suggestion. In case anybody else needs it my code is below in C#. The problem for me was the striping level. .Net was set to 2.0, not subset, and I did not need to change the manifest. This did work on a Galaxy s4, s6, and Note 5.

    Code (CSharp):
    1.     void SendMail(string aFrom, string aTo, string aSubject, string aBody, string aPassword)
    2.     {
    3.         if (!aTo.Contains("@") && !aTo.ToLower().Contains(".com"))
    4.             return;
    5.  
    6.         MailMessage mail = new MailMessage();
    7.  
    8.         mail.From = new MailAddress(aFrom);
    9.         mail.To.Add(aTo);
    10.         mail.Subject = aSubject;
    11.         mail.Body = aBody;
    12.  
    13.         SmtpClient smtpServer = new SmtpClient();
    14.         smtpServer.Host = "smtp.gmail.com";
    15.         smtpServer.Port = 587;
    16.         smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
    17.         smtpServer.Credentials = new System.Net.NetworkCredential(aFrom, aPassword) as ICredentialsByHost;
    18.         smtpServer.EnableSsl = true;
    19.         ServicePointManager.ServerCertificateValidationCallback =
    20.             delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    21.             { return true; };
    22.         smtpServer.Send(mail);
    23.     }
     
  16. ovirta

    ovirta

    Joined:
    Mar 20, 2015
    Posts:
    26
    Even later comment on this but thought I'd share what we found out as well. Only change we needed to do was to change Player settings - Android - Internet Access -> Require. This adds the following line to AndroidManifest.xml:
    <uses-permission android:name="android.permission.INTERNET"/>
    After this emails are coming to gmail account. Unity editor 5.5.0f3 with Samsung Galaxy Tab 4 used in testing.