Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 858
  • Last Modified:

Sending an email through MS Exchange using MAPI from .NET

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
ajhenley
Asked:
ajhenley
  • 2
1 Solution
 
NetPointerCommented:
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
 
ajhenleyAuthor Commented:
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
 
NetPointerCommented:
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
 
Bob LearnedCommented:
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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now