即時消息截圖並保存,但我不知道如何閱讀並將其作爲附件通過電子郵件發送至Gmail。 這裏是我的代碼:將屏幕截圖作爲電子郵件附件發送
// 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("[email protected]");
mail.To.Add("[email protected]");
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("[email protected]", "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");
我看到的唯一的事情是,你的'MailMessage'和'SmtpClient'對象應該是'using'塊內,以確保他們得到正確關閉。過去我遇到過問題,電子郵件發送被延遲,直到添加了「使用」塊。 –