none
if and send

    Question

  • Hi all,

    I have a this code. But i didn't write 'if condition' if i click to OK button, send mail else dont send mail and close application. Please help this topic. Thank you..

    $olFolderMail = 16
    $ol = New-Object -comObject Outlook.Application 
    $Mailbox = $ol.GetNameSpace("MAPI")



    Add-Type -AssemblyName System.Windows.Forms
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 


    #$Form.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon('C:\ProgramData\Autodesk\Vault 2017\Extensions\DataStandard\Vault\electron.ico')


    $olFolderMail = 16
    $ol = New-Object -comObject Outlook.Application 
    $Mailbox = $ol.GetNameSpace("MAPI")



    Add-Type -AssemblyName System.Windows.Forms
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 


    #$Form.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon('C:\ProgramData\Autodesk\Vault 2017\Extensions\DataStandard\Vault\electron.ico')

    $objForm = New-Object System.Windows.Forms.Form 
    $objForm.Text = "Mail ile Bilgilendirme Formu"
    $objForm.Size = New-Object System.Drawing.Size(300,500) 
    $objForm.StartPosition = "CenterScreen"

    $objForm.KeyPreview = $True
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
        {$x=$objTextBox.Text;$objForm.Close()}})
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
        {$objForm.Close()}})

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(100,400)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.BackColor = "White"
    $OKButton.ForeColor = "Green"
    $OKButton.Text = "Gönder!"
    $OKButton.Add_Click({$x=$objTextBox.Text;$objTextBox2.Text;$objForm.Close()})

    $objForm.Controls.Add($OKButton)


    #$CancelButton = New-Object System.Windows.Forms.Button
    #$CancelButton.Location = New-Object System.Drawing.Size(150,120)
    #$CancelButton.Size = New-Object System.Drawing.Size(75,23)
    #$CancelButton.Text = "Vazgeç"
    #$CancelButton.Add_Click({$objForm.Close()})
    #$objForm.Controls.Add($CancelButton)

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(10,20) 
    $objLabel.Size = New-Object System.Drawing.Size(280,20) 
    $objLabel.Text = "Proje Kodunu Giriniz.."
    $objForm.Controls.Add($objLabel) 


    $objTextBox = New-Object System.Windows.Forms.TextBox 
    $objTextBox.Location = New-Object System.Drawing.Size(10,40) 
    $objTextBox.Size = New-Object System.Drawing.Size(260,20)
    $objTextBox.AppendText(("{0}`r" -f $TextBox.Text)) 
    $objForm.Controls.Add($objTextBox) 




    $objLabel2 = New-Object System.Windows.Forms.Label
    $objLabel2.Location = New-Object System.Drawing.Size(10,100) 
    $objLabel2.Size = New-Object System.Drawing.Size(300,20) 
    $objLabel2.Text = "Ek Bilgi Giriniz."
    $objForm.Controls.Add($objLabel2) 



    $objTextBox2 = New-Object System.Windows.Forms.TextBox 
    $objTextBox2.Location = New-Object System.Drawing.Size(10,120) 
    $objTextBox2.Size = New-Object System.Drawing.Size(260,250) 
    $objTextBox2.WordWrap = $True
    $objTextBox2.Multiline = $True,
    $objTextBox2.AppendText(("{0}`r" -f $TextBox2.Text))
    $objForm.Controls.Add($objTextBox2) 
    $objForm.Topmost = $True

    $exitButton = New-Object System.Windows.Forms.Button
    $exitButton.Location = "100,430"
    $exitButton.Size = "75,23"
    $exitButton.ForeColor = "Red"
    $exitButton.BackColor = "White"
    $exitButton.Text = "Exit"
    $exitButton.add_Click({$objForm.close()})
    $objForm.Controls.Add($exitButton)
     



    $objForm.Add_Shown({$objForm.Activate()})
    [void] $objForm.ShowDialog()
    $global:x=$objTextBox.Text
    $global:y=$objTextBox2.Text
    $x
    $y

    $mail = $ol.CreateItem(0)
    $null = $Mail.Recipients.Add("alp.akdogan@electron.com.tr")

    $Mail.Subject = "İş Emri State Değişikliği."  
    $mail.Getinspector.WordEditor.Range().Paste()
    $Mail.Body = ("İlgili İş Emri 'Calisiliyor'dan 'Kontrol Bekliyor'a getirilmiştir. Bilgilerinize..

    Proje Kodu :  $global:x

    Ek Bilgiler : $global:y


    Saygilarımla..")


    $Mail.Send()

    Thursday, March 16, 2017 9:05 AM

Answers