2014-01-26 98 views
0

即時消息截圖並保存,但我不知道如何閱讀並將其作爲附件通過電子郵件發送至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"); 
+0

我看到的唯一的事情是,你的'MailMessage'和'SmtpClient'對象應該是'using'塊內,以確保他們得到正確關閉。過去我遇到過問題,電子郵件發送被延遲,直到添加了「使用」塊。 –

回答

2

據我所看到的,你已經添加附件:

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

問題是,MIME類型是不正確的,你的路徑從一個不同你保存圖像:

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

沒有teste這裏!

嘗試以下操作:

mail.Attachments.Add(new Attachment(Application.dataPath + "/../test-" + count + ".png", 
            @"image/png")); 
+0

感謝您的答覆,但我認爲該參數是不正確的,聽取錯誤: 最好的重載方法匹配'System.Net.Mail.Attachment.Attachment(System.IO.Stream,字符串,字符串)'有一些無效參數 – darkman

+0

好的,您不能發送文件名和附件名稱。現在檢查。 – MarcinJuraszek

相關問題