Excel VBA Makrosu veya PowerShell Kullanarak Outlook E-postaları Gönderin


Postaları bir Excel elektronik tablosundaki kullanıcı e-postaları listesine göre düzenleme görevi vardı. Her e-posta mesajı, her bir kullanıcıya özel bazı veriler içermeli ve ayrıca kişisel bir dosya da eklenmelidir. Bu makalede, bir VBA makrosu veya PowerShell betiği kullanarak Excel dosyasından bir alıcı listesine otomatik olarak e-posta göndermek için Outlook profilinin nasıl kullanılacağına bakacağız.

Önemli. Her iki e-posta gönderme yöntemi için de bilgisayarınızda bir Outlook posta profili yapılandırılmalıdır. Bu posta kutusu (ve bu e-posta adresi) mesajı göndermek için kullanılacaktır.

Aşağıdaki sütunları içeren bir Excel dosyanız olduğunu varsayalım:

Email | Full Name | Last Password Change Date | Account status

Görevim, Excel listesindeki herkese e-posta göndermek için bu şablonu kullanmak:

Konu: a-d.site etki alanındaki hesap durumunuz
Gövde: Sayın %FullUsername%,
a-d.site etki alanındaki hesabınız %status konumunda % durum
Son şifre değişikliğinin tarih ve saati %pwdchange% şeklindedir.

Outlook Üzerinden E-posta Göndermek için Excel VBA Makrosu

İşte doğrudan bir Excel belgesinde oluşturulabilen küçük bir VBA (Uygulamalar için Visual Basic) posta makrosu.

Yeni bir makro oluşturun: Görünüm -> Makrolar. Makronun adını (send_email) belirtin ve Oluştur'u tıklayın:

Karşınıza çıkan VBA editörüne aşağıdaki kodu kopyalayıp yapıştırın (gerekli tüm açıklamaları ekledim). E-posta göndermeyi otomatikleştirmek için kullanacağım

CreateObject (“Outlook.Application”)

bir Outlook nesnesinin VBA betiklerinde oluşturulmasına ve kullanılmasına izin veren işlev.

Sub send_email()
Dim olApp As Object
Dim olMailItm As Object
Dim iCounter As Integer
Dim Dest As Variant
Dim SDest As String
' Subject
strSubj = "Your account status on a-d.site domain"
On Error GoTo dbg
' Create a new Outlook object
Set olApp = CreateObject("Outlook.Application")
For iCounter = 2 To WorksheetFunction.CountA(Columns(1))
' Create a new item (email) in Outlook
Set olMailItm = olApp.CreateItem(0)
strBody = ""
useremail = Cells(iCounter, 1).Value
FullUsername = Cells(iCounter, 2).Value
Status = Cells(iCounter, 4).Value
pwdchange = Cells(iCounter, 3).Value
'Make the body of an email
strBody = "Dear " & FullUsername & vbCrLf
strBody = strBody & " Your account in a-d.site domain is in" & Status & “ state” & vbCrLf
strBody = strBody & "The date and time of the last password change is" & pwdchange & vbCrLf
olMailItm.To = useremail
olMailItm.Subject = strSubj
olMailItm.BodyFormat = 1
' 1 - text format of an email, 2 -  HTML format
olMailItm.Body = strBody
'Add an attachment (filename format is username@a-d.site.txt). Comment out the following line if you do not need the attachments
olMailItm.Attachments.Add ("C:\ps\" & useremail & ".txt")
olMailItm.Send
Set olMailItm = Nothing
Next iCounter
Set olApp = Nothing
dbg:
'Display errors, if any
If Err.Description <> "" Then MsgBox Err.Description
End Sub

Bu Excel dosyasını .xlsm (makroları destekleyen bir Excel çalışma kitabı biçimi) olarak kaydedin. E-posta göndermek için oluşturduğunuz oluşturulan prosedürü (makro) seçin ve Çalıştır'a tıklayın.

VBA makrosu, Excel elektronik tablosundaki tüm satırları yineler, listedeki her alıcıya bir mesaj oluşturur ve gönderir. Gönderilen e-posta iletileri, Outlook'ta Gönderilmiş Öğeler klasöründe saklanır.

Başka bir kullanıcı veya paylaşılan posta kutusu (Exchange kullanıyorsanız) adına bir e-posta göndermek istiyorsanız, SendAs/Adına Gönderildi izinlerini vermeniz ve komut dosyasına (önceden) aşağıdaki kodu eklemeniz gerekir.

olMailItm.Send

).

olMailItm.SentOnBehalfOfName = "yoursecondemail@a-d.site"

PowerShell Kullanarak Outlook'tan E-posta Gönderme

PowerShell'de e-posta göndermek için Send-MailMessage cmdlet'ini kullanabilirsiniz. Ancak, posta sunucusunda kimlik doğrulaması yapmanızı gerektirir ve OAuth ve Microsoft Modern Kimlik Doğrulama gibi modern kimlik doğrulama yöntemlerini desteklemez. Bu nedenle, bilgisayarınızda yapılandırılmış bir Outlook profiliniz varsa, e-posta göndermek çok daha kolaydır.

Aşağıda, bir Excel dosyasından veri okuyan ve her kullanıcıya bir e-posta göndermek için bir Outlook profili kullanan bir PowerShell betiği örneği verilmiştir:

# open the Excel file
$ExcelObj = New-Object -comobject Excel.Application
$ExcelWorkBook = $ExcelObj.Workbooks.Open("C:\PS\user_list.xlsx")
$ExcelWorkSheet = $ExcelWorkBook.Sheets.Item("Sheet1")
# Get the number of filled rows in an xlsx file
$rowcount=$ExcelWorkSheet.UsedRange.Rows.Count
# Loop through all the rows in column 1, starting from the second row (these cells contain the usernames and e-mails).
for($i=2;$i -le $rowcount;$i++){
$useremail = $ExcelWorkSheet.Columns.Item(1).Rows.Item($i).Text
$FullUsername =  $ExcelWorkSheet.Columns.Item(2).Rows.Item($i).Text
$Status =  $ExcelWorkSheet.Columns.Item(4).Rows.Item($i).Text
$pwdchange = $ExcelWorkSheet.Columns.Item(3).Rows.Item($i).Text
# Generate message body text
$strSubj = " Your account status on a-d.site domain "
$strBody = "Dear " + $FullUsername
$strBody = $strBody + " `r`n Your account in a-d.site domain is in " + $Status
$strBody = $strBody + "`r`n The date and time of the last password change is : " +  $pwdchange
$strfile="C:\ps\" + $useremail + ".txt"
# We assume that Outlook is running, if it is not you will need to start it with the command $outlook = new-object -comobject outlook.application
$outlook = [Runtime.InteropServices.Marshal]::GetActiveObject("Outlook.Application")
$email = $outlook.CreateItem(0)
$email.To = $useremail
$email.Subject = $strSubj
$email.Body =  $strBody
# Attach a file (if necessary)
$email.Attachments.add($strfile)
#send the e-mailmessage
$email.Send()
}
$ExcelWorkBook.close($true)

Bu PowerShell betiği, Outlook'un bilgisayarınızda çalıştığını varsayar. Komut dosyası, XLSX dosyasındaki her alıcı SMTP adresi için e-postanın konusunu ve gövdesini oluşturur ve dosyayı ekler. Ardından e-postayı gönderir.

İlgili makaleler