我是以自己的Gmail做寄信人,所以SmtpClient的部分是參考用Gmail時的寫法,用其他家的Mail時設定都會不一樣的樣子。
在使用此方式時,作為SMTP的信箱帳戶必須要將安全防護性降低,才能順利寄出信件。
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Net;
- using System.Net.Mail;
- public class EmailController : MonoBehaviour {
- void Start ()
- {
- }
- void Update ()
- {
- }
- void EmailAction()
- {
- MailMessage mailMessage = new MailMessage ();
- SmtpClient smtpClient = new SmtpClient ("smtp.gmail.com");
- Attachment attachment = new Attachment (@"Assets/A.png"); //指定要夾帶的物件路徑
- mailMessage.From = new MailAddress ("寄信人信箱", "寄信人名字", System.Text.Encoding.UTF8);
- mailMessage.To.Add ("收信人信箱1");
- mailMessage.To.Add ("收信人信箱2");
- mailMessage.CC.Add ("收信人信箱3");
- mailMessage.Bcc.Add ("收信人信箱4");
- mailMessage.Subject = "送給你一張好圖片";
- mailMessage.Body = "這是我精挑細選、要送給你的一張好圖片。";
- mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
- mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
- mailMessage.Attachments.Add (attachment);
- mailMessage.Priority = MailPriority.High;
- smtpClient.Port = 587;
- smtpClient.Credentials = new System.Net.NetworkCredential ("寄信人信箱", "寄信人信箱密碼") as ICredentialsByHost;
- smtpClient.EnableSsl = true;
- ServicePointManager.ServerCertificateValidationCallback = delegate(object sender,
- System.Security.Cryptography.X509Certificates.X509Certificate certificate,
- System.Security.Cryptography.X509Certificates.X509Chain chain,
- System.Net.Security.SslPolicyErrors sslPolicyErrors)
- {
- return true;
- };
- smtpClient.Send (mailMessage);
- Debug.Log ("寄信完成!!");
- }
- }
哈囉紹瑋你好~
回覆刪除先感謝分享你的研究XD
我在使用時遇到一些問題,使用Gmail寄信方會有權限問題會被Gmail檔下
後來參考下方這篇重新設定權限就能成功寄出信件
在這協助幫忙補充XD
http://blog.lyhdev.com/2015/10/gmail-smtp-invalid-login-error.html
感謝補充~~~~
刪除