Solved

Sending an email through MS Exchange using MAPI from .NET

Posted on 2003-10-28
5
801 Views
Last Modified: 2012-06-27
I have a need to connect directly to exchange without going through Outlook. My application needs to able to read and send emails. I have gotten the reading of emails working because of an example I have found, but I can' t figure out the syntax to send an email. This is a desktop app running in vb.net.
 I am using the MAPI, MSMAPI and AxMSMAPI dlls.
the code is as follows:

#Region "My objects ans collections definition"
        ' MAPI definitions
        Dim oSession As MAPI.Session

        Dim oFolder As MAPI.Folder
        Dim stFolderID As String

        ' TreeView Definitions
        Dim rootNode As TreeNode

#End Region

        Private Sub ButtonLogon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLogon.Click
            ' Login to Exchange Server
            oSession = CreateObject("MAPI.Session")
            oSession.Logon(ShowDialog:=True, NewSession:=False)

            If (oSession.CurrentUser.Name = "Unknown") Then
                'Not a good logon, logoff and exit
                oSession.Logoff()
                MsgBox("Logon error!", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "CDO Logon")
                Exit Sub
            End If
        End Sub


#Region "MAPI Fetch e-mails"
        ' Extract information for every mail into selected mail folder
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonFetchMails.Click
            Dim oMessage As MAPI.Message
            Dim items As String() = New String(2) {}

            If stFolderID Is Nothing Then
                Exit Sub
            End If

            Cursor.Current = Cursors.WaitCursor     ' show we're busy doing the sort
            ListViewMails.BeginUpdate()             ' Notify that update begins
            ListViewMails.Items.Clear()

            For Each oMessage In oFolder.Messages
                Try
                    items(0) = oMessage.Sender.Name
                Catch
                    items(0) = ""
                End Try
                items(1) = oMessage.Subject
                items(2) = oMessage.TimeReceived
                ListViewMails.Items.Add(New ListViewItem(items))
            Next
            ListViewMails.EndUpdate()               ' Notify that update ends
            Cursor.Current = Cursors.Default        ' restore default cursor
        End Sub

        ' Show mail content for selected mail
        Private Sub ListViewMails_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListViewMails.Click
            Dim oMessage As MAPI.Message
            Dim lvItem As ListViewItem
            Dim Count As Integer = 0

            lvItem = New ListViewItem
            Cursor.Current = Cursors.WaitCursor     ' show we're busy doing the sort

            If ListViewMails.SelectedItems.Count > 1 Then
                MsgBox("Seleceted = " & ListViewMails.SelectedItems.Count)
                For Count = 0 To ListViewMails.SelectedItems.Count - 1
                    lvItem = ListViewMails.SelectedItems(Count)
                    RichTextBoxMailContent.Clear()

                    For Each oMessage In oFolder.Messages
                        If lvItem.SubItems(2).Text = oMessage.TimeReceived Then
                            RichTextBoxMailContent.AppendText(oMessage.Text)
                        End If
                    Next
                Next
            Else
                lvItem = ListViewMails.FocusedItem
                RichTextBoxMailContent.Clear()

                For Each oMessage In oFolder.Messages
                    If lvItem.SubItems(2).Text = oMessage.TimeReceived Then
                        RichTextBoxMailContent.AppendText(oMessage.Text)
                    End If
                Next
            End If

            Cursor.Current = Cursors.Default        ' restore default cursor
        End Sub
#End Region


---------------------------------------------
what is the syntax to send an email in this structure???
0
Comment
Question by:ajhenley
  • 2
5 Comments
 
LVL 4

Expert Comment

by:NetPointer
ID: 9635609
You can use System.Web.Mail namespace like this.

MailMessage mail = new MailMessage();
mail.From = txtFrom.Text;
mail.To = "abc@xyz.com";
mail.Subject = txtSubject.Text;
mail.Body = txtBody.Text ;
mail.BodyFormat = MailFormat.Html;
SmtpMail.Send(mail);

HTH,
NetPointer

0
 

Author Comment

by:ajhenley
ID: 9635634
No, but that isn't using MAPI. That is sending smtp mail. I am trying to send email through an exchange server (my company's) that doesn't accept smtp mail except for inbound. ANd the firewall won't allow ANY smtp mail to cross it (this is for our intranet site)

But thanks for the speedy reply.
0
 
LVL 4

Accepted Solution

by:
NetPointer earned 250 total points
ID: 9637160
Sorry. I misunderstood..

Now following function, explains that how to do that in VBA...

It wont be too difficult for you to figure out how to do that in your platform.

also check this. http://www.absolutelydiverse.com.au/vb_tips/hardcore/tip_1.htm

Function SendEmail(xAddress, xSubject, xText, ByVal xAttachments)
 ' "xAttachments" - tablou unidimensional:
 '                  lista fisierelor atasate.
 Dim objSession As Object, objMessage As Object
 Dim objOneRecip As Object
 Dim i As Integer

 On Error GoTo error_sendMsg

 Set objSession = CreateObject("MSMAPI.MAPISession")
 objSession.SignOn
 Set objMessage = CreateObject("MSMAPI.MAPIMessages")
 objMessage.SessionID = objSession.SessionID
 objMessage.Compose
 objMessage.MsgIndex = -1
 objMessage.RecipAddress = xAddress
 objMessage.AddressResolveUI = False
 objMessage.MsgSubject = xSubject
 objMessage.MsgNoteText = xText
   
 If IsArray(xAttachments) Then
  For i = 0 To UBound(xAttachments)
    objMessage.AttachmentIndex =objMessage.AttachmentCount
    objMessage.AttachmentPathName = xAttachments(i)
  Next
 End If
   
 objMessage.Send False
 objSession.SignOff
   
 On Error GoTo 0
 SendEmail = True
 Exit Function
   
 error_sendMsg:
 On Error GoTo 0
 SendEmail = False
   
End Function

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10232172
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: NetPointer {http:#9637160}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

19 Experts available now in Live!

Get 1:1 Help Now