Exemplo n.º 1
0
        private void CopyAttachments(IMessage msg, MailMessage outMsg)
        {
            MAPI33.IMAPITable attachmentsTable = null;
            msg.GetAttachmentTable(0, out attachmentsTable);
            using (attachmentsTable) {
                MAPI33.MapiTypes.Value[,] rows;
                attachmentsTable.SetColumns(new Tags[] {Tags.PR_ATTACH_FILENAME, Tags.PR_ATTACH_CONTENT_ID, Tags.PR_ATTACH_MIME_TAG, Tags.PR_ATTACH_ENCODING, Tags.PR_ATTACH_LONG_PATHNAME, Tags.PR_ATTACH_SIZE, Tags.PR_ATTACH_NUM},
                    IMAPITable.FLAGS.Default);

                //Use OpenAttach to get an IAttachment

                int attachIdx = 0;
                for( ;attachmentsTable.QueryRows(1, 0, out rows) == Error.Success && rows.Length > 0; ) {
                    try {
                        attachIdx++;
                        String attachFilename, attachMimeTag, attachContentId;
                        uint attachNum;
                        IAttachment attachObj;
                        byte[] attachData;

                        attachNum = (uint)((MapiInt32)rows[0,6]).Value;
                        msg.OpenAttach(attachNum, IAttachment.IID, 0, out attachObj);
                        using (attachObj) {
                            if (rows[0,0] is MapiString) {
                                attachFilename = ((MapiString)rows[0,0]).Value;
                            } else {
                                attachFilename = String.Format("Attach{0}.dat", attachIdx);
                            }

                            if (rows[0, 1] is MapiString) {
                                attachContentId = ((MapiString)rows[0, 1]).Value;
                            } else {
                                attachContentId = null;
                            }

                            if (rows[0,2] is MapiString) {
                                attachMimeTag = ((MapiString)rows[0,2]).Value;
                            } else {
                                attachMimeTag = String.Format("application/x-octet-stream");
                            }

                            IStream attachDataStream;
                            IUnknown unk;
                            Error hr = attachObj.OpenProperty(Tags.PR_ATTACH_DATA_BIN, IStream.IID, 0, 0, out unk);
                            //OLE object attachments are PR_ATTACH_DATA_OBJ, so if BIN isn't found, try that
                            if (hr == Error.NotFound) {
                                hr = attachObj.OpenProperty(Tags.PR_ATTACH_DATA_OBJ, IStream.IID, 0, 0, out unk);
                            }

                            if (hr == Error.Success) {
                                attachDataStream = (IStream)unk;
                                unk.Dispose();

                                using (attachDataStream) {
                                    int bytesRead = 0;
                                    ArrayList streamContents = new ArrayList();
                                    byte[] buffer = new byte[1024];

                                    do {
                                        attachDataStream.Read(buffer, (uint)buffer.Length, out bytesRead);
                                        if (bytesRead == buffer.Length) {
                                            streamContents.AddRange(buffer);
                                        } else {
                                            for (int idx = 0; idx < (int)bytesRead; idx++) {
                                                streamContents.Add(buffer[idx]);
                                            }
                                        }
                                    } while (bytesRead > 0);

                                    attachData = (byte[])streamContents.ToArray(typeof(byte));
                                }
                            } else {
                                attachData = System.Text.Encoding.ASCII.GetBytes(String.Format("Error getting contents of attachment '{0}' due to error '{1}'", attachFilename, hr));
                            }

                            //Use a MemoryStream to stream the attachment data into the Attachment object
                            using (MemoryStream ms = new MemoryStream(attachData)) {
                                outMsg.AddAttachment(ms, attachFilename, attachMimeTag, attachContentId);
                            }
                        }
                    } catch (Exception e) {
                        System.Diagnostics.Debug.WriteLine(e.ToString());
                    }
                }
            }
        }