Unity 如何在程序中发送Email邮件

Unity 开发日志 专栏收录该内容
47 篇文章 0 订阅

        发送邮件需要用到System.Net.Mail命名空间下的MailMessage和SmtpClient类,smtp是一种提供可靠且有效的电子邮件传输协议,是建立在FTP文件传输服务上的一种邮件服务。下面封装一个Email类,声明MailMessage和SmtpClient字段:

using System;
using System.Net.Mail;

namespace SK.Framework
{
    /// <summary>
    /// 邮件
    /// </summary>
    public sealed class Email : IDisposable
    {
        public MailMessage MailMessage;
        /// <summary>
        /// Smtp Client
        /// Smtp : 简单邮件传输协议(Simple Mail Transfer Protocol)
        /// </summary>
        public SmtpClient SmtpClient;

        public void Dispose()
        {
            MailMessage.Dispose();
            SmtpClient.Dispose();
        }
    }
}

        发送邮件需要提供发送所使用的邮箱,以QQ邮箱为例:首先需要QQ邮箱SMTP服务器主机地址信息:

 

         然后需要所用邮箱的账号和密码,此处密码并不是指真实的邮箱密码,而是用于第三方客户端登录的授权码,需要到QQ邮箱的设置中开启SMTP服务并获取授权码:

        有了上述信息后,封装发送邮件所用的函数:

using System;
using System.Net;
using System.Text;
using UnityEngine;
using System.Net.Mail;

namespace SK.Framework
{
    public static class Mailer
    {
        //邮箱服务器主机,根据使用的发送邮箱,使用其对应的服务器主机 例如 QQ邮箱服务器主机 smtp.qq.com
        private readonly static string host = "smtp.qq.com";
        //邮箱服务器主机端口
        private readonly static int port = 25;
        //发送邮件所用邮箱
        private static string senderEmail = "*********@qq.com";
        //发送邮件所用邮箱的密码 (第三方客户端登录授权码)
        private static string password = "****************";

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="subject">主题</param>
        /// <param name="body">主体</param>
        /// <param name="toEmail">接收邮箱</param>
        /// <param name="callback">回调</param>
        public static void Send(string subject, string body, string toEmail, Action callback = null)
        {
            using(var email = new Email()
            {
                MailMessage = new MailMessage()
                {
                    From = new MailAddress(senderEmail),
                    Subject = subject,
                    Body = body,
                    SubjectEncoding = Encoding.UTF8,
                    BodyEncoding = Encoding.UTF8,
                    IsBodyHtml = false,
                },
                SmtpClient = new SmtpClient()
                {
                    Host = host,
                    Port = port,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(senderEmail, password),
                    EnableSsl = false,
                    DeliveryMethod = SmtpDeliveryMethod.Network
                }
            })
            {
                email.MailMessage.To.Add(new MailAddress(toEmail));
                email.SmtpClient.SendCompleted += (s, e) => callback?.Invoke();
                email.SmtpClient.SendMailAsync(email.MailMessage);
                Debug.Log($"发送邮件:接收人 - {toEmail} 主题 - {subject} 内容 - {body}");
            }
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="subject">主题</param>
        /// <param name="body">主体</param>
        /// <param name="toEmails">接收邮箱</param>
        /// <param name="callback">回调</param>
        public static void Send(string subject, string body, string[] toEmails, Action callback = null)
        {
            using(var email = new Email()
            {
                MailMessage = new MailMessage()
                {
                    From = new MailAddress(senderEmail),
                    Subject = subject,
                    Body = body,
                    SubjectEncoding = Encoding.UTF8,
                    BodyEncoding = Encoding.UTF8,
                    IsBodyHtml = false
                },
                SmtpClient = new SmtpClient()
                {
                    Host = host,
                    Port = port,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(senderEmail, password),
                    EnableSsl = false,
                    DeliveryMethod = SmtpDeliveryMethod.Network
                }
            })
            {
                for (int i = 0; i < toEmails.Length; i++)
                {
                    email.MailMessage.To.Add(new MailAddress(toEmails[i]));
                }
                email.SmtpClient.SendCompleted += (s, e) => callback?.Invoke();
                email.SmtpClient.SendMailAsync(email.MailMessage);
                Debug.Log($"群发邮件:主题 - {subject} 内容 - {body}");
            }
        }
    }
}

        测试:

using UnityEngine;
using SK.Framework;

public class Foo : MonoBehaviour
{
    private void Start()
    {
        Mailer.Send("邮件标题:测试", "邮件内容:测试asdfg", "136512892@qq.com");
    }
}

    欢迎关注公众号 “当代野生程序猿”

  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:点我我会动 设计师:我叫白小胖 返回首页

打赏作者

CoderZ1010

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值