Unity3D教程:如何利用U3D发送邮件附件

发表于2016-09-28
评论0 3.1k浏览

案例效果图





代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Program
    {
        static void Main(string[] args)
        {
            SmtpClient mailClient = new SmtpClient("smtp.qq.com");
            mailClient.EnableSsl = true;
            //Credentials登陆SMTP服务器的身份验证.
            mailClient.Credentials = new NetworkCredential("1213250243@qq.com", "密码");
            //test@qq.com发件人地址、test@tom.com收件人地址
            MailMessage message = new MailMessage(new MailAddress("1213250243@qq.com"), new MailAddress("aladdingame@qq.com"));
  
            // message.Bcc.Add(new MailAddress("tst@qq.com")); //可以添加多个收件人
            message.Body = "Hello Word!";//邮件内容
            message.Subject = "this is a test";//邮件主题
            //Attachment 附件
            Attachment att = new Attachment(@"D:/test.mp3");
            message.Attachments.Add(att);//添加附件
            Console.WriteLine("Start Send Mail....");
            //发送....
            mailClient.Send(message);
  
            Console.WriteLine("Send Mail Successed");
  
            Console.ReadLine();
        }
    }


Unity
  说明:截图并且发送到指定邮件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
usingUnityEngine;
using System.Collections;
using System;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
  
public class SendEmailSrc : 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("1213250243@qq.com");
        mail.To.Add("1213250243@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.qq.com");
        smtpServer.Credentials = new System.Net.NetworkCredential("1213250243@qq.com", "密码") as ICredentialsByHost;
        smtpServer.EnableSsl = true;
        ServicePointManager.ServerCertificateValidationCallback =
            delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            { return true; };
  
        smtpServer.Send(mail);
        Debug.Log("success");
    }


项目源码

点击下载
Unity QQ群:375151422 

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

0个评论