zl程序教程

您现在的位置是:首页 >  前端

当前栏目

Unity 工具类 之 发送邮件功能的简单封装 SendEmailWrapper

封装工具 简单 功能 Unity 发送 邮件
2023-09-11 14:20:50 时间

Unity 工具类 之 发送邮件功能的简单封装 SendEmailWrapper

 

目录

Unity 工具类 之 发送邮件功能的简单封装 SendEmailWrapper

一、简单介绍

二、实现原理

三、注意事项

四、效果预览

五、开通邮箱 SMTP 服务

六、代码实现发送邮件功能

七、关键代码


 

一、简单介绍

Unity 工具类,自己整理的一些游戏开发可能用到的模块,单独独立使用,方便游戏开发。

本节介绍,在Unity中如何使用QQ Smtp 发送邮件的。

 

二、实现原理

1、登录 QQ 邮箱,开通 SMTP 服务

2、得到 SMTP 授权码

3、封装函数,发送邮件

 

三、注意事项

1、发送邮件的时候,如果附件文件太大,可能会影响发送效率,甚至会导致发送失败

 

四、效果预览

 

五、开通邮箱 SMTP 服务

1、登录 QQ 邮箱

 

2、找到设置按钮,进入设置

 

3、在设置 选中 账户 一栏

 

4、往下拉,找到 POP3/SMTP,点击开启(若已经开启了,可以直接点击生成授权码)

 

5、通过密保手机,发送短信消息

 

6、之后就会生成一个授权码,同时 SMTP 功能开启

 

六、代码实现发送邮件功能

1、打开 Unity,新建空工程

 

2、导入邮件附件资源

 

3、新建脚本,封装发送邮件功能,和测试发送邮件功能

 

4、把测试发送邮件功能脚本添加到场景中

 

5、运行场景

 

 

 

七、关键代码

1、SendEmailWrapper

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;

public class SendEmailWrapper 
{
    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <param name="emailFormat">邮件的格式化内容</param>
    /// <returns>true 发送成功</returns>
    public static bool SendEmail(EmailFormat emailFormat) {
        if (emailFormat == null)
        {
            Debug.LogError("EmailFormat 不能为空 ");
            return false;
        }

        if (emailFormat.To.Count==0 || emailFormat.To == null) {
            Debug.LogError("EmailFormat 收件人不能为空 ");
            return false;
        }

        // 创建邮件信息,设置发件人
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress(emailFormat.From);

        // 添加收件方(可以是列表)
        foreach (string item in emailFormat.To)
        {
            mail.To.Add(item);

        }

        // 设置邮件主题和内容
        mail.Subject = emailFormat.Subject;
        mail.Body = emailFormat.Body;

        // 设置邮件的附件(可以是多个)
        if (emailFormat.Attachments !=null && emailFormat.Attachments.Count >0) {
            foreach (string item in emailFormat.Attachments)
            {

                mail.Attachments.Add(new Attachment(item));
            }
        }

        // 绑定开通 Smtp 服务的邮箱 Credentials登陆SMTP服务器的身份验证
        SmtpClient smtpClient = new SmtpClient(emailFormat.SmtpClient);
        smtpClient.Credentials = new System.Net.NetworkCredential(emailFormat.From, emailFormat.AuthorizationCode) as System.Net.ICredentialsByHost;
        smtpClient.EnableSsl = true;

        // 设置相关回调
        System.Net.ServicePointManager.ServerCertificateValidationCallback =
            delegate (object s, X509Certificate certificate, X509Chain chain,SslPolicyErrors sslPolicyErrors
        ){ return true; };

        // 发送邮件
        smtpClient.Send(mail);

        Debug.Log("邮件发送成功");

        return true;
    }
}

public class EmailFormat {
    // 账号
    public string From { get; set; }
    // 开启 Smtp 的授权码
    public string AuthorizationCode { get; set; }
    // 那个授权端 QQ,163,或者网易等等
    public string SmtpClient { get; set; }
    // 端口(可要可不要)
    public string Port { get; set; }
    // 邮件主题
    public string Subject { get; set; }
    // 邮件内容
    public string Body { get; set; }
    // 邮件附件
    public List<string> Attachments { get; set; }
    // 收件人
    public List<string> To { get; set; }

    public EmailFormat() {
        Attachments = new List<string>();
        To = new List<string>();
    }

}

 

2、Test_SendEmailWrapper

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;

public class Test_SendEmailWrapper : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

        
        // 线程中发送邮件,避免影响主线程
        Thread t = new Thread(SendEmail);
        t.Start();
    }

    // 发送邮件
    void SendEmail() {

        // 新建一个发件体
        EmailFormat emailFormat = new EmailFormat();

        // 邮件账户和授权码,授权平台,端口(可不要)
        emailFormat.From = "你的邮箱@qq.com";
        emailFormat.AuthorizationCode = "你的授权码";
        emailFormat.SmtpClient = "smtp.qq.com";
        emailFormat.Port = "587";

        // 邮件主题和内容
        emailFormat.Subject = "你的邮件主题:Test Unity Send Email";
        emailFormat.Body = "你的邮件内容:Hello ,Test Unity Send Email";

        // 添加附件,可多个
        emailFormat.Attachments.Add(@"你的附件地址/Attachments/Image.png");
        emailFormat.Attachments.Add(@"Assets/Attachments/Music.mp3");
        // 附件不建议发送视频,太大影响发送效率,可能会发送失败
        //emailFormat.Attachments.Add(@"Assets/Attachments/Video.mp4");

        // 添加收件人,可多个
        emailFormat.To.Add("收件邮箱@qq.com");
        emailFormat.To.Add("收件邮箱@163.com");

        // 发送邮件
        SendEmailWrapper.SendEmail(emailFormat);
    }
   
}