public Int32 SaveDraftOld(dtoMailMessage eMail)
        {
            try
            {
                Mail mail = Mapper.Map <Mail>(eMail);
                PrepareDraftMailData(mail);

                if (eMail.MailId == 0)
                {
                    dataContext.Mails.Add(mail);
                    int retValue = dataContext.SaveChanges();
                    dataContext = new MailManagerDBConnection();
                }
                else
                {
                    dataContext.Entry(mail).State = EntityState.Modified;
                    Int32 retval = dataContext.SaveChanges();
                    dataContext = new MailManagerDBConnection();
                }

                return(mail.MailId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private dtoMailMessage validateMailForDraft(dtoMailMessage eMail)
        {
            if (eMail.MailId > 0)
            {
                return(eMail);
            }

            return(SaveDraft(eMail));
        }
        private void validateAttachmentOfForwardedMail(dtoMailMessage dtoMail, Int32 NewMailId)
        {
            if (dtoMail.AttachmentsList.Count > 0 && dtoMail.AttachmentsList[0].MailId != NewMailId)
            {
                for (Int32 i = 0; i < dtoMail.AttachmentsList.Count; i++)
                {
                    dtoMail.AttachmentsList[i].AttachmentId = 0;
                    dtoMail.AttachmentsList[i].MailId       = NewMailId;
                    dtoMail.AttachmentsList[i].AttachmentId = mailAttachmentService.SaveAttachment(dtoMail.AttachmentsList[i]);
                }

                fileService.CopyFiles(Convert.ToInt32(dtoMail.AttachmentsList[0].MailId), NewMailId);
            }
        }
        public dtoMailMessage SaveDraft(dtoMailMessage eMail)
        {
            using (DbContextTransaction transaction = dataContext.Database.BeginTransaction())
            {
                try
                {
                    Mail mail = Mapper.Map <Mail>(eMail);
                    PrepareDraftMailData(mail);

                    if (eMail.MailId == 0)
                    {
                        dataContext.Mails.Add(mail);
                        int retValue = dataContext.SaveChanges();
                        eMail.MailId = mail.MailId;
                        if (eMail.AttachmentsList.Count > 0 && eMail.AttachmentsList[0].MailId != mail.MailId)
                        {
                            var sourceMailId = Convert.ToInt32(eMail.AttachmentsList[0].MailId);

                            for (Int32 i = 0; i < eMail.AttachmentsList.Count; i++)
                            {
                                eMail.AttachmentsList[i].AttachmentId = 0;
                                eMail.AttachmentsList[i].MailId       = mail.MailId;

                                MailAttachment attch = Mapper.Map <MailAttachment>(eMail.AttachmentsList[i]);
                                dataContext.MailAttachments.Add(attch);
                                dataContext.SaveChanges();

                                eMail.AttachmentsList[i].AttachmentId = attch.AttachmentId;
                            }

                            fileService.CopyFiles(sourceMailId, mail.MailId);
                        }
                    }
                    else
                    {
                        dataContext.Entry(mail).State = EntityState.Modified;
                        Int32 retval = dataContext.SaveChanges();
                    }

                    transaction.Commit();
                    return(eMail);
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    dataContext = new MailManagerDBConnection();
                    throw ex;
                }
            }
        }
        public IHttpActionResult PutMail(Int32 Id, [FromBody] dtoMailMessage mailToSave)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                return(Ok(mailMessageService.SaveDraft(mailToSave)));
            }
            catch (Exception ex)
            {
                return(InternalServerError(new Exception(ex.Message)));
            }
        }
        public dtoMailMessage GetMailDetails(Int32 mailId)
        {
            var mailDetail = dataContext.Mails.Where(m => m.MailId == mailId).FirstOrDefault();

            if (mailDetail == null)
            {
                return(null);
            }

            dtoMailMessage dtoMail = Mapper.Map <dtoMailMessage>(mailDetail);

            dtoMail.FromUser = Mapper.Map <dtoUserAccount>(dataContext.UserAccounts.Where(u => u.EmailId == dtoMail.FromEmail).FirstOrDefault());
            string[] tm = dtoMail.ToEmail.Split(',');
            dtoMail.ToUsersList     = Mapper.Map <List <dtoUserAccount> >(dataContext.UserAccounts.Where(u2 => tm.Contains(u2.EmailId)).ToList());
            dtoMail.AttachmentsList = Mapper.Map <List <dtoMailAttachment> >(dataContext.MailAttachments.Where(a1 => a1.MailId == dtoMail.MailId).ToList());
            return(dtoMail);
        }
        public IHttpActionResult GetMail(Int32 Id)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                dtoMailMessage mailDetail = mailMessageService.GetMailDetails(Id);

                if (mailDetail == null)
                {
                    return(NotFound());
                }

                return(Ok(mailDetail));
            }
            catch (Exception ex)
            {
                return(InternalServerError(new Exception(ex.Message)));
            }
        }
        public dtoMailTransactionResult SendMail(dtoMailMessage eMail)
        {
            try
            {
                dtoMailTransactionResult mailResp = new dtoMailTransactionResult();
                Mail mailMessage            = new Mail();
                bool senderMailUpdateStatus = false;

                eMail = validateMailForDraft(eMail);

                var emailsToSend = eMail.ToEmail.Split(',');

                foreach (string toEmail in emailsToSend)
                {
                    mailMessage = Mapper.Map <Mail>(eMail); //mapping

                    //prepare eMail Message for Reveiver
                    PrepareReceiverMailData(mailMessage, toEmail);

                    MailStatus mailStatus = new MailStatus();
                    mailStatus.EmailId          = toEmail.Trim();
                    mailStatus.IsSentSuccessful = false;
                    using (DbContextTransaction transaction = dataContext.Database.BeginTransaction())
                    {
                        try
                        {
                            //Save reveiver Mail
                            dataContext.Mails.Add(mailMessage);
                            int resCount = dataContext.SaveChanges();

                            foreach (var dtoAttach in eMail.AttachmentsList)
                            {
                                MailAttachment mAttach = new MailAttachment();
                                mAttach.AttachmentId = 0;
                                mAttach.MailId       = mailMessage.MailId;
                                mAttach.Attachment   = dtoAttach.Attachment;

                                dataContext.MailAttachments.Add(mAttach);
                                int attRes = dataContext.SaveChanges();
                            }

                            mailStatus.MailId           = mailMessage.MailId; //
                            mailStatus.IsSentSuccessful = true;
                            mailResp.Mails.Add(mailStatus);

                            if (!senderMailUpdateStatus)
                            {
                                Mail senderMail = Mapper.Map <Mail>(eMail); //mapping
                                PrepareSenderMailData(senderMail);
                                //Update sender mail Status
                                dataContext.Entry(senderMail).State = EntityState.Modified;
                                Int32 senderReturnVal = dataContext.SaveChanges();
                                senderMailUpdateStatus = true;
                                mailResp.ResultSattus  = true;
                            }

                            transaction.Commit();

                            fileService.CopyFiles(eMail.MailId, mailMessage.MailId);
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            mailStatus.IsSentSuccessful = false;
                            mailResp.Mails.Add(mailStatus);
                        }
                    }
                }

                return(mailResp);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #9
0
 private MailAddress Sender(dtoMailMessage dtoMessage)
 {
     return((SMTPconfig.RelayAllowed) ? ((MailSettings.Sender == SenderType.System || dtoMessage.FromUser == null) ? SMTPconfig.System.Sender : dtoMessage.FromUser) : SMTPconfig.System.Sender);
 }
Exemple #10
0
 private MailAddress From(dtoMailMessage dtoMessage)
 {
     return((MailSettings.Sender == SenderType.System) ? SMTPconfig.System.Sender : dtoMessage.FromUser);
     //return (SMTPconfig.RelayAllowed) ? ((MailSettings.Sender == SenderType.System) ? SMTPconfig.SystemSender : dtoMessage.FromUser) : ((SMTPconfig.SystemSender.Address ==dtoMessage.FromUser.Address) ?  SMTPconfig.SystemSender : new MailAddress( SMTPconfig.SystemSender.Address, dtoMessage.FromUser.Address)) ;
 }
Exemple #11
0
 private String SubjectForCopy(Int32 idUserLanguage, Language dLanguage, dtoMailMessage dtoMessage)
 {
     return((MailSettings.Subject == SubjectType.Free) ? SMTPconfig.System.SubjectForSenderCopy(idUserLanguage, dLanguage) + dtoMessage.UserSubject : SMTPconfig.System.SubjectPrefix(idUserLanguage, dLanguage) + SMTPconfig.System.SubjectForSenderCopy(idUserLanguage, dLanguage) + dtoMessage.UserSubject);
 }