【问题标题】:Send screenshot as email attachment将屏幕截图作为电子邮件附件发送
【发布时间】: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");

【问题讨论】:

  • 我唯一看到的是您的 MailMessageSmtpClient 对象应该在 using 块内,以确保它们正确关闭。我过去曾遇到过电子邮件发送延迟到添加 using 块的问题。

标签: c# email attachment


【解决方案1】:

据我所知,您已经添加了附件:

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"));

【讨论】:

  • 感谢您的回复,但我认为参数不正确,听到错误:最好的重载方法匹配`System.Net.Mail.Attachment.Attachment(System.IO.Stream, string, string)' 有一些无效参数
  • 好的,你不能发送文件名和附件名。立即检查。
猜你喜欢
  • 1970-01-01
  • 2011-05-04
  • 1970-01-01
  • 2021-01-18
  • 1970-01-01
  • 2013-02-26
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多