我在这里找到了用于使用Powershell发送电子邮件和附件的代码,对此非常有用,但是我正在尝试向其中添加电子邮件CC。我不知道。如果可以的话,请帮我。我尝试了各种添加$ message.Cc.Add(“ email_address”);的方法。但无法正常运作。谢谢。
$Username = "MyUserName";
$Password = "MyPassword";
$path = "C:\attachment.txt";
function Send-ToEmail([string]$email, [string]$attachmentpath){
$message = new-object Net.Mail.MailMessage;
$message.From = "YourName@gmail.com";
$message.To.Add($email);
$message.Subject = "subject text here...";
$message.Body = "body text here...";
$attachment = New-Object Net.Mail.Attachment($attachmentpath);
$message.Attachments.Add($attachment);
$smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com", "587");
$smtp.EnableSSL = $true;
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message);
write-host "Mail Sent" ;
$attachment.Dispose();
}
Send-ToEmail -email "reciever@gmail.com" -attachmentpath $path;
答案 0 :(得分:0)
对我有用。请尝试。
$smtpCred = (Get-Credential)
$ToAddress = 'to@outlook.com'
$CcAddress = 'cc@outlook.com'
$FromAddress = 'from@outlook.com'
$SmtpServer = 'smtp.office365.com'
$SmtpPort = '587'
$mailparam = @{
To = $ToAddress
Cc = $CcAddress
From = $FromAddress
Subject = 'Test Subject'
Body = 'Test Body'
SmtpServer = $SmtpServer
Port = $SmtpPort
Credential = $smtpCred
}
Send-MailMessage @mailparam -UseSsl
答案 1 :(得分:0)
$Username = "MyUserName";
$Password = "MyPassword";
$path = "C:\attachment.txt";
function Send-ToEmail([string]$email, [string]$emailCc, [string]$attachmentpath){
$message = new-object Net.Mail.MailMessage;
$message.From = "YourName@gmail.com";
$message.To.Add($email);
$message.Cc.Add($emailCc);
$message.Subject = "subject text here...";
$message.Body = "body text here...";
$attachment = New-Object Net.Mail.Attachment($attachmentpath);
$message.Attachments.Add($attachment);
$smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com", "587");
$smtp.EnableSSL = $true;
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message);
write-host "Mail Sent" ;
$attachment.Dispose();
}
Send-ToEmail -email "reciever@gmail.com" -emailCc "CCreciever@gmail.com" -attachmentpath $path;