Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Giusort · May 11, 2016 at 11:02 AM · c#emailattachment

Email attachment on Android & iOS

Hi! I'm trying to send an email using google server with this code:

 MailMessage mail = new MailMessage();
         
         mail.From = new MailAddress("MAILFROM");
         mail.To.Add("MAILTO");
         mail.Subject = "Test Mail Subject";
         mail.Body = "Test Mail Body";
 
         SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
         smtpServer.Port = 587;
         smtpServer.Credentials = new System.Net.NetworkCredential ("LOGIN", "PASSWORD");
         smtpServer.EnableSsl = true;
         ServicePointManager.ServerCertificateValidationCallback = 
             delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
             return true;
             };
         try {
             smtpServer.Send(mail);
             } catch (Exception e) {
                 Debug.Log (e.GetBaseException ());
             }


and it works.

Now I would add an attachment that is in StreamingAssets folder; I tryed in dozens of ways, like

 System.Net.Mail.Attachment attachment;
 attachment = new System.Net.Mail.Attachment (string.Format (@"Assets/StreamingAssets/{0}", "FILENAME"));
 mail.Attachments.Add(attachment);

but... NOTHING, no email sent.

Some idea?

Thank you!

EDIT:

Solved this way:

 string FilePath = "";
 string AttachmentName = "ATTACHNAME";
 string FileName = "";
 
 #if UNITY_EDITOR
             FilePath = string.Format(@"Assets/StreamingAssets/{0}", AttachmentName);
         #else
             FilePath = Application.persistentDataPath + "/" + AttachmentName;
             if(!File.Exists(FilePath)) {
                 WWW loadImage = new WWW("jar:file://" + Application.dataPath + "!/assets/" + AttachmentName);
                 while(!loadImage.isDone) {}
                 File.WriteAllBytes(FilePath, loadImage.bytes);
             }
         #endif
         
         FileName = FilePath;
 
         MailMessage mail = new MailMessage();
         
         mail.From = new MailAddress("FROMEMAIL");
         mail.To.Add("TOEMAIL");
         mail.Subject = "eMail Subject";
         mail.Body = "eMail Body";
 
         Attachment data = new Attachment(FileName, System.Net.Mime.MediaTypeNames.Application.Octet);
             // Add time stamp information for the file.
             System.Net.Mime.ContentDisposition disposition = data.ContentDisposition;
             disposition.CreationDate = System.IO.File.GetCreationTime(FileName);
             disposition.ModificationDate = System.IO.File.GetLastWriteTime(FileName);
             disposition.ReadDate = System.IO.File.GetLastAccessTime(FileName);
 
         mail.Attachments.Add(data);
 
         SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
         smtpServer.Port = 587;
         smtpServer.Credentials = new System.Net.NetworkCredential ("LOGIN", "PASSWORD");
         smtpServer.EnableSsl = true;
         ServicePointManager.ServerCertificateValidationCallback = 
             delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
             return true;
             };
         try {
             smtpServer.Send(mail);
             } catch (Exception e) {
                 Debug.Log (e.GetBaseException ());
             }


Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KeOt777 · May 25, 2017 at 09:42 PM 0
Share

Hello! Thanks for the code, just a quick question, I got an Exception on smtpServer.Send(mail) the exception reads

System.IO.IOException: Connection closed

Any ideas?

avatar image MashaelA · Apr 06, 2019 at 05:54 PM 0
Share

Hello @$$anonymous$$ort, thank you for your code, I tried it and the email sent successfully but the attachment file can’t be opened by the receiver I am wondering why?

Thanks!

5 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by grobm · Sep 28, 2017 at 02:00 AM

Issue with this method is that some mail servers require second authentication (confirm by logging in to gmail). then this works for a period and then the user is prompted to do it again... On iOS they do not get a prompt and typically I find the iOS runtime crashes. (Unity 2017.1.1p1)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image grobm · Sep 28, 2017 at 02:01 AM 0
Share

Using a proxy mail server method (php, asp, etc.) your sending the message via the mail server not client.

avatar image
0

Answer by garethlamyh · Mar 31, 2018 at 03:16 AM

Thank you so much for your codes! I am writing an android app which exports .STL file. And I would like to send the exported file as an attachment to an email account. I kept struggling until I found your codes. Thanks again! It helps me a lot!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Azaberym · Apr 26, 2018 at 02:55 PM

Hey, I tried this but I got an error : "System.Net.Sockets.SocketException : A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond."

I tried with an exchange mail service with no success so I tought that it could work with google but nothing changed...

Here is the code I use (nearly the same as above):

     MailMessage mail = new MailMessage ();
     mail.From = new MailAddress(adresseSTR);
     mail.To.Add (destinataire);
     mail.Subject = "Justificatif Arrêt Maladie";
     mail.Body = textMail;

     Attachment data = new Attachment (photoPath, System.Net.Mime.MediaTypeNames.Image.Jpeg);

     mail.Attachments.Add (data);


     SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
     smtpServer.Port = 587;
     smtpServer.Credentials = new System.Net.NetworkCredential (adresseSTR, passwordSTR);
     smtpServer.EnableSsl = true;
     ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
         return true;
     };
     try{
         smtpServer.Send(mail);
     }catch(Exception e){
         Debug.Log ("Erreur à l'envoie");
         Debug.Log (e.GetBaseException ());
     }

If anyone got an idea on what's happening here... I'm lost. It would be so nice if Application.OpenURL("mailto:...") would work with attachements...

Thanks!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by MashaelA · Apr 14, 2019 at 05:26 PM

hello, i used the same code but the attachment sent is empty ,does anyone know why this happens?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UnityQueries · Aug 20, 2021 at 01:51 PM 0
Share

Hi, I am working on sending an email with an attachment from Unity using Gmail SMTP servers but I am getting the following exception:

Exception : System.Net.Mail.SmtpException: Server does not support secure connections.

I have tried by setting SMTP Port 587,25,465 and enabled EnableSsl = true but still I am getting above exception.

Can you anyone provide a solution for this error.

Thank in advance.

avatar image
0

Answer by Mayank516 · Sep 22, 2021 at 06:19 PM

You can use the following plugin from asset store for iOS and android. Plugins are cheap and developer provides really nice support.

iOS : https://assetstore.unity.com/packages/tools/email-composer-in-ios-45218?_ga=2.188046824.1112218515.1632332140-784483838.1626851333

ANDROID: https://assetstore.unity.com/packages/tools/integration/email-composer-in-android-109359

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Send email with attachment 1 Answer

Sending email with Smtp client not working on Android 0 Answers

Initialising List array for use in a custom Editor 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges