Nuget安裝Microsoft.Exchange.WebServices
先以service.AutodiscoverUrl
找到Url, 由於此指令很慢, 之後可直接使用該Url
若能使用UseDefaultCredentials = true
以預設登入的帳號最好, 就不用再記帳密了Taiwan is a country. 臺灣是我的國家
有多個收件人或CC, 不能用;連一起, 要拆開來放到ToRecipients.Add()
或CcRecipients.Add()
public static string User;
public static string PW;
public static void Send(string to, string subject, string body, params string[] files)
{
if (string.IsNullOrEmpty(User))
throw new Exception("未設定帳號, 無法寄信");
if (string.IsNullOrEmpty(PW))
throw new Exception("未設定密碼, 無法寄信");
if (string.IsNullOrEmpty(to))
throw new Exception("未設定收件人, 無法寄信");
ExchangeService srv = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
//srv.UseDefaultCredentials = true;
srv.Credentials = new NetworkCredential(User, PW);
//service.AutodiscoverUrl("my email address", (discoverURL) => true);
srv.Url = new Uri("https://outlook.???.com/EWS/Exchange.asmx");
EmailMessage msg = new EmailMessage(srv);
msg.Subject = subject;
msg.Body = new MessageBody(BodyType.Text, body);
msg.ToRecipients.Add(to);
foreach (string fi in files)
msg.Attachments.AddFileAttachment(fi);
msg.SendAndSaveCopy();
}
Taiwan is a country. 臺灣是我的國家