Solved

Sending an email through MS Exchange using MAPI from .NET

Posted on 2003-10-28
5
807 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

832 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