using UnityEngine;//純Log用
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class Mail {
public void SendMail() {
MailMessage mail = new MailMessage();
mail.From = new MailAddress("寄件者信箱");
mail.To.Add("收件者信箱");
mail.Subject = "主旨";
try{
//內文附圖
AlternateView plainView = AlternateView.CreateAlternateViewFromString("<p>",null, "text/plain");
mail.AlternateViews.Add(plainView);
AlternateView htmlView = CreateAlternateView("Floor0001");
mail.AlternateViews.Add(htmlView);
//附件
FileStream imageFileStream = new FileStream(Application.streamingAssetsPath+"/Floor0001.jpg",FileMode.Open);
Attachment attachment = new Attachment(imageFileStream,"Floor0001.jpg");
mail.Attachments.Add(attachment);
Debug.Log("Success");
}catch{
Debug.Log("Fail");
}
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Port = 587;
smtpServer.Credentials = new System.Net.NetworkCredential("寄件者信箱", "寄件者密碼") as ICredentialsByHost;
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
smtpServer.Send(mail);
}
private AlternateView CreateAlternateView(string pictureName) {
string htmlBody = "<html><body><img src=cid:"+pictureName+"A></img><p>" +
"<img src=cid:"+pictureName+"B></img><p>" +
"Send by unity3d!</body></html>";
AlternateView alternateView = AlternateView.CreateAlternateViewFromString(
htmlBody,
null, "text/html");
imageA.ContentId = pictureName+"A";
LinkedResource imageB = new LinkedResource(Application.streamingAssetsPath+"/"+pictureName+"B.jpg" );
imageB.ContentId = pictureName+"B";
alternateView.LinkedResources.Add(imageA);
alternateView.LinkedResources.Add(imageB);
return alternateView;
}