コード例 #1
0
ファイル: MainForm.cs プロジェクト: zhonghai66/pstsdknet
        private void exportButton_Click(object sender, EventArgs e)
        {
            if (attachmentListBox.SelectedItem == null)
            {
                MessageBox.Show("No item selected!");
                return;
            }

            var nodeId = new NodeID {
                Value = (uint)messagesListBox.SelectedItems[0].Tag
            };
            var attachmentName = attachmentListBox.SelectedItem as String ?? string.Empty;

            char[] invalidChars        = System.IO.Path.GetInvalidFileNameChars();
            string invalidString       = Regex.Escape(new string(invalidChars));
            var    validAttachmentName = Regex.Replace(attachmentName, "[" + invalidString + "]", "");

            if (null != attachmentName)
            {
                string fileFilter = Path.GetExtension(attachmentName);

                var sfd = new SaveFileDialog();
                sfd.Filter   = "*" + fileFilter + "|*" + fileFilter;
                sfd.FileName = validAttachmentName;

                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    IMessage message = _pst.OpenMessage(nodeId);

                    foreach (IAttachment attachment in message.Attachments)
                    {
                        if (attachment.Filename == attachmentName)
                        {
                            try
                            {
                                if (attachment.IsMessage)
                                {
                                    var writer          = new MimeMessageWriter();
                                    var embeddedMessage = attachment.OpenAsMessage();
                                    using (var fs = sfd.OpenFile()) writer.Write(embeddedMessage, fs);
                                }
                                else
                                {
                                    using (var bw = new BinaryWriter(sfd.OpenFile()))
                                    {
                                        bw.Write(attachment.Bytes);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("Error completing requested action.\r\n\r\n" + ex.Message + "\r\n" + ex.StackTrace);
                            }

                            break;
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: MainForm.cs プロジェクト: zhonghai66/pstsdknet
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (messagesListBox.SelectedIndices.Count < 1)
            {
                return;
            }

            var nodeId = new NodeID {
                Value = (uint)messagesListBox.SelectedItems[0].Tag
            };

            var emailSubject = messagesListBox.SelectedItems[0].SubItems[1].Text;

            char[] invalidChars        = System.IO.Path.GetInvalidFileNameChars();
            string invalidString       = Regex.Escape(new string(invalidChars));
            var    validOutputFilename = Regex.Replace(emailSubject, "[" + invalidString + "]", "");

            validOutputFilename =
                string.IsNullOrEmpty(validOutputFilename) ?
                "Untitled Message.eml" : validOutputFilename + ".eml";

            string fileFilter = Path.GetExtension(validOutputFilename);

            var sfd = new SaveFileDialog();

            sfd.Filter   = "*" + fileFilter + "|*" + fileFilter;
            sfd.FileName = validOutputFilename;

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    IMessage message = _pst.OpenMessage(nodeId);

                    var writer = new MimeMessageWriter();
                    using (var fs = sfd.OpenFile()) writer.Write(message, fs);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error completing requested action.\r\n\r\n" + ex.Message + "\r\n" + ex.StackTrace);
                }
            }
        }
コード例 #3
0
        public void Write(IMessage message, Stream stream)
        {
            var outMessage = new DotNetOpenMail.EmailMessage();

            // assuming this is right for unicode support?
            outMessage.HeaderEncoding = DotNetOpenMail.Encoding.EncodingType.QuotedPrintable;
            outMessage.HeaderCharSet  = Encoding.UTF8;

            if (message.HasSubject)
            {
                outMessage.Subject = message.Subject;
            }

            if (message.RecipientCount > 0)
            {
                foreach (var recipient in message.Recipients)
                {
                    var mailAddress =
                        new DotNetOpenMail.EmailAddress(
                            recipient.HasEmailAddress ? recipient.EmailAddress : " ", recipient.Name,
                            DotNetOpenMail.Encoding.EncodingType.QuotedPrintable, Encoding.UTF8
                            );

                    switch (recipient.RecipientType)
                    {
                    case RecipientType.mapi_to:
                        outMessage.AddToAddress(mailAddress);
                        //outMessage.To.Add(mailAddress);
                        break;

                    case RecipientType.mapi_cc:
                        outMessage.AddCcAddress(mailAddress);
                        //outMessage.CC.Add(mailAddress);
                        break;

                    case RecipientType.mapi_bcc:
                        outMessage.AddBccAddress(mailAddress);
                        //outMessage.Bcc.Add(mailAddress);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }
            if (message.AttachmentCount > 0)
            {
                foreach (var attachment in message.Attachments)
                {
                    var filename = attachment.Filename;

                    if (attachment.IsMessage)
                    {
                        var embeddedMessage = attachment.OpenAsMessage();
                        var messageWriter   = new MimeMessageWriter();
                        var msgStream       = new MemoryStream();
                        messageWriter.Write(embeddedMessage, msgStream);

                        //msgStream = new MemoryStream(msgStream.ToArray());
                        //.Flush();
                        //msgStream.Seek(0, SeekOrigin.Begin);

                        var messageAttachment =
                            new DotNetOpenMail.FileAttachment(msgStream.ToArray());
                        messageAttachment.ContentType = "message/rfc822";
                        messageAttachment.FileName    = filename + ".eml";
                        //new Attachment(msgStream, filename + ".eml", "message/rfc822");

                        outMessage.AddMixedAttachment(messageAttachment);
                        //outMessage.Attachments.Add(messageAttachment);
                    }
                    else
                    {
                        var fileAttachment =
                            new DotNetOpenMail.FileAttachment(attachment.Bytes);
                        fileAttachment.FileName    = filename;
                        fileAttachment.ContentType = "application/octet-stream";

                        //    new Attachment(new MemoryStream(attachment.Bytes), filename);
                        //outMessage.Attachments.Add(fileAttachment);
                        outMessage.AddMixedAttachment(fileAttachment);
                    }
                }
            }
            if (message.HasBody && message.HasHtmlBody)
            {
                //first we create the Plain Text part
                //var plainTextBody = AlternateView.CreateAlternateViewFromString(message.Body, null, "text/plain");
                //then we create the Html part
                //var htmlBody = AlternateView.CreateAlternateViewFromString(message.HtmlBody, null, "text/html");

                //outMessage.AlternateViews.Add(plainTextBody);
                //outMessage.AlternateViews.Add(htmlBody);
                outMessage.TextPart          = new DotNetOpenMail.TextAttachment(message.Body);
                outMessage.TextPart.CharSet  = Encoding.UTF8;
                outMessage.TextPart.Encoding = DotNetOpenMail.Encoding.EncodingType.QuotedPrintable;

                outMessage.HtmlPart          = new DotNetOpenMail.HtmlAttachment(Encoding.UTF8.GetString(message.HtmlBody));
                outMessage.HtmlPart.CharSet  = Encoding.UTF8;
                outMessage.HtmlPart.Encoding = DotNetOpenMail.Encoding.EncodingType.QuotedPrintable;
            }
            else if (message.HasBody)
            {
                //outMessage.Body = message.Body;
                //outMessage.BodyText = message.Body;
                outMessage.TextPart          = new DotNetOpenMail.TextAttachment(message.Body);
                outMessage.TextPart.CharSet  = Encoding.UTF8;
                outMessage.TextPart.Encoding = DotNetOpenMail.Encoding.EncodingType.QuotedPrintable;
            }
            else if (message.HasHtmlBody)
            {
#warning Encoding is not detected
                //outMessage.TextPart = new DotNetOpenMail.TextAttachment("");
                outMessage.HtmlPart          = new DotNetOpenMail.HtmlAttachment(Encoding.UTF8.GetString(message.HtmlBody));
                outMessage.HtmlPart.CharSet  = Encoding.UTF8;
                outMessage.HtmlPart.Encoding = DotNetOpenMail.Encoding.EncodingType.QuotedPrintable;


                //outMessage.Body = message.HtmlBody;
                //outMessage.IsBodyHtml = true;
            }
            // attempt to get sender details

            PropId senderDisplayNameProp      = 0x0C1A; // sender display name
            PropId senderEmailAddressProp     = 0x0C1F; // sender email address
            PropId senderDisplayNameProp_Alt  = 0x0042; // sender "representing" display name
            PropId senderEmailAddressProp_Alt = 0x0065; // sender "representing" email address

            string senderDisplayname = string.Empty;

            if (message.PropertyExists(senderDisplayNameProp))
            {
                senderDisplayname =
                    message.GetPropertyType(senderDisplayNameProp).Value == (ushort)PropertyType.KnownValue.prop_type_string
                    ? PropertyHelper.GetEncodedStringProperty(message.ReadProperty(senderDisplayNameProp), Encoding.Default)
                    : PropertyHelper.GetUnicodeStringProperty(message.ReadProperty(senderDisplayNameProp));
            }
            else if (message.PropertyExists(senderDisplayNameProp_Alt))
            {
                senderDisplayname =
                    message.GetPropertyType(senderDisplayNameProp_Alt).Value == (ushort)PropertyType.KnownValue.prop_type_string
                    ? PropertyHelper.GetEncodedStringProperty(message.ReadProperty(senderDisplayNameProp_Alt), Encoding.Default)
                    : PropertyHelper.GetUnicodeStringProperty(message.ReadProperty(senderDisplayNameProp_Alt));
            }

            string senderEmailAddress = string.Empty;
            if (message.PropertyExists(senderEmailAddressProp))
            {
                senderEmailAddress =
                    message.GetPropertyType(senderEmailAddressProp).Value == (ushort)PropertyType.KnownValue.prop_type_string
                    ? PropertyHelper.GetEncodedStringProperty(message.ReadProperty(senderEmailAddressProp), Encoding.Default)
                    : PropertyHelper.GetUnicodeStringProperty(message.ReadProperty(senderEmailAddressProp));
            }
            else if (message.PropertyExists(senderEmailAddressProp_Alt))
            {
                senderEmailAddress =
                    message.GetPropertyType(senderEmailAddressProp_Alt).Value == (ushort)PropertyType.KnownValue.prop_type_string
                    ? PropertyHelper.GetEncodedStringProperty(message.ReadProperty(senderEmailAddressProp_Alt), Encoding.Default)
                    : PropertyHelper.GetUnicodeStringProperty(message.ReadProperty(senderEmailAddressProp_Alt));
            }

            var senderAddress =
                new DotNetOpenMail.EmailAddress(string.IsNullOrEmpty(senderEmailAddress) ? " " : senderEmailAddress, senderDisplayname, DotNetOpenMail.Encoding.EncodingType.QuotedPrintable, Encoding.UTF8);
            //new MailAddress(string.IsNullOrEmpty(senderEmailAddress) ? "*****@*****.**" : senderEmailAddress, senderDisplayname);
            //if (senderAddress.Address == "*****@*****.**")
            //    SetEmptyAddress(senderAddress);
            outMessage.FromAddress = senderAddress;

            //outMessage.Sender = senderAddress;
            //outMessage.From = senderAddress;

            // TODO: pull this prop from IMessage instance... -th
            //outMessage.ReplyTo = senderAddress;

            // for now, this is pointless, since the implementation of MailMessage.Send
            // overwrites this with current time... <sigh> -th
            PropId sentTimeProp = 0x0039; // "client submit" time

            if (message.PropertyExists(sentTimeProp))
            {
                var sentTime = (DateTime)PropertyHelper.GetTimeProperty(message.ReadProperty(sentTimeProp));

                outMessage.AddCustomHeader("Date", sentTime.ToString("ddd, dd MMM yyyy HH:mm:ss zzz", System.Globalization.CultureInfo.InvariantCulture).Remove(29, 1));
                //MimeDateTime(sentTime));
                //outMessage.Headers.Add("Date", MimeDateTime(sentTime));
            }
            using (var sw = new StreamWriter(stream))
                sw.Write(outMessage.ToDataString());

            //outMessage.Save(stream);
        }