Ejemplo n.º 1
0
        public SendMailResponse Send(SendMailRequest request)
        {
            SendMailResponse result = new SendMailResponse();

            try
            {
                result.RequestGuid = Guid.NewGuid();
                smtpClient.Send(request.ToMailMessage());
                SendMailDetail details = new SendMailDetail();

                details.BCC    = string.IsNullOrEmpty(Convert.ToString(request.BCC)) ? null : string.Join(";", request.BCC.ToArray());
                details.CC     = string.IsNullOrEmpty(Convert.ToString(request.CC)) ? null : string.Join(";", request.CC.ToArray());
                details.From   = request.From;
                details.Status = CommunicationStatus.Success;
                details.To     = string.Join(";", request.To.ToArray());
                result.Details.Add(details);
                Response mailresponse = new Response();
                result.RequestGuid = mailresponse.MailResponse(result);
            }
            catch (Exception ex)
            {
                Logger.Current.Error("Exception occurred sending the mail..", ex);
            }
            return(result);
        }
Ejemplo n.º 2
0
        public SendMailResponse SendIndependent(SendMailRequest request)
        {
            var result = new SendMailResponse {
                RequestGuid = Guid.NewGuid()
            };

            smtpClient.Send(request.ToMailMessage());
            return(result);
        }
Ejemplo n.º 3
0
        private SendMailResponse SmtpSend(Guid token, SendMailRequest request)
        {
            Logger.Current.Verbose("sending an email from smtp-mail service");
            var result = new SendMailResponse();

            try
            {
                result.Token = token;
                var message = request.ToMailMessage();
                System.Net.Mail.Attachment attachment = default(System.Net.Mail.Attachment);

                if (request.To.Count() > 1)
                {
                    result.Token = token;
                    request.To.Each(s =>
                    {
                        string mailBody = request.Body;
                        if (request.MergeValues != null)
                        {
                            var mergeData = request.MergeValues.Where(m => m.Key == s).Select(m => m.Value).FirstOrDefault();
                            if (mergeData != null)
                            {
                                mergeData.Each(m =>
                                {
                                    mailBody = mailBody.Replace("*|" + m.Key + "|*", m.Value);
                                });
                            }
                            request.To = new List <string>()
                            {
                                s
                            };
                            var contactMessage  = request.ToMailMessage();
                            contactMessage.Body = mailBody;
                            smtpClient.Send(contactMessage);
                        }
                        else
                        {
                            request.To = new List <string>()
                            {
                                s
                            };
                            var contactMessage  = request.ToMailMessage();
                            contactMessage.Body = mailBody;
                            smtpClient.Send(contactMessage);
                        }
                        result.StatusID = LandmarkIT.Enterprise.CommunicationManager.Responses.CommunicationStatus.Success;
                        Logger.Current.Verbose("Sending an email from smtp-mail service is successful");
                    });
                }
                else
                {
                    if (request.MergeValues != null)
                    {
                        var mergeData = request.MergeValues.Where(m => m.Key == request.To.FirstOrDefault()).Select(m => m.Value).FirstOrDefault();
                        if (mergeData != null)
                        {
                            mergeData.Each(m =>
                            {
                                message.Body = message.Body.Replace("*|" + m.Key + "|*", m.Value);
                            });
                        }
                    }

                    if (request.AttachmentGUID != null && request.AttachmentGUID != Guid.Empty)
                    {
                        string       fileName      = request.AttachmentGUID.ToString() + ".ics";
                        string       savedFileName = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["ATTACHMENT_PHYSICAL_PATH"].ToString(), fileName);
                        StreamReader reader        = new StreamReader(savedFileName);
                        attachment = new System.Net.Mail.Attachment(reader.BaseStream, fileName, MediaTypeNames.Application.Octet);
                        attachment.ContentDisposition.FileName = fileName;
                    }
                    if (request.NightlyAttachmentGUIDS.IsAny())
                    {
                        int i = 0;
                        foreach (Guid giud in request.NightlyAttachmentGUIDS)
                        {
                            System.Net.Mail.Attachment nightlyAttachment = default(System.Net.Mail.Attachment);
                            string fileName  = giud.ToString() + ".csv";
                            string excelPath = System.Configuration.ConfigurationManager.AppSettings["NIGHTLYREPORT_PHYSICAL_PATH"].ToString();

                            var          icsContent         = string.Empty;
                            string       savedExcelFileName = Path.Combine(excelPath, fileName);
                            StreamReader reader             = new StreamReader(savedExcelFileName);
                            nightlyAttachment = new System.Net.Mail.Attachment(reader.BaseStream, fileName, MediaTypeNames.Application.Octet);
                            nightlyAttachment.ContentDisposition.FileName = i == 0 ? "1 Day Report.csv" : "7 Days Report.csv ";
                            message.Attachments.Add(nightlyAttachment);
                            i++;
                        }
                    }

                    result.RequestGuid = request.RequestGuid == Guid.Empty ? Guid.NewGuid() : request.RequestGuid;

                    if (attachment != null)
                    {
                        message.Attachments.Add(attachment);
                    }

                    smtpClient.Send(message);
                    result.StatusID = LandmarkIT.Enterprise.CommunicationManager.Responses.CommunicationStatus.Success;
                    Logger.Current.Verbose("Sending an email from smtp-mail service is successful");
                }
            }
            catch (Exception ex)
            {
                result.StatusID        = LandmarkIT.Enterprise.CommunicationManager.Responses.CommunicationStatus.Failed;
                result.ServiceResponse = ex.Message;
                Logger.Current.Error("An error occured while sending an email from smtp-mail service/send-grid", ex);
                ExceptionHandler.Current.HandleException(ex, DefaultExceptionPolicies.LOG_ONLY_POLICY, values: new object[] { request });
            }
            return(result);
        }