locked
Can I convert my System.Net.Mail.MailMessage attachment to MAPI atttachment RRS feed

  • Question

  •  In the code below I get a System.Net.Mail.MailMessage and then want to open in Outlook.  So I try and convert it to MAPI.  But I can't get:

    myNewMail.Attachments.Add(attach, DisplayName:="test")

    to work.  It tells me that a parameter is wrong no matter what I do.  Please help!
     


    Public
    Function DisplayNewEMail(ByVal email As System.Net.Mail.MailMessage, ByVal modal As Boolean) As Boolean

    Dim myOutlookApplication As OL.ApplicationClass = Nothing

    myOutlookApplication = New OL.ApplicationClass

    Dim result As Boolean = False

    Try

    Dim myNewMail As OL.MailItem = CType(outLookApp.CreateItem(OL.OlItemType.olMailItem), OL.MailItem)

    For Each attach As Object In email.Attachments

    myNewMail.Attachments.Add(attach, DisplayName:="test")

    Next

    If (Not email.To Is Nothing AndAlso email.To.ToString.Length > 0) Then

    myNewMail.Recipients.Add(email.To.ToString)

    End If

    myNewMail.Subject = email.Subject

    Wednesday, November 19, 2008 8:26 PM

Answers

  • Hi ,

    I think you may on the wrong way ,System.Net.Mail.MailMessage and Outlook.MailItem is different ,their attachments could not be converted to each other.

    As a work around ,we need to save the attachment to the hard disk first and then add it to the Outlook.MailItem.

    Please try the following code,as you didn't give the information about your Visual Studio Version and your Outlook Version ,the sample I wrote againest Visual Studio 2008&Outlook 2007

    Outlook.Inspectors ins;       
      private void ThisAddIn_Startup(object sender, System.EventArgs e)  
            {  
                 ins = Application.Inspectors;  
     
                ins.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(ins_NewInspector);  
            }  
            void ins_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)  
            {  
     
                 
                System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();  
                msg.Attachments.Add(new System.Net.Mail.Attachment(@"C:\users\v\desktop\SaveCorupput.doc"));  
                byte[] array = new byte[msg.Attachments[0].ContentStream.Length];  
                msg.Attachments[0].ContentStream.Read(array, 0, Convert.ToInt32(msg.Attachments[0].ContentStream.Length));  
                System.IO.FileStream fs = new System.IO.FileStream(@"C:\users\v\desktop\SaveCorupputaaaaaaa.doc", System.IO.FileMode.CreateNew, System.IO.FileAccess.ReadWrite);  
                fs.Write(array, 0, array.Length);  
                fs.Flush();  
                ((Outlook.MailItem)Inspector.CurrentItem).Attachments.Add(@"C:\users\v\desktop\SaveCorupputaaaaaaa.doc", Outlook.OlAttachmentType.olByValue, missing, "abc");  
            } 


    Thanks

     

    • Marked as answer by Tim Li Thursday, November 27, 2008 1:48 AM
    Tuesday, November 25, 2008 10:07 AM