- Home /
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 ());
}
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?
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!
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)
Using a proxy mail server method (php, asp, etc.) your sending the message via the mail server not client.
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!
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!
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?
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.
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.
ANDROID: https://assetstore.unity.com/packages/tools/integration/email-composer-in-android-109359
Your answer
Follow this Question
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