首页 > 代码库 > 如何用unity3d实现发送带附件的邮件

如何用unity3d实现发送带附件的邮件

以Gmail为例。
点击屏幕的Capture按钮得到当前屏幕截图,点击Send按钮将之前的截图作为附件发送邮件。

 

using UnityEngine;using System.Collections;using System;using System.Net;using System.Net.Mail;using System.Net.Security;using System.Security.Cryptography.X509Certificates; public class TryDemo : MonoBehaviour {     // Use this for initialization    void Start () {     }         // Update is called once per frame    void Update () {         }     void OnGUI() {        if (GUI.Button(new Rect(0, 50, 100, 40), "Capture")) {            Debug.Log("Capture Screenshot");            Application.CaptureScreenshot("screen.png");        }        if (GUI.Button(new Rect(0, 0, 100, 40), "Send")) {            SendEmail();        }    }     private void SendEmail()    {        MailMessage mail = new MailMessage();                 mail.From = new MailAddress("youraddress@gmail.com");        mail.To.Add("youraddress@qq.com");        mail.Subject = "Test Mail";        mail.Body = "This is for testing SMTP mail from GMAIL";        mail.Attachments.Add(new Attachment("screen.png"));                 SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");        smtpServer.Port = 587;        smtpServer.Credentials = new System.Net.NetworkCredential("youraddress@gmail.com", "yourpassword")as ICredentialsByHost;        smtpServer.EnableSsl = true;        ServicePointManager.ServerCertificateValidationCallback =             delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)         { return true; };         smtpServer.Send(mail);        Debug.Log("success");    }}

 

①Application.RegisterLogCallback(OnDebugCallBackHandler);可以给debug的打印加入回调函数,监听每次的打印,通过这个来判断是否有出错日志并作出反应
②基本的发送错误日志方法就是通过邮件发送,可以通过在unity中用c#脚本来发送邮件,具体方法在上文有提到;也可以通过Process.Start(“name.exe”)来调用外部程序发送日志邮件,具体操作可以百度c编程发送邮件,有很多例子。
③用c#脚本发送邮件日志更方便实现,c#提供了邮件发送功能的封装。另外还可以用Application.CaptureScreenshot(“filename”)来保存截图用于发送,提供错误时程序截图。

 

原文链接

如何用unity3d实现发送带附件的邮件