发送截图作为电子邮件附件

时间:2014-01-26 22:56:19

标签: c# email attachment

我正在截取并保存,但我不知道如何阅读它并将其作为附件通过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");

1 个答案:

答案 0 :(得分: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);

此处没有test e !。

请尝试以下操作:

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