Exemplo n.º 1
0
        private Error addAttachments(EMailQueue email, List <string> attachments, FileCopyType fileCopyType)
        {
            var error = new Error();

            if (attachments != null && attachments.Count() > 0)
            {
                // Queue the attachments
                int orderNo = 1;
                foreach (var fileName in attachments)
                {
                    // Copy or move the file to a temporary location for sending. After sending, we delete it.
                    string tempFolder = GetConfigurationSetting("MediaFolder", "") + "\\EMailQueue\\" + email.Id.ToString();
                    try {
                        Directory.CreateDirectory(tempFolder);
                    } catch { }
                    db.LogTestFolder(tempFolder);

                    // Copy/move the file
                    string targetFile = tempFolder + "\\" + fileName.FileName();

                    try {
                        if (fileCopyType == FileCopyType.Move)
                        {
                            File.Move(fileName, targetFile);
                        }
                        else
                        {
                            File.Copy(fileName, targetFile, true);
                        }

                        var attachment = new EMailQueueAttachment {
                            EMailQueue = email,
                            FileName   = targetFile,
                            OrderNo    = orderNo++
                        };
                        db.InsertOrUpdateEMailQueueAttachment(attachment);
                    } catch (Exception e1) {
                        error.SetError(e1);
                        break;
                    }
                }
            }
            return(error);
        }
Exemplo n.º 2
0
        public Error SendEMail(EMailMessage message)
        {
            var error = new Error();

            string subject = message.Subject,
                   text    = message.Message;

            // Find/validate the message template (if specified)
            if (message.TemplateId != MessageTemplateType.None)
            {
                var messageTemplate = db.FindMessageTemplate(_company.Id, message.TemplateId);
                if (messageTemplate == null)
                {
                    error.SetError(EvolutionResources.errEMailTemplateNotFound);
                }
                else
                {
                    subject = messageTemplate.Subject;
                    text    = messageTemplate.Message;
                }
            }

            if (!error.IsError)
            {
                // Validate the sender
                if (message.Sender == null ||
                    string.IsNullOrEmpty(message.Sender.EMail) ||
                    !message.Sender.EMail.IsValidEMail())
                {
                    error.SetError(EvolutionResources.errInvalidSenderEMailAddress, "", (message.Sender == null ? "[Invalid EMail]" : message.Sender.EMail), (message.Sender == null ? "[No Name]" : message.Sender.FullName));
                }
                else
                {
                    // Now validate the recipients
                    if (message.Recipients.Count() == 0)
                    {
                        error.SetError(EvolutionResources.errNoToRecipientsSelected);
                    }
                    else
                    {
                        string recList  = "",
                               copyList = "";

                        foreach (var user in message.Recipients)
                        {
                            if (string.IsNullOrEmpty(user.EMail) || !user.EMail.IsValidEMail())
                            {
                                if (string.IsNullOrEmpty(error.Message))
                                {
                                    error.SetError(EvolutionResources.errInvalidEMailsForUsers);
                                }
                                error.Message += $"<br/>{user.FullName} ({user.EMail})";
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(recList))
                                {
                                    recList += ",";
                                }
                                recList += user.EMail;
                            }
                        }
                        foreach (var user in message.Copies)
                        {
                            if (string.IsNullOrEmpty(user.EMail) || !user.EMail.IsValidEMail())
                            {
                                if (string.IsNullOrEmpty(error.Message))
                                {
                                    error.SetError(EvolutionResources.errInvalidEMailsForUsers);
                                }
                                error.Message += $"<br/>{user.FullName} ({user.EMail})";
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(copyList))
                                {
                                    copyList += ",";
                                }
                                copyList += user.EMail;
                            }
                        }

                        // Even though we can get errors above, we still send the message
                        // to all the valid addresses
                        if (!string.IsNullOrEmpty(recList))
                        {
                            AddOrganisationDetails(message.Dict);
                            AddUserDetails(message.Sender, message.Dict);

                            var newEMail = new EMailQueue {
                                QueuedDate       = DateTime.Now,
                                SenderAddress    = message.Sender.EMail,
                                ReplyToAddress   = message.Sender.EMail,
                                RecipientAddress = recList,
                                CopyAddress      = copyList,
                                BCCAddress       = _company.EmailAddressBCC,
                                MessageSubject   = subject.DoSubstitutions(message.Dict),
                                MessageText      = text.DoSubstitutions(message.Dict)
                            };
                            db.InsertOrUpdateEMailQueue(newEMail);

                            error    = addAttachments(newEMail, message.Attachments, message.FileCopyType);
                            error.Id = newEMail.Id;
                        }
                    }
                }
            }
            return(error);
        }
Exemplo n.º 3
0
        EMailQueueModel mapToModel(EMailQueue item)
        {
            var newItem = Mapper.Map <EMailQueue, EMailQueueModel>(item);

            return(newItem);
        }