Solved

VBS to send email via Outlook 2010

Posted on 2012-12-29
1
3,321 Views
Last Modified: 2012-12-29
I use multiple Exchange Accounts in Outlook 2010, I have created a VB Script to extract data from a CSV file and create emails to 20-50 users monthly.

I have this working fine but been unable to get the VBScript to select the correct Exchange Account - its not the default one which I use constantly.

Is there a way to select the correct Exchange account as the 'SendUsingAccount'?

'Mailer script that extracts information from CSV file to

'const
	const ForReading = 1
	const Separator = ","

'init
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set objOutlook = CreateObject("Outlook.Application")
	

	strDataFile = "C:\Users\emailtest.csv"	'this is name and location of txt file with data
	
	intCounter = 0
	

'Read data to create mail msg

	Set objFile = fso.OpenTextFile (strDataFile, ForReading)
	
	Do
	  '1- Read records from textfile
		strTextLine = objFile.ReadLine
		arrItem= Split(strTextLine, Separator)

		'waste some memory, for clarity
		strSurname = arrItem(3)
		strFirstName = arrItem(4)	
		strEmail = arrItem(5)
		strStatus = arrItem(9)
		strDate = arrItem(10)	

		If strStatus = "Expired" Then
		  strStatusMsg = "expired"
		  strStatusDate = "at your earliest convenience"
		ElseIf strStatus = "Valid" Then
		  strStatusMsg = "expires"
		  strStatusDate = "by this date"
		Else
		  strStatusMsg = "ERRORm"
		  strStatusDate = "ERRORd"
		End If

		If strEmail = "Email" Then
			strEmail = "own@email.com"
		ElseIf strEmail = "-" Then
			strEmail = "own@email.com"
		End If
		
	  '2- Create email message

		strMsgBody = vbCrLf & _
				strFirstName & " " & strSurname & vbCrLf & _
				vbCrLf & _
				"Your qualification " & strStatusMsg & " on " & strDate & 	"." & vbCrLf & _
				vbCrLf &_
				"ipsom lorum " & strStatusDate & " " & _
				vbCrLf				
				
		' debug : msgbox to see if it works so far
		'MsgBox strEmail & vbCrLf & vbCrLf & strMsgBody, vbOKonly, strTitle

		If objOutlook = "Outlook" Then
			'Set objMailMsg = objOutlook.CreateItem(0)
			Set objMailMsg = objOutlook.CreateItem(olMailItem)
				With objMailMsg
					'.SentOnBehalfOfName = "own@email.com" 
					.To = strEmail 
					.Subject =  "Qualification " & strStatusMsg & " on " & strDate
					.Body =  strMsgBody
					'.HTMLBody =  strMsgBody
					'.BodyFormat = olFormatHTML
					'.Importance = olImportanceNormal
					'.DeleteAfterSubmit = True		'don't keep sent mail
				End With
			If objMailMsg.To <> "" Then
				SMTPAddress = "own@email.com"
				With objMailMsg
					.SendUsingAccount = SMTPAddress
					.Send
				End With
			end if				
			intCounter = intCounter + 1
		End If
		Set objMailMsg = Nothing
 
	Loop While objFile.AtEndOfStream = False
	
	'cleanup
	objFile.Close
	Set objFile = Nothing
	Set fso = Nothing
	
'	objOutlook.Quit
	Set objOutlook = Nothing

	strFeedback="Script completed" & vbCrLf & _ 
			intCounter & " messages placed in Outlook OutBox"
	MsgBox strFeedback, vbOKonly, StrTitle

	Wscript.Quit

Open in new window


I have 3 different Exchange accounts configured in Outlook plus iCloud and own ISP email using POP3.  I need to use one of the Exchange accounts...

Many thanks in advance
0
Comment
Question by:TazDevil1674
1 Comment
 
LVL 9

Accepted Solution

by:
TazDevil1674 earned 0 total points
ID: 38730149
I have resolved this myself...  

If objMailMsg.To <> "" Then
				SMTPAddress = "own@email.com"
				With objMailMsg
					Set .SendUsingAccount = .Session.Accounts.Item(3)
					.Send
				End With
			end if	

Open in new window


Once I specified SET before the .SendUsingAccount it actually worked (I changed the .send to .display to see the output before it was sent)

I found a macro for Outlook to show the correct Session Numbers for each account
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
OWA address for sennatech.sg domain 6 43
Include feature in Outlook 3 37
Outlook - access to other mailboxes - but not cache to the local OST. 5 48
Library not Registered 16 49
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
Find out what you should include to make the best professional email signature for your organization.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now