您当前的位置: 首页 >  unity

幻世界

暂无认证

  • 0浏览

    0关注

    237博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity发送邮件附件功能

幻世界 发布时间:2019-01-21 14:01:38 ,浏览量:0

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

开启邮箱的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);
        }
    }
    /// 
    /// 邮件发送
    /// 
    private void SendEm(string UnityPath)
    {
        MailMessage mail = new MailMessage();
        //发送邮箱的地址,
        mail.From = new MailAddress("XXXXXXX@qq.com");
        //收件人邮箱地址 如需发送多个人添加多个Add即可
        mail.To.Add("XXXXXXXX@163.com");
        //标题
        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("XXXXXXXX@qq.com", "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("发送成功");
    }
}

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

关注
打赏
1660704426
查看更多评论
立即登录/注册

微信扫码登录

0.0352s