Unity发送邮件附件功能

版权声明:转发,使用请留言告知 https://blog.csdn.net/qq_37310110/article/details/86573143

开启邮箱的SMTP服务,以qq邮箱为例,设置>账户>服务

下面截图是用的自己的账号密码发送的额时候 邮箱收到的邮件。

根据提示把密码替换为授权码 即可,授权码不唯一不用记住

发件箱

收件箱

功能脚本

#region 模块信息
// **********************************************************************
// Copyright (C) 2019 Blazors
// Please contact me if you have any questions
// File Name:             Director
// Author:                romantic
// WeChat||QQ:            at853394528 || 853394528 
// **********************************************************************
#endregion

using System.ComponentModel;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;

public class SendEmail : MonoBehaviour {
    SmtpClient smtpServer;
    //默认文件目录
    private string UnityPath = @"F:\私人资料\考试报名材料\xpg.jpg";
    private void Start()
    {
        //所使用邮箱的SMTP服务器,QQ:smtp.qq.com;网易:smtp.163.com;谷歌:smtp.gmail.com等
        smtpServer = new SmtpClient("smtp.qq.com");
        smtpServer.SendCompleted += OnSendCompleted;
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            SendEm(UnityPath);
        }
    }
    /// <summary>
    /// 邮件发送
    /// </summary>
    private void SendEm(string UnityPath)
    {
        MailMessage mail = new MailMessage();
        //发送邮箱的地址,
        mail.From = new MailAddress("[email protected]");
        //收件人邮箱地址 如需发送多个人添加多个Add即可
        mail.To.Add("[email protected]");
        //标题
        mail.Subject = "2019努力加油";
        //正文
        mail.Body = "中商情报网讯:1月18日晚,CBA常规赛第32轮北京首钢主场对阵浙江广厦控股开赛,此次比赛由中国移动北京公司与咪咕公司联手在5G网络环境下用真4K全程直播本场比赛。据悉,这场直播打造全球首场5G+真4K体育赛事直播。";
        //添加一个本地附件 
        mail.Attachments.Add(new Attachment(UnityPath));
        //SMTP端口
        smtpServer.Port = 587;
        //账号密码:去邮箱的设置系统获取授权码替代 。
        smtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "xxxxxxxxx") as ICredentialsByHost;
        smtpServer.EnableSsl = true;
        ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors){ return true; };
        smtpServer.Send(mail);
        Debug.Log("发送成功");
    }

    private void OnSendCompleted(object sender, AsyncCompletedEventArgs e)
    {
        Debug.Log("发送成功");
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37310110/article/details/86573143