Ejemplo n.º 1
0
        /// <summary>
        /// 发送阅读回折邮件
        /// </summary>
        /// <param name="sHtmlText">邮件内容</param>
        /// <param name="from">发送人</param>
        /// <param name="to">接收人</param>
        public void sendReceiptMail(string sHtmlText, string subject, ASObject from, string[] to)
        {
            using (MemoryStreamEx stream = new MemoryStreamEx(32000))
            {
                Mail_Message m = new Mail_Message();
                m.MimeVersion = "1.0";
                m.Date = DateTime.Now;
                m.MessageID = MIME_Utils.CreateMessageID();

                m.Subject = subject;
                StringBuilder sb = new StringBuilder();
                foreach (string p in to)
                {
                    if (sb.Length > 0)
                        sb.Append(",");
                    sb.Append(p);
                }
                m.To = Mail_t_AddressList.Parse(sb.ToString());

                //--- multipart/alternative -----------------------------------------------------------------------------------------
                MIME_h_ContentType contentType_multipartAlternative = new MIME_h_ContentType(MIME_MediaTypes.Multipart.alternative);
                contentType_multipartAlternative.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.');
                MIME_b_MultipartAlternative multipartAlternative = new MIME_b_MultipartAlternative(contentType_multipartAlternative);
                m.Body = multipartAlternative;

                //--- text/plain ----------------------------------------------------------------------------------------------------
                MIME_Entity entity_text_plain = new MIME_Entity();
                MIME_b_Text text_plain = new MIME_b_Text(MIME_MediaTypes.Text.plain);
                entity_text_plain.Body = text_plain;
                text_plain.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, sHtmlText);
                multipartAlternative.BodyParts.Add(entity_text_plain);

                //--- text/html ------------------------------------------------------------------------------------------------------
                MIME_Entity entity_text_html = new MIME_Entity();
                MIME_b_Text text_html = new MIME_b_Text(MIME_MediaTypes.Text.html);
                entity_text_html.Body = text_html;
                text_html.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, sHtmlText);
                multipartAlternative.BodyParts.Add(entity_text_html);

                MIME_Encoding_EncodedWord headerwordEncoder = new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8);
                m.ToStream(stream, headerwordEncoder, Encoding.UTF8);
                stream.Position = 0;

                SMTP_Client.QuickSendSmartHost(null, from.getString("send_address", "stmp.sina.com"), from.getInt("send_port", 25),
                    from.getBoolean("is_send_ssl", false), from.getString("account"), PassUtil.Decrypt(from.getString("password")),
                    from.getString("account"), to, stream);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Clones mail message.
        /// </summary>
        /// <returns>Returns cloned message.</returns>
        /// <exception cref="ObjectDisposedException">Is raised when this object is disposed and this method is accessed.</exception>
        public Mail_Message Clone()
        {
            if(this.IsDisposed){
                throw new ObjectDisposedException(this.GetType().Name);
            }

            MemoryStreamEx ms = new MemoryStreamEx(64000);
            this.ToStream(ms,null,null);
            ms.Position = 0;

            return Mail_Message.ParseFromStream(ms);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="mm">邮件对象</param>
        /// <param name="from">发送人</param>
        /// <param name="to">接收人</param>
        private void send(Mail_Message mm, ASObject from, string[] to)
        {
            using (MemoryStreamEx stream = new MemoryStreamEx(32000))
            {
                MIME_Encoding_EncodedWord headerwordEncoder = new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8);
                mm.ToStream(stream, headerwordEncoder, Encoding.UTF8);
                stream.Position = 0;

                SMTP_Client.QuickSendSmartHost(null, from.getString("send_address", "stmp.sina.com"), from.getInt("send_port", 25),
                    from.getBoolean("is_send_ssl", false), from.getString("account"), PassUtil.Decrypt(from.getString("password")),
                    from.getString("account"), to, stream);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Sets body data from the specified stream.
        /// </summary>
        /// <param name="stream">Source stream.</param>
        /// <param name="transferEncoding">Specifies content-transfer-encoding to use to encode data.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> or <b>transferEncoding</b> is null reference.</exception>
        /// <exception cref="InvalidOperationException">Is raised when this method is accessed and this body is not bounded to any entity.</exception>
        public void SetData(Stream stream,string transferEncoding)
        {
            if(stream == null){
                throw new ArgumentNullException("stream");
            }
            if(transferEncoding == null){
                throw new ArgumentNullException("transferEncoding");
            }

            if(string.Equals(transferEncoding,MIME_TransferEncodings.QuotedPrintable,StringComparison.InvariantCultureIgnoreCase)){
                using(MemoryStreamEx fs = new MemoryStreamEx(32000)){
                    QuotedPrintableStream encoder = new QuotedPrintableStream(new SmartStream(fs,false),FileAccess.ReadWrite);
                    Net_Utils.StreamCopy(stream,encoder,32000);
                    encoder.Flush();
                    fs.Position = 0;
                    SetEncodedData(transferEncoding,fs);
                }
            }
            else if(string.Equals(transferEncoding,MIME_TransferEncodings.Base64,StringComparison.InvariantCultureIgnoreCase)){
                using(MemoryStreamEx fs = new MemoryStreamEx(32000)){
                    Base64Stream encoder = new Base64Stream(fs,false,true,FileAccess.ReadWrite);
                    Net_Utils.StreamCopy(stream,encoder,32000);
                    encoder.Finish();
                    fs.Position = 0;
                    SetEncodedData(transferEncoding,fs);
                }
            }
            else if(string.Equals(transferEncoding,MIME_TransferEncodings.Binary,StringComparison.InvariantCultureIgnoreCase)){
                SetEncodedData(transferEncoding,stream);
            }
            else if(string.Equals(transferEncoding,MIME_TransferEncodings.EightBit,StringComparison.InvariantCultureIgnoreCase)){
                SetEncodedData(transferEncoding,stream);
            }
            else if(string.Equals(transferEncoding,MIME_TransferEncodings.SevenBit,StringComparison.InvariantCultureIgnoreCase)){
                SetEncodedData(transferEncoding,stream);
            }
            else{
                throw new NotSupportedException("Not supported Content-Transfer-Encoding '" + transferEncoding + "'.");
            }
        }
Ejemplo n.º 5
0
        void btnSendMail_Click(object sender, RoutedEventArgs e)
        {
            using (MemoryStreamEx stream = new MemoryStreamEx(32000))
            {
                try
                {
                    if (String.IsNullOrWhiteSpace(txtTo.Text))
                        throw new Exception("收件人不能为空!");

                    if (Mail_Message == null)
                        Mail_Message = createMessage();

                    updateMessage(Mail_Message);

                    if ((Mail_Type == MailType.AllReply || Mail_Type == MailType.Reply) && _mail != null)
                        Mail_Message.InReplyTo = _mail["message_id"] as string;

                    MIME_Encoding_EncodedWord headerwordEncoder = new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8);
                    Mail_Message.ToStream(stream, headerwordEncoder, Encoding.UTF8);

                    int mail_type = (int)DBWorker.MailType.OutboxMail;
                    string folder = "OUTBOX";
                    if (reviewer != null)
                    {
                        mail_type = (int)DBWorker.MailType.SendMail;
                        folder = "DSBOX";
                    }
                    if (_saveMail == null)
                    {
                        StringBuilder sb = new StringBuilder();
                        string uuid = Guid.NewGuid().ToString();
                        sb.Append(MailReceiveWorker.getFilePath(uuid)).Append("/").Append(uuid).Append(".eml");
                        string file = sb.ToString();
                        DirectoryInfo dir = Directory.GetParent(store_path + file);
                        if (!dir.Exists)
                            dir.Create();

                        Mail_Message.ToFile(store_path + file, headerwordEncoder, Encoding.UTF8);
                        _saveMail = saveMail(null, Mail_Message, uuid, file, mail_type, folder);
                        if (mail_type == (int)DBWorker.MailType.SendMail)
                        {
                            _saveMail["reviewer_id"] = reviewer.getLong("id");
                            _saveMail["reviewer_name"] = reviewer.getString("name");
                            _saveMail["operator_id"] = Desktop.instance.loginedPrincipal.id;
                            _saveMail["operator_name"] = Desktop.instance.loginedPrincipal.name;
                        }
                        MailWorker.instance.dispatchMailEvent(MailWorker.Event.Create, _saveMail, null);

                        if (mail_type == (int)DBWorker.MailType.SendMail)
                        {
                            MailWorker.instance.syncUserMail(_saveMail);
                        }
                    }
                    else
                    {
                        List<string> list = getUpdateFields();
                        string file = _saveMail["mail_file"] as string;
                        string uuid = _saveMail["uuid"] as string;
                        Mail_Message.ToFile(store_path + file, headerwordEncoder, Encoding.UTF8);
                        _saveMail = saveMail(_saveMail, Mail_Message, uuid, file, mail_type, folder);
                        if (mail_type == (int)DBWorker.MailType.SendMail)
                        {
                            _saveMail["reviewer_id"] = reviewer.getLong("id");
                            _saveMail["reviewer_name"] = reviewer.getString("name");
                            list.Add("reviewer_id");
                            list.Add("reviewer_name");
                            if (String.IsNullOrWhiteSpace(_saveMail.getString("operator_id")))
                            {
                                _saveMail["operator_id"] = Desktop.instance.loginedPrincipal.id;
                                _saveMail["operator_name"] = Desktop.instance.loginedPrincipal.name;
                                list.Add("operator_id");
                                list.Add("operator_name");
                            }
                        }
                        MailWorker.instance.dispatchMailEvent(MailWorker.Event.Update, _saveMail, list.ToArray());
                    }

                    if (Mail_Type == MailType.AllReply || Mail_Type == MailType.Reply)
                    {
                        _mail["handle_action"] = 2;
                        _mail["is_handled"] = true;

                        MailWorker.instance.dispatchMailEvent(MailWorker.Event.Update, _mail, new string[] { "handle_action", "is_handled" });
                    }
                    if (mail_type == (int)DBWorker.MailType.OutboxMail)
                    {
                        MailSendWorker.instance.AddMail(_saveMail);
                        MailSendWorker.instance.Start();
                    }

                    this.Close();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.Write(ex.StackTrace);
                    this.Dispatcher.BeginInvoke((System.Action)delegate
                    {
                        ime.controls.MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    }, System.Windows.Threading.DispatcherPriority.ApplicationIdle);
                }
            }
        }
Ejemplo n.º 6
0
        void btnSave_Click(object sender, RoutedEventArgs e)
        {
            using (MemoryStreamEx stream = new MemoryStreamEx(32000))
            {
                try
                {
                    if (Mail_Message == null)
                        Mail_Message = createMessage();

                    updateMessage(Mail_Message);
                    MIME_Encoding_EncodedWord headerwordEncoder = new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8);
                    Mail_Message.ToStream(stream, headerwordEncoder, Encoding.UTF8);

                    if (_saveMail == null)
                    {
                        //存放邮件到草稿目录 DRAFT
                        StringBuilder sb = new StringBuilder();
                        string uuid = Guid.NewGuid().ToString();
                        sb.Append(MailReceiveWorker.getFilePath(uuid)).Append("/").Append(uuid).Append(".eml");
                        string file = sb.ToString();
                        DirectoryInfo dir = Directory.GetParent(store_path + file);
                        if (!dir.Exists)
                            dir.Create();

                        Mail_Message.ToFile(store_path + file, headerwordEncoder, Encoding.UTF8);
                        _saveMail = saveMail(null, Mail_Message, uuid, file, (int)DBWorker.MailType.DraftMail, "DRAFT");

                        MailWorker.instance.dispatchMailEvent(MailWorker.Event.Create, _saveMail, null);
                        MailWorker.instance.syncUserMail(_saveMail);
                    }
                    else
                    {
                        string file = _saveMail["mail_file"] as string;
                        string uuid = _saveMail["uuid"] as string;
                        Mail_Message.ToFile(store_path + file, headerwordEncoder, Encoding.UTF8);
                        _saveMail = saveMail(_saveMail, Mail_Message, uuid, file, (int)DBWorker.MailType.DraftMail, "DRAFT");

                        MailWorker.instance.dispatchMailEvent(MailWorker.Event.Update, _saveMail, getUpdateFields().ToArray());
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.Write(ex.StackTrace);
                    this.Dispatcher.BeginInvoke((System.Action)delegate
                    {
                        ime.controls.MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    }, System.Windows.Threading.DispatcherPriority.ApplicationIdle);
                }
            }
        }