Solved

Sending an email through MS Exchange using MAPI from .NET

Posted on 2003-10-28
5
793 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

6 Experts available now in Live!

Get 1:1 Help Now