Beispiel #1
0
        /// <summary>
        /// 把邮件ContentInfo转换为数据库实体
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="hmailInfo"></param>
        /// <returns></returns>
        public static IList<ContentInfo> ConvertToDbContentEntity(int mailId, HMailInfo hmailInfo)
        {
            if (hmailInfo == null)
                throw new ArgumentNullException("hmailInfo");

            try
            {
                IList<ContentInfo> contentList = new List<ContentInfo>();
                if (hmailInfo.ContentInfo.Count() > 0)
                    hmailInfo.ContentInfo.AsParallel().ForAll(v => contentList.Add(new ContentInfo(null,mailId,v.ContentType,v.Charset,v.Encoding,v.Content)));
                return contentList;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Beispiel #2
0
        /// <summary>
        /// 把邮件AttachmentInfo转换为数据库实体
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="hmailInfo"></param>
        /// <returns></returns>
        public static IList<AttachmentInfo> ConvertToDbAttachEntity(int mailId, HMailInfo hmailInfo)
        {
            if (hmailInfo == null)
                throw new ArgumentNullException("hmailInfo");

            try
            {
                IList<AttachmentInfo> AttList = new List<AttachmentInfo>();
                if (hmailInfo.Attachments.Count() > 0)
                    hmailInfo.Attachments.AsParallel().ForAll(v => AttList.Add(new AttachmentInfo(null, mailId, v.FileName, v.ContentType, v.Encoding, v.FileBuffer)));
                return AttList;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Beispiel #3
0
        /// <summary>
        /// 把邮件MailInfo转换为数据库实体
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="hmailInfo"></param>
        /// <returns></returns>
        public static MailInfo ConvertToDbMailEntity(int userId, HMailInfo hmailInfo)
        {
            if(hmailInfo == null)
                throw new ArgumentNullException("hmailInfo");

            try
            {
                MailInfo mailTmp = new MailInfo();
                StringBuilder sb = new StringBuilder();
                mailTmp.Subject = hmailInfo.SubJect;
                mailTmp.FromAddress = hmailInfo.From.Address;

                if (hmailInfo.To != null)
                    hmailInfo.To.AsParallel().ForAll((s) => { sb.Append(string.Format("{0}{1}", s.Address, ",")); });
                mailTmp.ToAddress = sb.ToString().TrimEnd(',');

                sb.Clear();
                if (hmailInfo.Cc != null)
                    hmailInfo.Cc.AsParallel().ForAll((v) => { sb.Append(string.Format("{0}{1}", v.Address, ",")); });
                mailTmp.Cc = sb.ToString().TrimEnd(',');

                sb.Clear();
                if (hmailInfo.Bcc != null)
                    hmailInfo.Bcc.AsParallel().ForAll((n) => { sb.Append(string.Format("{0}{1}", n.Address, ",")); });
                mailTmp.Bcc = sb.ToString().TrimEnd(',');

                mailTmp.ReceiveDate = ConvertCommon.ConvertFromMailDate(hmailInfo.Date);
                mailTmp.DownloadDate = DateTime.Now;
                mailTmp.Type = (int)MailInfo.MailType.Common;
                mailTmp.UserId = userId;
                mailTmp.Del = 0;
                mailTmp.Status = (int)MailInfo.MailStatus.Unread;

                return mailTmp;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Beispiel #4
0
        //获得单条邮件
        public HMailInfo GetMailInfoById(int id, ShortOrWhole type)
        {
            try
            {
                //for test
                id = 2;

                HMailInfo mailInfo = new HMailInfo();
                string cmd = "top " + id.ToString() + " 50";
                if (type == ShortOrWhole.Whole)
                    cmd = "retr " + id.ToString();
                result = ExcuteMailCommand(cmd, Encoding.UTF8);
                mailEncoding = "UTF-8";

                //if (!(result.Contains("charset=\"UTF-8\"") || result.Contains("charset=\"utf-8\"") || result.Contains("charset=utf-8") || result.Contains("charset=UTF-8")))
                    //GetMailEncoding(cmd);
                mailInfo.MailId = id;
                string _partten = "((?=References:)|(?=MIME-Version:)|(?=Mime-version:)|(?=Mime-Version:)|(?=Message-ID:)|(?=Message-Id:)|(?=Date:)|(?=date:)|(?=Content-Type:)|(?=content-type:)|(?=--)|(?=To:)|(?=to:)|(?=TO:)|(?=Subject:)|(?=subject:)|(?=SUBJECT:)|(?=Cc:)|(?=CC:)|(?=cc:)|(?=From:)|(?=from:)|(?=FROM:)|(?=X-Originating-IP:)|(?=X-Coremail-Antispam:)|(?=Sender:)|(?=Reply-To:)|(?=Reply-to)|(?=X-Mailer:)|(?=X-Priority:)|(?=\r\n\r\n))";
                mailInfo.To = GetAddress(MatchAndDecodeMail(result, string.Format(@"((?<=\r\nTo:)|(?<=\r\nTO:)|(?<=\r\nto:))[\s\S]*?{0}", _partten)));
                mailInfo.From = GetAddress(MatchAndDecodeMail(result, string.Format(@"((?<=\r\nFrom:)|(?<=\r\nfrom:)|(?<=\r\nFROM:))[\s\S]*?{0}", _partten)))[0];

                mailInfo.Cc = GetAddress(MatchAndDecodeMail(result, string.Format(@"((?<=\r\nCc:)|(?<=\r\nCC:)|(?<=\r\ncc:))[\s\S]*?{0}", _partten)));
                mailInfo.SubJect = MatchAndDecodeMail(result, string.Format(@"((?<=\r\nSubject:)|(?<=\r\nsubject:)|(?<=\r\nSUBJECT:))[\s\S]*?{0}", _partten));
                mailInfo.Date = MatchAndDecodeMail(result, string.Format(@"((?<=\r\nDate:)|(?<=\r\ndate:))[\s\S]*?{0}", _partten));
                if (type == ShortOrWhole.Whole)
                {
                    IList<HMailAttachmentInfo> mailMentInfo = new List<HMailAttachmentInfo>();
                    mailInfo.ContentInfo = GetMailContent(result, ref mailMentInfo);
                    mailInfo.Attachments = mailMentInfo;
                }
                GetOneMailEvent();
                return mailInfo;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #5
0
 //發送郵件
 public static bool SendHMail(HMailInfo hMailInfo)
 {
     MailMessage msg = new MailMessage();
     msg.From = hMailInfo.From;
     foreach (MailAddress mail in hMailInfo.To)
         msg.To.Add(mail);
     foreach (MailAddress mail in hMailInfo.Cc)
         msg.CC.Add(mail);
     foreach (MailAddress mail in hMailInfo.Bcc)
         msg.Bcc.Add(mail);
     foreach (HMailAttachmentInfo mail in hMailInfo.Attachments)
         msg.Attachments.Add(Attachment.CreateAttachmentFromString(Encoding.GetEncoding(mail.Encoding).GetString(mail.FileBuffer), mail.ContentType));
     StringBuilder strBud = new StringBuilder();
     foreach (HMailContentInfo mail in hMailInfo.ContentInfo)
         strBud.Append(mail.Content);
     msg.Body = strBud.ToString();
     msg.IsBodyHtml = true;
     msg.BodyEncoding = msg.SubjectEncoding = Encoding.UTF8;
     try
     {
         smtpClient.Send(msg);
         return true;
     }
     catch (Exception ex)
     {
         SendMailFAailedEvent(ex);
         return false;
     }
 }