我想在此电子邮件中发送CSV文件。 我可以毫无问题地发送电子邮件,但是我尝试了很多发送附件的方法,但是最终电子邮件没有附件地发送了。
function sendmail($Body) {
$Smtp = New-Object System.Net.Mail.SmtpClient
$MailMessage = New-Object System.Net.Mail.MailMessage
$Smtp.Host = "smtp-server"
$MailMessage.From = "sender@example.com"
$MailMessage.To.Add("recipient@example.org")
$MailMessage.Subject = "Hello"
$MailMessage.Body = $Body
$smtp.Port = 25
$smtp.EnableSsl = $false
$Smtp.Send($MailMessage)
}
$Body = "12334"
sendmail $Body
电子邮件必须看起来像这样:
您好,所有信息都在文件Example.csv
中
答案 0 :(得分:0)
Send-MailMessage
具有处理附件的直接方法。下面是示例。
Send-MailMessage -from "User01 <user01@example.com>" `
-to "User02 <user02@example.com>", `
"User03 <user03@example.com>" `
-subject "Sending the Attachment" `
-body "Forgot to send the attachment. Sending now." `
-Attachment "data.csv" -smtpServer smtp.fabrikam.com
或者,您应该使用system.net.mail.mailmessage
,然后必须使用:
$EmailFrom = "<user@domain.tld>"
$EmailTo = "<user@domain.tld>"
$EmailSubject = "<email subject"
$SMTPServer = "smtphost.domain.tld"
$SMTPAuthUsername = "username"
$SMTPAuthPassword = "password"
$emailattachment = "<full path to attachment file.csv>"
function send_email {
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($emailfrom)
$mailmessage.To.add($emailto)
$mailmessage.Subject = $emailsubject
$mailmessage.Body = $emailbody
$attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')
$mailmessage.Attachments.Add($attachment)
#$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("$SMTPAuthUsername", "$SMTPAuthPassword")
$SMTPClient.Send($mailmessage)
}
如果需要,请参考THIS。
希望有帮助。