Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Hypnobeard
Sep 15, 2004

Obey the Beard



Howdy.

I'm running into a frustrating issue trying to send a simple SMTP message from a server in Powershell v1.

Here's the relevant portion of the script; the attachment is present and the path is correct. (I've omitted some object dumps I've added in the course of troubleshooting for clarity.)

code:
$msg = new-object System.Net.Mail.MailMessage
$att = new-object System.Net.Mail.Attachment($file, 'text/plain')

$msg.subject = "subject"
$msg.body = "body"
$msg.Attachments.Add($att)
$msg.To.Add("address1@host.com")
$msg.To.Add("address2@host.com")
$msg.From = "sender@host.com"

$smtpServer = "smtpserver address"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($msg)
This is creating the message fine, setting the sender, adding the attachment.. but it's refusing to add the recipients:

code:
From                        : [email]sender@host.com[/email]
Sender                      :
ReplyTo                     :
To                          : {, }
Bcc                         : {}
CC                          : {}
Priority                    : Normal
DeliveryNotificationOptions : None
Subject                     : subject
SubjectEncoding             :
Headers                     : {}
Body                        : body
BodyEncoding                : System.Text.ASCIIEncoding
IsBodyHtml                  : False
Attachments                 : {filename_of_attachement.csv}
AlternateViews              : {}
It doesn't matter if I create the MailAddress objects first and pass those to To.Add or specify a sender in the MailMessage constructor.

What am I doing wrong?

Adbot
ADBOT LOVES YOU

Hypnobeard
Sep 15, 2004

Obey the Beard



adaz posted:

You're not doing anything wrong by that code sample I stared at it for like 10 minutes and can't see anything wrong. I mean just copy pasting that code block gets me this:




e: Oh you're using powershell v1. I don't have v1 handy anywhere to test it on, sec guessing it's because you're using an older version of the framework let me look at it.

e2: If you do $msg.To after constructing/adding the address what is outputted? Are DisplayName, User,Host,Address also all blank? Even by v2 of the framework you aren't doing anything wrong.

DisplayName is blank, User, Host and Address have the appropriate entries.

If it helps narrow it down, this is 64-bit Server 2008 Enterprise SP2 with the Windows Powershell feature installed.

I'm guessing we need either to install v2 or put in a later version of .NET (v2, 3, and 3.5 are currently installed).

Hypnobeard fucked around with this message at 20:44 on Dec 2, 2011

Hypnobeard
Sep 15, 2004

Obey the Beard



Jethro posted:

How sure are you that you're running v1? I'm pretty sure 2008 SP2 has v2 already. Try looking at $psversiontable. If it exists, you're running v2. If not you are indeed on v1.

$psversiontable doesn't exist (empty response). $Host.Version returns:

code:
PS Variable:\> $Host.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
1      0      0      0
So I'm pretty sure I'm on v1.

Hypnobeard
Sep 15, 2004

Obey the Beard



adaz posted:

Oh that's fine, displayname isn't a required attribute you don't need it to send email. For whatever reason with v1 it's not displaying address when you just check the $msg and output it to the shell but it should still send the actual email just fine. V2 looks like displays address by default or displayname if it's filled in. You can create a valid MailAddress with a displayname if you really care about how it shows up (http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx)

I'm not too worried about the displayname; it's refusing to send the email period because of the lack of recipient. I guess the answer is upgrade to v2, which means it's time to start the approval process. Meh.

Thank you very much for the help. :)

Hypnobeard
Sep 15, 2004

Obey the Beard



adaz posted:

Weird, it shouldn't care at all about display name. I was looking at some of my old code I know was created in the V1 days and I've never had to add a display name. So something like this won't work? It still whines about invalid recipient? There has to be something else going on here, in particular i'm wondering about that from part, it really shouldn't be displaying it like that from everything I remember, almost like it's html

code:
$to = [system.net.mail.mailaddress]"address1@host.com"
$from = [system.net.mail.mailaddress]"address2@host.com"

$msg = new-Object System.Net.Mail.MailMessage
$msg.to.add($to)
$msg.from = $from
$msg.subject = "blah"
$msg.body = "blah"

Grrr. Turns out the McAfee client on the server was set to block port 25 transactions. Fixing that resolved the problem. It's still weird that it doesn't show the recipient addresses on the MailMessage object, though.

Again, thanks for the help.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply