首先展示一下效果图吧:
因为比较简单 就直接上代码了,代码已经帮大家注释过了。
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 NewBehaviourScript : MonoBehaviour
{
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("[email protected]");//发件人
mail.To.Add("[email protected]");//收件人
mail.Subject = "Test Mail";//标题
mail.Body = "This is for Me";//内容
mail.Attachments.Add(new Attachment("screen.png"));//附件
SmtpClient smtpServer = new SmtpClient("smtp.qq.com");
smtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "nshpweoijlgvbfif") as ICredentialsByHost;
//Credentials证书 NetworkCredential网络证书 nshpweoijlgvbfif邮箱授权密码!
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback =
delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
smtpServer.Send(mail);
Debug.Log("success");
}
}
代码因为版本的更新稍加修改就可以用了。
至于怎么和邮箱怎么生生授权码,看下图,在邮箱设置=>账户里面。
把POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务里面内容开启,下面的二维码可以生成授权码。
注意:A.如果打包后不能发送,请将: Api Compatibility Level *=.NET 2.0设置为这个数
B.需要生成授权码