【Unity3D自学记录】用Unity3D发邮件(带附件)

用Unity3D发邮件(带附件),要引用一些必要的类

代码如下:

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 SendEmailTest : MonoBehaviour
{

    void OnGUI()
    {
        if (GUI.Button(new Rect(0, 50, 100, 40), "截图"))
        {
            Application.CaptureScreenshot("Screen.png");
        }
        if (GUI.Button(new Rect(0, 0, 100, 40), "发送右键"))
        {
            SendEmail();
        }
    }

    private void SendEmail()
    {
        MailMessage mail = new MailMessage();

        mail.From = new MailAddress("[email protected]");
        mail.To.Add("[email protected]");
        mail.Subject = "题目";
        mail.Body = "内容";
        mail.Attachments.Add(new Attachment("Screen.png"));
        SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
        smtpServer.Port = 587;
        smtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "Emailpassword") as ICredentialsByHost;
        smtpServer.EnableSsl = true;
        ServicePointManager.ServerCertificateValidationCallback =
            delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            { return true; };
        smtpServer.Send(mail);
    }
}

MailMessage类:http://msdn.microsoft.com/zh-tw/library/system.net.mail.mailmessage.aspx

SmtpClient 类:http://msdn.microsoft.com/zh-cn/library/system.net.mail.smtpclient(VS.80).aspx



你可能感兴趣的:(Unity3D_技术,Unity3D)