Question

Im taking a screenshot, and saving it, but i don't know how to read it and send it as an attachment in E-mail with Gmail. Here's my code :

// We should only read the screen after all rendering is complete
yield return new WaitForEndOfFrame();

// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D( width, height, TextureFormat.RGB24, false );
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0 );
tex.Apply();

// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();

// save our test image (could also upload to WWW)
File.WriteAllBytes(Application.dataPath + "/../test-" + count + ".png", bytes);
count++;

DestroyObject(tex);

Debug.Log(Application.dataPath + "/../test-" + count + ".png");



MailMessage mail = new MailMessage();

mail.From = new MailAddress("test@gmail.com");
mail.To.Add("test@gmail.com");
mail.Subject = "Test";
mail.Body = "testing class";
mail.Attachments.Add(new Attachment(Application.dataPath + "/../teste-" + count + ".png",
                                    Application.dataPath + "/../teste-" + count + ".png", 
                                    "png"));

SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Port = 587;
smtpServer.Credentials = new System.Net.NetworkCredential("test@gmail.com", "123456") as ICredentialsByHost;
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = 
    delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
{
    return true; 
};
smtpServer.Send(mail);
Debug.Log("success");
Was it helpful?

Solution

As far as I can see, you're already adding the attachment:

mail.Attachments.Add(new Attachment(Application.dataPath + "/../teste-" + count + ".png",
                                    Application.dataPath + "/../teste-" + count + ".png", 
                                    "png"));

problem is, the MIME type is incorrect, and your path differs from the one you're saving image to:

File.WriteAllBytes(Application.dataPath + "/../test-" + count + ".png", bytes);

There is no teste here!.

Try following:

mail.Attachments.Add(new Attachment(Application.dataPath + "/../test-" + count + ".png",
                                    @"image/png"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top