Link to home
Start Free TrialLog in
Avatar of shaji_am
shaji_am

asked on

Sending E- mail from VB project

Hi,

I am finalising a VB project. Now I want to send an
e- mail from my project to one person with one message
when a specific condition is met. Is it possible?

Thanks and regards

Shaji


ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this is Using MAPI example from MSDN

1. Start a new project in Visual Basic. Form1 is created by default.

2. Add MSMAPI.VBX to the project.

3. Add a MAPI session control (MAPISession1) and a MAPI message control

   (MAPIMessages1) to the form.


4. Add a command button (Command1) to the form.
5. Put the following code in the command button click event.


      Sub Command1_Click ()
         'MAPI constants from CONSTANT.TXT file:
         Const SESSION_SIGNON = 1
         Const MESSAGE_COMPOSE = 6
         Const ATTACHTYPE_DATA = 0
         Const RECIPTYPE_TO = 1
         Const RECIPTYPE_CC = 2
         Const MESSAGE_RESOLVENAME = 13
         Const MESSAGE_SEND = 3
         Const SESSION_SIGNOFF = 2

         'Open up a MAPI session:
         MapiSession1.Action = SESSION_SIGNON
         'Point the MAPI messages control to the open MAPI session:
         MapiMessages1.SessionID = form1.MapiSession1.SessionID

         MapiMessages1.Action = MESSAGE_COMPOSE   'Start a new message

         'Set the subject of the message:
         MapiMessages1.MsgSubject = "This is the subject."
         'Set the message content:
         MapiMessages1.MsgNoteText = "This is the mail message."

         'The following four lines of code add an attachment to the message,
         'and set the character position within the MsgNoteText where the
         'attachment icon will appear. A value of 0 means the attachment will
         'replace the first character in the MsgNoteText. You must have at
         'least one character in the MsgNoteText to be able to attach a file.
         MapiMessages1.AttachmentPosition = 0
         'Set the type of attachment:
         MapiMessages1.AttachmentType = ATTACHTYPE_DATA
         'Set the icon title of attachment:
         MapiMessages1.AttachmentName = "System Configuration File"
         'Set the path and file name of the attachment:
         MapiMessages1.AttachmentPathName = "C:\CONFIG.SYS"

         'Set the recipients
         MapiMessages1.RecipIndex = 0                    'First recipient
         MapiMessages1.RecipType = RECIPTYPE_TO          'Recipient in TO line
         MapiMessages1.RecipDisplayName = "EddieSpaghetti"   'e-mail name
         MapiMessages1.RecipIndex = 1                  'add a second recipient
         MapiMessages1.RecipType = RECIPTYPE_TO        'Recipient in TO line
         MapiMessages1.RecipDisplayName = "TanyaLasagna"     'e-mail name
         MapiMessages1.RecipIndex = 2                   'Add a third recipient
         MapiMessages1.RecipType = RECIPTYPE_CC         'Recipient in CC line
         MapiMessages1.RecipDisplayName = "BlairAngelHair"  'e-mail name
         MapiMessages1.RecipIndex = 3                  'Add a fourth recipient
         MapiMessages1.RecipType = RECIPTYPE_CC          'Recipient on CC Line
         MapiMessages1.RecipDisplayName = "JoanieCannelloni" 'e-mail name"

         'MESSAGE_RESOLVENAME checks to ensure the recipient is valid and puts
         'the recipient address in MapiMessages1.RecipAddress
         'If the E-Mail name is not valid, a trappable error will occur.
         MapiMessages1.Action = MESSAGE_RESOLVENAME
         'Send the message:
         MapiMessages1.Action = MESSAGE_SEND

         'Close MAPI mail session:
         MapiSession1.Action = SESSION_SIGNOFF
      End Sub


6. Save the project.
7. Run the code, and click the command button.

sorry ignore previous post it is 16 bit code using .VBX
Avatar of Anthony Perkins
shaji_am,

Before proceding further, please tend to all your 7 pending questions that you have not bothered to follow-up on.  There are some dating back to April.

For the record:
Questions Asked 7
Last 10 Grades Given  
Question Grading Record 0 Answers Graded / 0 Answers Received

Avatar of xSinbad
xSinbad

What is your email program?
If you have outlook

Option Explicit

Dim App As Object
Dim Itm As Object

Set App = CreateObject("Outlook.Application")
Set Itm = App.CreateItem(0)
With Itm
   .Subject = "A tip from vbCode Magician"
   .To = "mail1@get2net.dk; mail2@get2net.dk"
   .Body = "For tips and source visit vbCode Magician"
   .Send
End With


from
http://www.vbecstasy.com/code.asp?cat=Network%2FInternet&id=45&catid=inet

See this great free tool:
You don't need to worry about which client mail application is already installed...

http://www.freevbcode.com/ShowCode.Asp?ID=109
<listening..>
This is from a test program I wrote:

======================
Form1:
======================

VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MsComCtl.ocx"
Begin VB.Form Form1
   Caption         =   "MAPI test"
   ClientHeight    =   5790
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   9660
   LinkTopic       =   "Form1"
   ScaleHeight     =   5790
   ScaleWidth      =   9660
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton Command3
      Caption         =   "&Attachments"
      Height          =   390
      Left            =   8175
      TabIndex        =   10
      Top             =   2775
      Width           =   1215
   End
   Begin MSComctlLib.ListView lvInbox
      Height          =   3315
      Left            =   900
      TabIndex        =   8
      Top             =   2250
      Width           =   7065
      _ExtentX        =   12462
      _ExtentY        =   5847
      View            =   3
      LabelWrap       =   -1  'True
      HideSelection   =   -1  'True
      FullRowSelect   =   -1  'True
      _Version        =   393217
      ForeColor       =   -2147483640
      BackColor       =   -2147483643
      BorderStyle     =   1
      Appearance      =   1
      NumItems        =   3
      BeginProperty ColumnHeader(1) {BDD1F052-858B-11D1-B16A-00C0F0283628}
         Text            =   "From"
         Object.Width           =   2540
      EndProperty
      BeginProperty ColumnHeader(2) {BDD1F052-858B-11D1-B16A-00C0F0283628}
         SubItemIndex    =   1
         Text            =   "Subject"
         Object.Width           =   2540
      EndProperty
      BeginProperty ColumnHeader(3) {BDD1F052-858B-11D1-B16A-00C0F0283628}
         SubItemIndex    =   2
         Text            =   "Attachments"
         Object.Width           =   2540
      EndProperty
   End
   Begin VB.TextBox txtBody
      Height          =   990
      Left            =   900
      MultiLine       =   -1  'True
      TabIndex        =   6
      Top             =   900
      Width           =   7065
   End
   Begin VB.TextBox txtTo
      Height          =   315
      Left            =   900
      TabIndex        =   5
      Top             =   150
      Width           =   7065
   End
   Begin VB.TextBox txtSubject
      Height          =   315
      Left            =   900
      TabIndex        =   4
      Top             =   525
      Width           =   7065
   End
   Begin VB.CommandButton Command2
      Caption         =   "&Get Inbox"
      Height          =   390
      Left            =   8175
      TabIndex        =   1
      Top             =   2250
      Width           =   1215
   End
   Begin VB.CommandButton Command1
      Caption         =   "&Send"
      Height          =   390
      Left            =   8175
      TabIndex        =   0
      Top             =   150
      Width           =   1215
   End
   Begin VB.Label Label4
      AutoSize        =   -1  'True
      Caption         =   "Inbox:"
      Height          =   195
      Left            =   150
      TabIndex        =   9
      Top             =   2325
      Width           =   435
   End
   Begin VB.Label Label3
      AutoSize        =   -1  'True
      Caption         =   "Body:"
      Height          =   195
      Left            =   150
      TabIndex        =   7
      Top             =   975
      Width           =   405
   End
   Begin VB.Label Label2
      AutoSize        =   -1  'True
      Caption         =   "Subject:"
      Height          =   195
      Left            =   150
      TabIndex        =   3
      Top             =   600
      Width           =   585
   End
   Begin VB.Label Label1
      AutoSize        =   -1  'True
      Caption         =   "To:"
      Height          =   195
      Left            =   150
      TabIndex        =   2
      Top             =   225
      Width           =   240
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Dim oOutlook As Outlook.Application
Dim oFolder As Outlook.MAPIFolder
Dim oMailitem As Outlook.MailItem

Private Sub Command1_Click()
Dim oMessage As Outlook.MailItem

    Set oMessage = oOutlook.CreateItem(olMailItem)
    With oMessage
        .To = txtTo.Text
        .Subject = txtSubject.Text
        .Body = txtBody.Text
        .Send
    End With
    Set oMessage = Nothing
   
End Sub

Private Sub Command2_Click()
Dim sAtt As String
Dim lst As ListItem

    Set oFolder = oOutlook.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
   
    For Each oMailitem In oFolder.Items
        Set lst = lvInbox.ListItems.Add(, , oMailitem.SenderName)
        lst.SubItems(1) = oMailitem.Subject
        If oMailitem.Attachments.Count > 0 Then
            lst.SubItems(2) = CStr(oMailitem.Attachments.Count)
        End If
        lst.SubItems(3) = oMailitem.EntryID
    Next oMailitem
   
End Sub

Private Sub Command3_Click()
Dim frm As Form2
Dim oAtt As Attachment

    If lvInbox.SelectedItem.Index = 0 Then
        MsgBox "Select a message first !", vbCritical
        Exit Sub
    End If
   
    If Len(lvInbox.SelectedItem.SubItems(2)) = 0 Then
        MsgBox "This message contains no attachment(s) !", vbInformation
        Exit Sub
    End If
   
    For Each oMailitem In oFolder.Items
        If oMailitem.EntryID = lvInbox.SelectedItem.SubItems(3) Then
            Set frm = New Form2
            frm.lblMessage.Caption = oMailitem.Subject
            For Each oAtt In oMailitem.Attachments
                frm.lstAtt.AddItem oAtt.FileName
            Next oAtt
            frm.Show vbModal
            Unload frm
        End If
    Next oMailitem
   
    Set oAtt = Nothing

