Saturday, January 23, 2010

Sending An Email Using QTP DOT Net Factory

Sending HTML formatted e-mail using QTP DOT Net factory. In the below code I have used Gmail account. You may try with other accounts also.

Dim vStUserName,vStPassword,vStServerName

vStUserName="Gmail Account Username"'Enter your Gmail Account User Name
vStPassword="Gmail Account Password"'Enter Your Gmail Account Password
vStServerName="smtp.gmail.com"'Gmail SMTP Server Name

vStFromAddress="FromAddress@gmail.com"'Enter valid From Address
vStToAddress="ToAddress@yahoo.com"'Enter Valid To Address

Set oCredentials=DotNetFactory.CreateInstance("System.Net.NetworkCredential","System",vStUserName,vStPassword)
Set oSMTPClient=DotNetFactory.CreateInstance("System.Net.Mail.SmtpClient","System",vStServerName)
Set oMessage=DotNetFactory.CreateInstance("System.Net.Mail.MailMessage","System")
Set oFromAddress =DotNetFactory.CreateInstance("System.Net.Mail.MailAddress","System",vStFromAddress)

oMessage.From=oFromAddress
oMessage.To.Add vStToAddress

oMessage.IsBodyHtml=True'If your message to be formatted with HTML, then make this property value as True or else False.
oMessage.Subject = "GMail Test"'Subject of your email
oMessage.Body = " This is the test text for Gmail email"

oSMTPClient.Port = 587
oSMTPClient.Credentials=oCredentials
oSMTPClient.EnableSSL = True
oSMTPClient.Send(oMessage)

'Kill All the objects
Set oMessage=Nothing
Set oFromAddress=Nothing
Set oCredentials=Nothing
Set oSMTPClient=Nothing

5 comments:

  1. Hi, I am getting the below error if i run the above code. could you please suggest.



    External object System.Net.Mail.SmtpClient::Send has thrown the following exception:
    A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.93.109:587
    Line (25): "oSMTPClient.Send(oMessage)".

    ReplyDelete
  2. Nagannadora,

    Are you behind a company firewall? I had the same problem. I cannot connect outside of my own office using scripts like these. If you are at home, you may need to reconfigure your own firewall.

    ReplyDelete
  3. It didn't work for me either. The error was generated by the SmtpClient stating "The remote certificate is invalid according to the validation process". it seems to try and make a connection rather than send it. Can this be configured to use pop?

    Thank you

    ReplyDelete
  4. Hi. Nice One and Working fine. Could you please tell me how to add attachment in this?

    ReplyDelete
  5. Hi. Really nice blog.
    Asif Can you give some links for sending an email though OutLook using DotNetFactory

    ReplyDelete