- Home /
Add a byte[] as attachment in a mail
In the game I capture a screenshot and I'd like to send it via mail.
Is it possible to send it wihtout saving the file to the hard drive?
I tried mail.Attachments.Add(screenshotFile);
But I need to change the byte[] to attachment. Is it possible to do that? (I'm using gmail as smtp).
My code is the following:
static public void GmailSend (string mail, string subject, string body)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("email@email.here");
mail.To.Add(mail);
mail.Subject = subject;
mail.Body = body;
**//add a byte[] attachment to the Mail here**
SmtpClient smtpServer = new SmtpClient(senderSmtp);
smtpServer.Port = senderPort;
smtpServer.Credentials = new System.Net.NetworkCredential(senderEmail, senderPassword) as ICredentialsByHost;
if (sslEnabled){
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
}
smtpServer.Send(mail);
Debug.Log("success");
}
Answer by Landern · Oct 03, 2014 at 02:21 PM
yes, use System.IO and a MemoryStream which has an overload for taking in a byte array.
using System.IO;
static public void GmailSend (string mail, string subject, string body)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("email@email.here");
mail.To.Add(mail);
mail.Subject = subject;
mail.Body = body;
using (MemoryStream ms = new MemoryStream(SOMEBYTEARRAYVAR))
{
mail.Attachments.Add(new Attachment(ms, "SOMEFILENAMEANDJUNK");
}
SmtpClient smtpServer = new SmtpClient(senderSmtp);
smtpServer.Port = senderPort;
smtpServer.Credentials = new System.Net.NetworkCredential(senderEmail, senderPassword) as ICredentialsByHost;
if (sslEnabled){
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
}
smtpServer.Send(mail);
Debug.Log("success");
}
Your answer
Follow this Question
Related Questions
Email attachment on Android & iOS 5 Answers
Unity 3 - Sending Email with C# 6 Answers
How do I send an email on IOS using Application.OpenURL with more than one line of body text? 4 Answers
Send an email from unity to email adress? 1 Answer
How to attach a file to an email on android (using intent and C#)? 1 Answer