Using LinkLabel to send email with details

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
ctrlaltdel
Posts: 12
Joined: Wed May 06, 2015 9:10 pm

Using LinkLabel to send email with details

Post by ctrlaltdel » Tue Mar 06, 2018 8:43 am

I would like to create a LinkLabel to send an email which will open a new email and populate the To, Subject and Body fields. Information to be gathered, current logged on user and computer name. I know how to use WMI to gather information about the computer, just need to know how to integrate it all into the linklabel.

Thanks

User avatar
jvierra
Posts: 13507
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Using LinkLabel to send email with details

Post by jvierra » Tue Mar 06, 2018 8:53 am

What do you mean "integrate into LinkLabel"??

$linklabel1.Text = $env:COMPUTERNAME + ':' + $env:USERNAME

User avatar
ctrlaltdel
Posts: 12
Joined: Wed May 06, 2015 9:10 pm

Re: Using LinkLabel to send email with details

Post by ctrlaltdel » Tue Mar 06, 2018 8:57 am

I want the link label to open an outlook email and prepoulate the fields with information that I provide

To: address@domain.com
Subject: New Email from $ENV:UserName on $ENV:ComputerName

Body: Please type your message below

User avatar
jvierra
Posts: 13507
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Using LinkLabel to send email with details

Post by jvierra » Tue Mar 06, 2018 9:07 am

You would have to code that in the click event and in the COM code create a new mail message and populate the fields then display the mail item. There are a number of ways to do this. Here is one:

https://social.technet.microsoft.com/Fo ... powershell

User avatar
localpct
Posts: 271
Joined: Wed Sep 09, 2015 12:53 pm

Re: Using LinkLabel to send email with details

Post by localpct » Wed Mar 07, 2018 6:10 am

Why open outlook?

Code: Select all

Send-MailMessage -Body "" -to "" -From "" -Subject "" -SmtpServer ""

User avatar
luk4mu5
Posts: 9
Joined: Wed May 17, 2017 4:17 pm
Location: England

Re: Using LinkLabel to send email with details

Post by luk4mu5 » Wed Mar 07, 2018 2:24 pm

so something like this:

Code: Select all

$linklabel1_LinkClicked=[System.Windows.Forms.LinkLabelLinkClickedEventHandler]{
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "person@company.com"
$Mail.bcc = "person2@company.com"
$Mail.cc = "person3@company.com"
$Mail.Subject = "New Email from $ENV:UserName on $ENV:ComputerName"
$Mail.Body = "Hello User, `nThis is an automated message!`nThe following user: $ENV:UserName has broken their device: $ENV:ComputerName and produced Error code $YourError`n`nPlease Send Help!"
$Mail.Importance = 2 # 0=Low 1=normal 2=high
$Mail.Display() # $Mail.Send() $Mail.Save()

User avatar
jvierra
Posts: 13507
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Using LinkLabel to send email with details

Post by jvierra » Wed Mar 07, 2018 2:49 pm

The request was to open a mail item in Outlook. This is common thing for automating support requests. We set the mail item To and Subject plus adding attachments. The message is then displayed so the user can add comments.

The link I posted does this in a few lines. It is familiar to the user and we do not have to know about mail servers and the users email address. This is all populated by Outlook. User adds comments and attaches other files and hits "Send". Mail gone. All in 6 or 7 lines of code.

Locked