End Sub

Private Sub Form_Load()
Dim lWidth As Long

    lWidth = (lvInbox.Width - 100) / 4
   
    Set oOutlook = New Outlook.Application
   
    With lvInbox
        .View = lvwReport
        .ColumnHeaders.Clear
        .ColumnHeaders.Add , , "From", lWidth
        .ColumnHeaders.Add , , "Subject", lWidth * 2
        .ColumnHeaders.Add , , "Attachments #", lWidth
        .ColumnHeaders.Add , , "ID", 0
    End With
   
End Sub

Private Sub Form_Unload(Cancel As Integer)
    'oOutlook.Quit
    Set oMailitem = Nothing
    Set oFolder = Nothing
    Set oOutlook = Nothing
End Sub

=========================
Form2:
=========================

VERSION 5.00
Begin VB.Form Form2
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   "Message"
   ClientHeight    =   3195
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   4680
   LinkTopic       =   "Form2"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   3195
   ScaleWidth      =   4680
   ShowInTaskbar   =   0   'False
   StartUpPosition =   3  'Windows Default
   Begin VB.ListBox lstAtt
      Height          =   2205
      Left            =   225
      TabIndex        =   0
      Top             =   675
      Width           =   4215
   End
   Begin VB.Label lblMessage
      Caption         =   "#"
      Height          =   390
      Left            =   225
      TabIndex        =   1
      Top             =   150
      Width           =   4215
   End
End
Attribute VB_Name = "Form2"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

===========================
Project:
===========================

Type=Exe
Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\WINNT\System32\stdole2.tlb#OLE Automation
Reference=*\G{00062FFF-0000-0000-C000-000000000046}#9.0#0#..\..\..\..\Program Files\Microsoft Office\Office\msoutl9.olb#Microsoft Outlook 9.0 Object Library
Form=Form1.frm
Object={831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0; MsComCtl.ocx
Form=Form2.frm
Startup="Form1"
Command32=""
Name="MailMAPItest"
HelpContextID="0"
CompatibleMode="0"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
VersionCompanyName=""
CompilationType=0
OptimizationType=0
FavorPentiumPro(tm)=0
CodeViewDebugInfo=0
NoAliasing=0
BoundsCheck=0
OverflowCheck=0
FlPointCheck=0
FDIVCheck=0
UnroundedFP=0
StartMode=0
Unattended=0
Retained=0
ThreadPerObject=0
MaxNumberOfThreads=1

[MS Transaction Server]
AutoRefresh=1

D'Mzzl!
RoverM
Hi!

Here's some examples for you over the net:

Download...
http://www.planetsourcecode.com/vb/scripts/ShowZip.asp?lngWId=1&lngCodeId=9418&strZipAccessCode=ODE%5F94182363
Description: With this program, you can send emails with multiple file attachement using MS winsock control. Heavily commented. Please don't forget to vote!!


Download...
http://www.planetsourcecode.com/vb/scripts/ShowZip.asp?lngWId=1&lngCodeId=10505&strZipAccessCode=ODE%5F105057258
Description: Are you tired of Outlook and Outlook Express hanging your machine, and letting the KAK virus in? Well, I am too, so in response, I made EZMailer, a simple email program that sends and receives mail, PERIOD. It doesn't attempt to interpret or auto-execute ...(description truncated


Download...
http://www.planetsourcecode.com/vb/scripts/ShowZip.asp?lngWId=1&lngCodeId=4796&strZipAccessCode=mail47960542
Description: Email with attachments, Progress Bar, Drag & Drop


Should a 'specific condition' occur at any part of the day, use a timer, set it to interval say... 5000, enabled property set to True.  In the timer, add the code to check for the condition.  If the condition is met, then use any of the above codes to send an email : )


That's it!

glass cookie : )
Hi shaji_am,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept appari's comment(s) as an answer.

shaji_am, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
EXPERTS,

While under almost every circumstance, DanRollins has done an incredible job of cleaning up this topic area, in this particular instance, I think he's slipped. appari, in a comment following his initial post, indicates that his/her post should be ignored.

Can I get a suggestion for resolving this question, please?

Netminder
CS Moderator
Netminder,

Actually what appari says is to ignore his/her previous posts, but his/her first post is still relevant.  So if Dan was referring to the first post, than he is correct.

However, I should add that due to the lack of informatoin in the original question and no follow-up from the questioner, it is hard to know what is the best answer.  So, you could concievably split between:
appari
xSinbad
Richie_Simonetti
roverm

Anthony



<sigh>

acperkins,

I can see you've gone over to the Dark Side, inhabited by the Evil Twisted SPLITLizard, aka COBOLdinosaur...

<grin>

Netminder
Netminder,

Sorry about the extra work.  But you did not have to take my word for it <g> ...

Anthony
Anthony,

Denial is the first symptom... but we'll see if we can save you... <laughing>

N