简体   繁体   中英

Sending Email Issue

While trying to attach the .CSV file and run the code, i get the error as "Exception calling "Send" with "1" argument(s): "Unable to send to all recipients."

I have tried with .txt file ($attachment = "\\path1\\test.txt") and the same code is executed without any error (but I have not received any mail)

$date_value = Get-Date -format yyyy_MM_dd
$file_Name = 'test_'+ $date_value.ToString() + '.csv'
$attachment = "\\path1\test.CSV"
$smtpserver = "server.com" 
$size=((Get-item $attachment ).length) -as [Int]

$from = "mail.com" 
[string[]]$To= "xyz@xyz.com"
$CC="xyz@xyz.com"
$Subject = "Test"
$body=@"

Hi,
Test
Regards,
Team
"@ 

$message = new-object System.Net.Mail.MailMessage ( $From , $To )
$message.CC.Add($cc) 
$message.Subject = $Subject 
$attach = new-object Net.Mail.Attachment($attachment) 
$message.Attachments.Add($attach) 
$message.body = $body 
$smtp = new-object Net.Mail.SmtpClient($smtpserver) 
if ($size -gt 0){
$smtp.Send($message) 
}
$attach.Dispose()
$message.Dispose()
$smtp.Dispose()

Expectation: .CSV file should get attached and send in the email

Have you tried the Send-MailMessage cmdlet with the -Attachment option? Its really simple.

Send-MailMessage -To $To -From $from -Cc $CC -Subject $Subject -Attachments $attachment -Body $body -SmtpServer $smtpserver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM