各位it前輩:
公司需求需要將每日產出報表mail的方式寄送。
每日報表產出後會有固定日期資料夾產生,而檔名是固定的。
報表一共有5個檔案,而公司mail傳輸協定是有ssl的
利用出處(以下連結):https://support.winshuttle.com/s/article/ka433000000L1DlAAK/Configure-Workflow-to-Send-Mail-using-SMTP-server-with-SSL-TLS-enforcement
信可以順利寄出了,卻有以下幾個問題?
1.mail的內容是html格式,卻出現了html語法
2.mail的附件無法帶上
想請求協助。
以下是powerscript寫法:
$time= get-date -Format yyyMMdd
$EmailFrom = "frommail@jsp.net>"
$EmailTo = "emailto@jsp.net"
$Subject = "Report Summary ($time)"
$Body = "this is a notification from myself"
$SMTPServer = "jsp.net.tw"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password");
$att1 = New-object Net.Mail.Attachment(".$time\Report1-$time.htm")
$att2 = New-object Net.Mail.Attachment(".$time\report2-$time.txt")
$att3 = New-object Net.Mail.Attachment(".$time\Report3-$time.csv")
$att4 = New-object Net.Mail.Attachment(".$time\Report4-$time.pdf")
$att5 = New-object Net.Mail.Attachment(".$time\Report5-$time.rpt")
$time= get-date -Format yyyMMdd
$att1 = ".$time\Report1-$time.htm" 註解:路徑部分若有問題就再調整
$att2 = ".$time\report2-$time.txt"
$att3 = ".$time\Report3-$time.csv"
$att4 = ".$time\Report4-$time.pdf"
$att5 = ".$time\Report5-$time.rpt"
$mail = New-Object System.Net.Mail.MailMessage
$mail.From = "frommail@jsp.net"
$mail.To.Add = "emailto@jsp.net"
$mail.Subject = "Report Summary ($time)"
$mail.IsBodyHtml=$true 註解:這個屬性要打開
$mail.body = "this is a notification from myself" 註解:自行敲HTML語法
$mail.Attachments.Add($att1) 註解:將附件加入要寄的mail中
$mail.Attachments.Add($att2)
$mail.Attachments.Add($att3)
$mail.Attachments.Add($att4)
$mail.Attachments.Add($att5)
$smtp = New-Object System.Net.Mail.SmtpClient("jsp.net.tw")
$smtp.EnableSsl = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential("username", "password")
$smtp.Send($mail)
runan5678謝謝你提供的powershell語法,有拿來測試,設定值都設定正確,執行後會出現以下訊息
抱歉 是我貼錯語法了
應該是$mail.To.Add("emailto@jsp.net")
而不是$mail.To.Add = "emailto@jsp.net"
要不要試試 blat +stunnel
eigen謝謝~提供其他方式。比較傾向用powershell→利用批次檔達到排程的方式處理。
既然都用 PowerShell 了,怎麼沒使用 Send-MailMessage 呢?
你問題所需要的參數
-BodyAsHtml
-Attachments "C:\1.txt", "C:\2.txt"
-Credential
-UseSsl
-Port
*如果使用 SSL 可能需要 -Port 參數配合。