Powershell电子邮件不包括附件?
我正在尝试设置一个批处理文件,该文件将调用Powershell,并最终将用户计算机上的文件发送到给定的电子邮件地址。虽然我可以得到电子邮件发送,但它总是发送不包括附件 我拥有的批处理文件代码是:Powershell电子邮件不包括附件?,powershell,email,Powershell,Email,我正在尝试设置一个批处理文件,该文件将调用Powershell,并最终将用户计算机上的文件发送到给定的电子邮件地址。虽然我可以得到电子邮件发送,但它总是发送不包括附件 我拥有的批处理文件代码是: powershell.exe -executionpolicy bypass -file C:\Users\attachment2.ps1 我的Powershell代码是: $attachment = "C:\Users\SearchResults\37.csv" $EmailFrom = "da
powershell.exe -executionpolicy bypass -file C:\Users\attachment2.ps1
我的Powershell代码是:
$attachment = "C:\Users\SearchResults\37.csv"
$EmailFrom = "david@gmail.com"
$EmailTo = "john@gmail.com"
$Subject = "The subject of your email"
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$Body = "This is just a test mail to verify the working of CMD"
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("xxx", "xxxx");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $attach, $Body)
如果有人知道为什么不包括附件,这将是非常有帮助的。我看过stackoverflow和其他站点上的其他示例,但无法修复我自己的代码。任何帮助都将不胜感激。仅仅因为您从批处理文件调用了Powershell脚本,并不意味着您应该滥发该标记。我现在已经删除了它。我看不到已定义或实际使用的
$message
。SmtpClient.Send(string,string,string,string)
方法不支持附件,您需要创建一个邮件对象,然后用SmtpClient发送它。但是说真的,你为什么不只是使用发送邮件
?很抱歉,我还是新手,我现在尝试实现发送邮件
方法,但现在连接到服务器时遇到问题。我的代码是$from=”blondedavid@gmail.com“$to=”blondedavid@gmail.com“$subject=“电子邮件的主题”$body=“这只是一封测试邮件,用于验证CMD“$smtp=“smtp.gmail.com”$SMTPClient=New Object Net.mail.SMTPClient($SmtpServer,587)$SMTPClient.enablesssl=$true$SMTPClient.Credentials=New Object System.Net.NetworkCredential(”blondedavid@gmail,“pswrd”);$attachment=“C:\Users\SearchResults\37.csv“发送邮件-SmtpServer$smtp-到$To-从$From-主题$Subject-正文$Body-正文HTML-附件$Attachment-优先级高