Beispiel #1
0
        //
        //====================================================================================================
        /// <summary>
        /// Send email by SMTP. return 'ok' if success, else return a user compatible error message
        /// </summary>
        public static bool send(CoreController core, AmazonSimpleEmailServiceClient client, EmailSendDomainModel email, ref string reasonForFail)
        {
            string logShortDetail = ", subject [" + email.subject + "], toMemberId [" + email.toMemberId + "], toAddress [" + email.toAddress + "], fromAddress [" + email.fromAddress + "]";
            string logLongDetail  = logShortDetail + ", BounceAddress [" + email.bounceAddress + "], replyToAddress [" + email.replyToAddress + "]";

            reasonForFail = "";
            try {
                if (core.mockEmail)
                {
                    //
                    // -- for unit tests, mock interface by adding email to core.mockSmptList
                    core.mockEmailList.Add(new MockEmailClass {
                        email = email
                    });
                    return(true);
                }
                //
                // -- test for email bounce block list
                using (var cs = core.cpParent.CSNew()) {
                    string sql = "select count(*) as cnt from EmailBounceList where name like" + DbController.encodeSqlTextLike(email.toAddress) + " and transient=0";
                    if (cs.OpenSQL(sql))
                    {
                        if (!cs.GetInteger("cnt").Equals(0))
                        {
                            reasonForFail = "Recipient email address is on the email block list";
                            return(false);
                        }
                    }
                }
                //
                // -- send email
                Body messageBody = new Body {
                };
                if (!string.IsNullOrEmpty(email.htmlBody))
                {
                    messageBody.Html = new Content {
                        Charset = "UTF-8", Data = email.htmlBody
                    };
                }
                if (!string.IsNullOrEmpty(email.textBody))
                {
                    messageBody.Text = new Content {
                        Charset = "UTF-8", Data = email.textBody
                    };
                }
                var sendRequest = new SendEmailRequest {
                    Source      = email.fromAddress,
                    Destination = new Destination {
                        ToAddresses = new List <string> {
                            email.toAddress
                        }
                    },
                    Message = new Message {
                        Subject = new Content(email.subject),
                        Body    = messageBody
                    }
                };
                try {
                    LogController.logInfo(core, "Sending SES email" + logShortDetail);
                    var response = client.SendEmail(sendRequest);
                    return(true);
                } catch (Exception ex) {
                    reasonForFail = "Error sending email [" + ex.Message + "]" + logShortDetail;
                    LogController.logError(core, "Unexpected exception during SES send" + logLongDetail + ", exception [" + ex + "]");
                    return(false);
                }
            } catch (Exception ex) {
                reasonForFail = "Error sending email [" + ex.Message + "]" + logShortDetail;
                LogController.logError(core, "Unexpected exception during SES configure" + logLongDetail + ", exception [" + ex + "]");
                return(false);
            }
        }
Beispiel #2
0
        //
        //====================================================================================================
        /// <summary>
        /// Send email by SMTP. return 'ok' if success, else return a user compatible error message
        /// </summary>
        public static bool send(CoreController core, EmailSendDomainModel email, ref string returnErrorMessage)
        {
            bool status = false;

            returnErrorMessage = "";
            try {
                string        smtpServer    = core.siteProperties.getText("SMTPServer", "127.0.0.1");
                SmtpClient    client        = new SmtpClient(smtpServer);
                MailMessage   mailMessage   = new MailMessage();
                MailAddress   fromAddresses = new MailAddress(email.fromAddress.Trim());
                ContentType   mimeType      = null;
                AlternateView alternate     = null;
                //
                mailMessage.From = fromAddresses;
                mailMessage.To.Add(new MailAddress(email.toAddress.Trim()));
                mailMessage.Subject          = email.subject;
                client.EnableSsl             = false;
                client.UseDefaultCredentials = false;
                //
                if ((string.IsNullOrEmpty(email.textBody)) && (!string.IsNullOrEmpty(email.htmlBody)))
                {
                    //
                    // html only
                    mailMessage.Body       = email.htmlBody;
                    mailMessage.IsBodyHtml = true;
                }
                else if ((!string.IsNullOrEmpty(email.textBody)) && (string.IsNullOrEmpty(email.htmlBody)))
                {
                    //
                    // text body only
                    mailMessage.Body       = email.textBody;
                    mailMessage.IsBodyHtml = false;
                }
                else
                {
                    //
                    // both html and text
                    mailMessage.Body       = email.textBody;
                    mailMessage.IsBodyHtml = false;
                    mimeType  = new System.Net.Mime.ContentType("text/html");
                    alternate = AlternateView.CreateAlternateViewFromString(email.htmlBody, mimeType);
                    mailMessage.AlternateViews.Add(alternate);
                }
                if (core.mockEmail)
                {
                    //
                    // -- for unit tests, mock interface by adding email to core.mockSmptList
                    core.mockEmailList.Add(new MockEmailClass {
                        email = email
                    });
                    status = true;
                }
                else
                {
                    //
                    // -- send email
                    try {
                        LogController.logInfo(core, "sendSmtp, to [" + email.toAddress + "], from [" + email.fromAddress + "], subject [" + email.subject + "], BounceAddress [" + email.bounceAddress + "], replyTo [" + email.replyToAddress + "]");
                        client.Send(mailMessage);
                        status = true;
                    } catch (Exception ex) {
                        returnErrorMessage = "There was an error sending email [" + ex + "]";
                        LogController.logError(core, returnErrorMessage);
                    }
                }
            } catch (Exception ex) {
                LogController.logError(core, "There was an error configuring smtp server ex [" + ex + "]");
                throw;
            }
            return(status);
        }