?
Solved

Sending an email through MS Exchange using MAPI from .NET

Posted on 2003-10-28
5
Medium Priority
?
848 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

719 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