我需要在windows server 2012 r2做排程能夠自動寄信
但公司mail需要使用465port寄信
我打算用powershell send-mailmessage
請問要如何修改指令?
send-mailmessage -from 'user01@xxx.com.tw' -To 'user02@xxx.com.tw' -Subject 'test' -smtpserver 'mymail.xxx.com.tw' -port 465
還是有什麼方便的軟體推薦的嗎
我用blat試過 但也是卡在同樣的問題 不知道怎麼設定465port....
我真的被大家搞亂了,還真的以為BAT無所不能.....(PS1還不錯無所不能)
真的不要用BAT/PS1啦,一點秘密也沒有,郵箱帳號密碼用記事本就可以直接看光光
以下是我用C# (Net FrameWork 4.7.2)寫的範例
!!!! 我沒說C#編譯出來的EXE不能反組譯哦,只是需要一點小功夫,至少不是記事本 !!!!
using System;
using System.Net;
using System.Net.Mail;
using System.Text;
namespace SendMailSMTPs
{
class Program
{
static void Main(string[] args)
{
using (SmtpClient smtpClient = new SmtpClient())
{
System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteServerCertificateValidationCallback);
var basicCredential = new NetworkCredential("MIS", "594088");
smtpClient.Port = 465; //使用SMTPS 465端口
smtpClient.EnableSsl = true; // 如果主機支援SSL的話
smtpClient.Host = "mail.yourCompany.com.tw";
smtpClient.UseDefaultCredentials = false; //不使用匿名帳戶
smtpClient.Credentials = basicCredential; // 上上上上上那一行的帳密
using (MailMessage message = new MailMessage())
{ // 信件內容
//發信者的郵箱跟名稱,以下全用utf-8避免亂碼
MailAddress fromAddress = new MailAddress("mis@mail.yourCompany.com.tw", "MIS群發", Encoding.GetEncoding("utf-8"));
message.SubjectEncoding = Encoding.GetEncoding("utf-8");
message.BodyEncoding = Encoding.GetEncoding("utf-8");
message.From = fromAddress;
message.Subject = "測試郵件";
message.SubjectEncoding = Encoding.GetEncoding("utf-8");
message.IsBodyHtml = true; // 如果不使用HTML的話可以不用設
message.Body = $"信件內容......<H1>支援HTML格式</H1>";
message.BodyEncoding = Encoding.GetEncoding("utf-8");
message.To.Add(new MailAddress("mydarling@mail.hisCompany.com.tw", "", Encoding.GetEncoding("utf-8")));
// 另一個收件者
message.To.Add(new MailAddress("mydarling2@gmail.com","親愛的GMAIL",Encoding.GetEncoding("utf-8")));
//CC、BCC也是比照辦理
//message.CC.Add(... message.BCC.Add
try
{
// 送出......
smtpClient.Send(message);
}
catch (Exception ex)
{
// 錯誤處理
Console.WriteLine(ex.Message);
}
}
}
}
static private bool RemoteServerCertificateValidationCallback(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
Console.WriteLine(certificate);
return true;
}
}
}
我用OUTLOOK正確收到信了
GMAIL(而且沒被丟進垃圾信件去)
試試把
smtpClient.EnableSsl = false ;
看看
謝謝Japhen提供的方法!
但我不知道為何真的怎麼試都無法@@
最後我用python的寫法解決了
https://www.ultrasmtp.com/kb/developers.php
蠻怪的....但謝謝大家的幫忙~~
bellayu ... 頂樓的那段程式修改過了,真是不好意思忘了加入SSL公鑰的動作
在main裡加一行
System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteServerCertificateValidationCallback);
跟一個
private bool RemoteServerCertificateValidationCallback(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
Console.WriteLine(certificate);
return true;
}