【发布时间】:2014-01-27 07:09:29
【问题描述】:
我正在截屏并保存它,但我不知道如何阅读它并将其作为电子邮件的附件发送到 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("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");
【问题讨论】:
-
我唯一看到的是您的
MailMessage和SmtpClient对象应该在using块内,以确保它们正确关闭。我过去曾遇到过电子邮件发送延迟到添加using块的问题。
标签: c# email attachment