TelefoneN
TelefoneN

Reputation: 35

Sending mail with Powershell Windows 7

I cant get this to work on a windows 7 client with powershell 2

 $smtpServer = "smtp.example.com"
 $msg = new-object Net.Mail.MailMessage
 $smtp = new-object Net.Mail.SmtpClient($smtpServer)
 $msg.From = "[email protected]"
 $msg.ReplyTo = "[email protected]"
 $msg.To.Add("[email protected]")
 $msg.subject = "My Subject"
 $msg.body = "This is the email Body."
 $smtp.Send($msg)

I am getting an exception that says "Exception calling "Send with "1" arguments" Failure Sending mail" Anyone have any idea? I have tried Send-MailMessage but it also fails, if I run the command on a server based windows it executes fine. I use the same account for the procedures.

Upvotes: 0

Views: 6711

Answers (3)

Discipulos
Discipulos

Reputation: 469

Changing port to 25 works, but why ?

Upvotes: 0

ArNumb
ArNumb

Reputation: 317

This might work:

$ol = New-Object -comObject Outlook.Application 
$mail = $ol.CreateItem(0) 
$Mail.Recipients.Add("[email protected]") 
$Mail.Subject = "PS1 Script TestMail" 
$Mail.Body = "Test Mail"
$Mail.Send() 

# you can use this for HTML-Mails 
# $Mail.HTMLBody = "<HTML><HEAD>Text<B>BOLD</B>  <span style='color:#E36C0A'>Color Text</span></HEAD></HTML>" 
# you can use this for attache a file 
# $Mail.Attachments.Add("D:\scripte\ol.txt")

For further reference.

Upvotes: 1

manojlds
manojlds

Reputation: 301037

This could be due to many reasons, but one issue that I had seen this exact same error was because an anti-virus program was blocking Powershell from sending the email. Check if this is the case by looking at your anti virus logs.

Beyond that, you might want to check if firewall is fine, you can connect to the SMTP server etc.

Upvotes: 2

Related Questions