Select Page
Codoid Blog

Sending an Email

Being a leading QA Company, we write blogs on all prominent software testing topics and tools using our real-world experience. So stay sharp by subscribing to our Newsletter.

Sending an email using VBScript can be done using Collaboration Data Object (CDO) messaging. But it is supported only in Windows 2000, Windows Server 2003, Windows Server 2008, Windows Vista, and Windows XP Operating Systems. To create object for CDO, cdosys.dll should be installed in your machine and many users reported failure when CDO is implemented in Windows 7. Then what is the solution for this problem?

We have an alternative, which is System.Net.Mail .Net class. Microsoft introduced this class when .Net Framework 2.0 was launched. The following are the major benefits of Net.Mail class,

  • CDOSYS.dll installation is not required to send an e-mail.
  • More powerful than CDO messaging.
  • Attaching file with message is very simple like adding recipients.
  • Attachment can be done for a filename or a stream.

Code

Dim strUserName,strPassword,strServerName
Dim strFromAddress,strToAddress,strFileToAttach
Dim oCredentials,oSMTPClient,oMessage,oFromAddress,oAttachment

'Setting Username
strUserName="Username"
'Setting Password
strPassword="Password"
'Specify SMTP Server name or IP address of the host computer.
strServerName="smtp.gmail.com"

'From Address
strFromAddress="[email protected]"
'To Address
strToAddress="[email protected]"
'File which will be attached to message
strFileToAttach="C:TestResult.html"

Set oCredentials=DotNetFactory.CreateInstance("System.Net.NetworkCredential","System",strUserName,
strPassword)
Set oSMTPClient=DotNetFactory.CreateInstance("System.Net.Mail.SmtpClient","System",strServerName)
Set oMessage=DotNetFactory.CreateInstance("System.Net.Mail.MailMessage","System")
Set oFromAddress =DotNetFactory.CreateInstance("System.Net.Mail.MailAddress","System",strFromAddress)
Set oAttachment=DotNetFactory.CreateInstance("System.Net.Mail.Attachment","System",strFileToAttach)

'Set ‘From’ property to Message object
oMessage.From=oFromAddress
'Set ‘To’ property to Message object
oMessage.To.Add strToAddress

'If your message has to be formatted with HTML, then make this property value as True.
oMessage.IsBodyHtml=False
'Email Subject
oMessage.Subject = "Test Mail"
'Email Body
oMessage.Body = "Automated Email"
'Adding the oAttachment object to Message object
oMessage.Attachments.Add(oAttachment)

'Setting PORT 587 for outbound mail
oSMTPClient.Port = 587
'Setting Credentials using NetworkCredential class
oSMTPClient.Credentials=oCredentials
'Enabling Secure Socket Layer (SSL) to encrypt the connection
oSMTPClient.EnableSSL = True
'And finally sending the message
oSMTPClient.Send(oMessage)

Set oMessage=Nothing
Set oFromAddress=Nothing
Set oCredentials=Nothing
Set oAttachment=Nothing
Set oSMTPClient=Nothing
  

Synopsis

System.Net.Mail class is not dependant upon COM and it is more versatile than CDO messaging.

Note: We can also embed an image into the message using LinkedResource class.

Reference: http://www.systemnetmail.com/

Comments(0)

Submit a Comment

Your email address will not be published. Required fields are marked *

Talk to our Experts

Amazing clients who
trust us


poloatto
ABB
polaris
ooredo
stryker
mobility