I am trying to send mail using Powershell with Japanese character on the subject and body of the mail. The mail is successfully sent and the Japanese character in the body is fine. But it is not working in subject. I got =?iso-2022-jp?Q?=1B=24B=25F=259=25H=1B=28B?= instead of テスト.
Can someone help me on this?
code:
$eSubject = "テスト This is subject"
$eBody = "テスト テスト テスト This is body"
$Encode = [Text.Encoding]::GetEncoding("csISO2022JP");
$s64 = [Convert]::ToBase64String($Encode.GetBytes($eSubject), [Base64FormattingOptions]::None)
$Mail = New-Object Net.Mail.MailMessage("From#mail.com","To#mail.com")
$Mail.Subject = [String]::Format("=?{0}?B?{1}?=", $Encode.HeaderName, $s64)
$View = [Net.Mail.AlternateView]::CreateAlternateViewFromString($eBody, $Encode, [Net.Mime.MediaTypeNames]::Text.Plain)
$View.TransferEncoding = [Net.Mime.TransferEncoding]::SevenBit
$Mail.AlternateViews.Add($View)
$SmtpClient = NEW-OBJECT Net.Mail.SmtpClient("localhost","25")
$SmtpClient.Send($Mail)
When using the default CmdLet Send-MailMessage it works fine for me:
$EmailParams = #{
To = 'Destination#domain.com'
From = 'From#domain.com'
Subject = 'テスト This is the subject'
Body = 'テスト This is the body'
SMTPServer = 'YourSMTPServer'
Encoding = 'UTF8'
}
Send-MailMessage #EmailParams
I don't know how. But it is working now, I just remove the encoding for subject.
code:
$eSubject = "テスト This is subject"
$eBody = "テスト テスト テスト This is body"
$Encode = [Text.Encoding]::GetEncoding("csISO2022JP");
$Mail = New-Object Net.Mail.MailMessage("From#mail.com","To#mail.com")
$Mail.Subject = $eSubject
$View = [Net.Mail.AlternateView]::CreateAlternateViewFromString($eBody, $Encode, [Net.Mime.MediaTypeNames]::Text.Plain)
$View.TransferEncoding = [Net.Mime.TransferEncoding]::SevenBit
$Mail.AlternateViews.Add($View)
$SmtpClient = NEW-OBJECT Net.Mail.SmtpClient("localhost","25")
$SmtpClient.Send($Mail)
Related
I try to create a msg file with powershell. I could already add the subject, and the receiver. but I can not add a CC.
This is what I have so far:
$obj = New-Object -ComObject Outlook.Application
$mail = $obj.CreateItem(0)
$Mail.Recipients.Add("email#email.com")
$Mail.Recipients.Type = olCC
$Mail.Recipients.Add("email2#email.com")
$Mail.Recipients.Add("email3#email.com")
$Mail.Subject = "Some Subject"
$Mail.Body = "test mail with powershell"
$Mail.Attachments.Add("c:\Users\se\Desktop\Attachment.txt")
$mail.SaveAs("c:\Users\se\Desktop\test.msg")
c:\Users\se\Desktop\test.msg
I tried to change the Recipient object from the default ("To") to CC but this doesn't work.
Per the documentation you need to change the type on the recipient objects, not on the Recipients collection before adding the recipients.
$cc = $Mail.Recipients.Add("email2#email.com")
$cc.Type = 2
$cc = $Mail.Recipients.Add("email3#email.com")
$cc.Type = 2
Also, olCC is not a valid constant in PowerShell. You need to either assign the numeric value of the constant (see above), define the constant yourself
$olCC = 2
# alternatively, if you want $olCC to be an actual constant:
#New-Variable -Name olCC -Value 2 -Option Constant
...
$cc = $Mail.Recipients.Add("email2#email.com")
$cc.Type = $olCC
$cc = $Mail.Recipients.Add("email3#email.com")
$cc.Type = $olCC
or look up the value in the Interop assembly (untested):
Add-Type -AssemblyName Microsoft.Office.Interop.Outlook
...
$cc = $Mail.Recipients.Add("email2#email.com")
$cc.Type = [Microsoft.Office.Interop.Outlook.Constants]::olCC
$cc = $Mail.Recipients.Add("email3#email.com")
$cc.Type = [Microsoft.Office.Interop.Outlook.Constants]::olCC
I found this code here for sending emails and attachments with Powershell and works great for that but I'm trying to add the email CC to it. I can't figure it out. Please give me a hand if you can. I tried various ways of adding $message.Cc.Add("email_address"); but can't get it working. Thank you.
$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;
$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;
It works to me. Please try.
$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
I'm making a script to automate Outlook 2016. I have one account with two different inboxes from clients.
At the end of the script, I need to send an email from the name of the inbox the script is run from. I'm authorized to send email in the name of both inboxes, but I can't get the script to do it. I post my actual code:
The code:
Function Send-Email {
param ([String]$desde,[String]$subject,[String]$buzon,[String]$inc)
$mail = $Outlook.CreateItem(0)
$firma ="
Textplan
"
$mail.subject = "Closed Ticket "+$subject
[String]$cuerpo =#"
Dear colleague,
bla bla bla
Thank you.
"#
$mail.sender = $buzon
$mail.body = $cuerpo+" "+$firma
$mail.To = $desde
$mail.Send()
Start-Sleep 3
}
$Outlook = New-Object -ComObject Outlook.Application
$desde = client2#mail.com
$buzon = inbox1#mail.com
$inc = 000000001
$subject = "Automat request"
Send-Email -desde $desde -subject $asunto -buzon $Buzon1 -inc $inc
Your issues look to be:
Object defined outside the function it's being used in:
$Outlook = New-Object -ComObject Outlook.Application
Unquoted strings in variables:
$desde = client2#mail.com
$buzon = inbox1#mail.com
$inc = 000000001
Mix and match of single and double quotes
I'd recommend reading about_quoting_rules to understand the difference as this is quite crucial when using variables within strings.
the function param $inc is defined has data but is not used for anything?
Recommend to remove this if it's not used.
Not specifically an issue, but I've changed the here string (#" "#) to a normal string with line breaks (`r`n`)
After resolving the issues and tidying the code:
Function Send-Email {
param (
[String]$desde,
[String]$subject,
[String]$buzon,
[String]$inc
)
$firma = 'Textplan'
$cuerpo = "Dear colleague,`r`nbla bla bla`r`nThank you."
$Outlook = New-Object -ComObject Outlook.Application
$mail = $Outlook.CreateItem(0)
$mail.subject = 'Closed Ticket ' + $subject
$mail.sender = $buzon
$mail.body = "$cuerpo $firma"
$mail.To = $desde
$mail.Send()
Start-Sleep 3
}
$desde = 'client2#mail.com'
$subject = 'Automat request'
$buzon = 'inbox1#mail.com'
$inc = '000000001'
Send-Email -desde $desde -subject $subject -buzon $buzon -inc $inc
I don't have Outlook to test the code, but from checking it over with the Outlook.Application documentation it now seems to be valid.
I finally used this lines and work well:
$mail.SentOnBehalfOfName = "inbox1d#mail.com"
$mail.SendUsingAccount = $Outlook.Session.Accounts | where {$_.DisplayName -eq $FromMail}
Function Send-Email {
param (
[String]$desde,
[String]$subject,
[String]$buzon,
[String]$inc
)
$firma = 'Textplan'
$cuerpo = "Dear colleague,`r`nbla bla bla`r`nThank you."
$Outlook = New-Object -ComObject Outlook.Application
$mail = $Outlook.CreateItem(0)
$mail.subject = 'Closed Ticket ' + $subject
$mail.sender = $buzon
$mail.body = "$cuerpo $firma"
$mail.SentOnBehalfOfName = "inbox1d#mail.com"
$mail.SendUsingAccount = $Outlook.Session.Accounts | where {$_.DisplayName -eq $FromMail}
$mail.Send()
Start-Sleep 3
}
$desde = 'client2#mail.com'
$subject = 'Automat request'
$buzon = 'inbox1#mail.com'
$inc = '000000001'
Send-Email -desde $desde -subject $subject -buzon $buzon -inc $inc
param ([switch]$configure)
$Date = Get-Date
$Server = gc env:computername
Create e-mail message
$msg = new-object Net.Mail.MailMessage
Set e-mail properties
$msg.subject = $Subject
Set e-mail body
$msg.body = $Body
Creating SMTP server object
$SMTP = new-object Net.Mail.SmtpClient($SMTPServer)
Email structure
$msg.From = "admin#email.com"
$msg.Replyto = "me#email.com"
$Subject = "Hardware Alert from $Server $Date"
$SMTPServer = "smtp.office365.com"
$SMTPPort = '25'
$SMTPUser = 'admin#email.com'
$SMTPPassword = 'password'
body
else{$smtp.Send($msg)}
I keep getting "a recipient must be specified" when I run this. What am I doing incorrectly?
I keep getting "a recipient must be specified" when I run this. What am I doing incorrectly?
You did not specify a recipient. (There is no one who could receive that mail.)
The MailMessage class not only has a From property, but also a To property.
$msg.To = "someone#somwhere.tld"
Check out the documentation at https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage(v=vs.110).aspx
My powershell script currently handles sending an email from the command line to a list of users. I would like to add an attachment to the email and also a list of Cc so all emails don't come through the "To" email parameter. Here is my current function.
function SendEmail
{
$smtpServer = "smtp.server"
$smtpFrom = "PROD <email#gmail.com>"
$smtpTo = "me#gmail.com"
$messageSubject = "Weekly List "+$day
$messagebody = "Hi User, Please find attached document. Thank you."
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messageSubject,$messagebody)
}
My attempt was to add a variable called $attachment = "\PathtoFile"
and add $attachment into the Send function, but that didn't work...
Use Send-MailMessage with the Attachments and Cc parameter:
Send-MailMessage -Attachments "C:\path\to\attachment.ext" -Cc "myboss#gmail.com"
You can also specify an encoding (you mentioned special characters):
Send-MailMessage -Encoding UTF8
In general, I would recommend splatting the parameters, so it ends up looking like this:
$MyAttachments = ".\a\relative\path.ext","C:\another\file.ext"
$MailParams = #{
SmtpServer = "smtp.server"
From = "PROD <email#gmail.com>"
To = "me#gmail.com"
Subject = "Weekly List "+$day
Body = "Hi User, Please find attached document. Thank you."
Attachments = $MyAttachments
Encoding = [System.Text.Encoding]::UTF8
}
Send-MailMessage #MailParams