Hi,

I want to trigger a mail from VB6 code. Here the only specific requirement is,

When the user click the button, it will do something and send the result in a mail using common id.

Eg: There is common mail id called RMG@xyz.com (Resource management Group) which will be used by all from RMG team

I want to trigger a mail from this id though the user currently logged into his own mail id (eg: Andreret@xyz.com) in his machine.

Is it possible? Can you please share some sample code snippet for this.

Recommended Answers

All 7 Replies

Heres 3 samples -

Option Explicit

Private Sub Command1_Click()
  Dim objSession As Object
  Dim objMessage As Object
  Dim objRecipient As Object


  'Create the Session Object
  Set objSession = CreateObject("mapi.session")


  'Logon using the session object
  'Specify a valid profile name if you want to
  'Avoid the logon dialog box
  objSession.Logon profileName:="MS Exchange Settings"


  'Add a new message object to the OutBox
  Set objMessage = objSession.Outbox.Messages.Add


  'Set the properties of the message object
  objMessage.Subject = "This is a test."
  objMessage.Text = "This is the message text."


  'Add a recipient object to the objMessage.Recipients collection
  Set objRecipient = objMessage.Recipients.Add


  'Set the properties of the recipient object
  objRecipient.Name = "divakar@xyz.there"  '<---Replace this with a valid
                                  'display name or e-mail alias
  objRecipient.Type = mapiTo
  objRecipient.Resolve

  'Send the message
  objMessage.Send showDialog:=False 'Show false if you do not want to load
  'outlook on the screen
  MsgBox "Message sent successfully!"

  'Logoff using the session object
  objSession.Logoff
End Sub

Private Sub Command2_Click()

Dim oShell As New Shell
    Dim oFolder As Folder
    Dim oFolderItem As FolderItem

    Set oFolder = oShell.NameSpace("C:\")
    Set oFolderItem = oFolder.Items.Item("AUTOEXEC.BAT")

    oFolderItem.InvokeVerb "&Copy"

    Set oFolder = oShell.NameSpace(9)
    Set oFolderItem = oFolder.Items.Item("Mail Recipient.MAPIMail")

    oFolderItem.InvokeVerb "&Paste"

End Sub

Private Sub Command3_Click()

Dim strPath$, OutlookItem, ColAttach
Const olByValue = 1
    Set OutlookItem = Application.CreateItem(0)
    OutlookItem.To = "davikar@here.there"
    OutlookItem.Subject = "Check mail work!" ' Subject header
    OutlookItem.Body = "Body shows work or not" ' Actual message
'Outlook! ""
    Set ColAttach = OutlookItem.Attachments
    strPath = "C:\Documents and Settings\vsales\My Documents\Quotes\Patrick.xls"
    ColAttach.Add strPath, olByValue, 1, "File Attachment"
    OutlookItem.Display
    SendKeys "%{s}", True

End Sub

If you want to use a different SMTP, you need to load the login details first and then send mail via the new smtp.

Have a look at THIS link with a very nice function on how to do this.

Thanks for your code.. Can you give me a breif about whats the difference between 3 codes.. I understand 1st is for triggering a mail and 3rd is triggering mail with attachment. I am not clear on 2nd one.. Also can you confirm my understanding as well?

Its a pleasure.:)

Command 2 gets the senders email address from the contacts list and not there name. You will notice sometime that a mail send to you only contains the persons name and not their mail address, this gets the actual mail address.

Also can you confirm my understanding as well?

Not sure what you mean by this???

Thanks.. Now clear...

"Also can you confirm my understanding as well?" - Means can you confirm my understading of point 1 and 3 (which i mentioned above)

However Can you please tell me what should be pre-requisites before getting this code run.

My actual requirement is, an exe file should run every day night 12:00 AM (in a schedule task) and perform the below
- Connect the server DB
- Run the query and produce the report in excel file
- Save the file
- Trigger a mail to stake holders.

My understading after reading your post is, To do the above steps I need to

- Create a profile called RMG@xyz.com
- Trigger a mail with attachment

Where should i keep the keep the exe file? Any specific network where the email provisions available or any spcific servers?

Even if a keep the exe file in a independent machine which will run 24/7, will the code automatically open outlook with user id and pwd and trigger?

Sorry i may ask more question but still i am not getting much understanding

Yes, your assumption of 1 and 2 is correct.:)

First, you will create a second account on every users desktop with the RMG as the account holder. This will be called from the application used by the users from their desktops.

Using excell is a total different scenario, look at your question, that is what got answered.

want to trigger a mail from VB6 code. Here the only specific requirement is,

When the user click the button, it will do something and send the result in a mail using common id.

When using excel documents, saving them and then send those as mail requires much more coding and manipulation of your email.

I think you need to be close this thread, open a new thread and post the exact requirements you need.;)

Thanks.. This info is more than enough :-)

No problem. Happy coding.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.