发送邮件

2019-08-10  本文已影响0人  JJJJJJJJJJC
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;
using UnityEngine;
public class 邮箱
{
    private SmtpException error;
    private string 发送邮箱;
    private string 名字;
    private SmtpClient smtp客户端;
    public SmtpException 发送错误 { get { return error; } }



    public 邮箱() { }
    public 邮箱(string host,string 发送邮箱,string 密码,string 名字 = null)
    {
        Init(host, 发送邮箱, 密码, 名字);
    }
    public void Init(string 主机,string 发送邮箱 ,string 密码, string name =null)
    {
        this.发送邮箱 = 发送邮箱;
        smtp客户端 = new SmtpClient();
        smtp客户端.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp客户端.Host = 主机;
        //smtp客户端.UseDefaultCredentials = true;//资格证书
        smtp客户端.Credentials = new NetworkCredential(发送邮箱,密码);
        if (name == null) return;
        this.名字 = name;
    }
    public bool 发送(string 标题,string 内容,string 接受邮箱,string 附加文件路径=null)
    {
        MailMessage 邮箱信息 = new MailMessage();
        邮箱信息.From = new MailAddress(发送邮箱, 名字);
        邮箱信息.To.Add(接受邮箱);
        邮箱信息.Subject = 标题;
        邮箱信息.Body = 内容;
        邮箱信息.BodyEncoding = Encoding.UTF8;
        邮箱信息.IsBodyHtml = true;//是否html邮箱
        邮箱信息.Priority = MailPriority.High;//优先级
       //邮箱信息.
        if(!string.IsNullOrEmpty(附加文件路径))
        {
            邮箱信息.Attachments.Add(new Attachment(附加文件路径));
        }
        return 发送(邮箱信息);

    }

    private bool 发送(MailMessage 邮箱数据)
    {
        try
        {
            smtp客户端.Send(邮箱数据);
            error = null;
            return true;
        }
        catch(SmtpException ex)
        {
            error = ex;
            return false;
        }
    }

    public static void 实例()
    {
        string host = "smtp.163.com";//邮件服务器
        string userName = "*****@163.com";
        string password = "******";
        string MyName = "收件人看到的名字";
        string title = "代码发的标题";
        string content = "内容``````````";
        string strto = "qxxxxx@qq.com";
        string attachPath = @"D:\360Downloads\成就\1.jpg";
        string strcc = "qxxxxx@qq.com";//抄送

        邮箱 邮件 = new 邮箱();
        邮件.Init(host, userName, password, MyName);
        邮件.发送(title, content, strto, attachPath);
    }

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

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.F))
            邮箱.实例();
    }
}

上一篇下一篇

猜你喜欢

热点阅读