Ejemplo n.º 1
0
        public static void StartSendTestMail(String recipientEmail, String mailSubj, String mailBody)
        {
            var log         = LogManager.GetLogger("ASC.CRM.MailSender");
            var smtpSetting = Global.TenantSettings.SMTPServerSetting;
            var smtpClient  = new SmtpClient(smtpSetting.Host, smtpSetting.Port);

            if (smtpSetting.RequiredHostAuthentication)
            {
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials           = new NetworkCredential(smtpSetting.HostLogin, smtpSetting.HostPassword);
            }
            smtpClient.EnableSsl = smtpSetting.EnableSSL;

            ASC.Core.CoreContext.TenantManager.SetCurrentTenant(TenantProvider.CurrentTenantID);

            var from = new MailAddress(smtpSetting.SenderEmailAddress, smtpSetting.SenderDisplayName, Encoding.UTF8);


            if (!SendBatchEmailsOperation.isValidMail(recipientEmail))
            {
                throw new Exception(String.Format(CRMCommonResource.MailSender_InvalidEmail, recipientEmail));
            }

            var to      = new MailAddress(recipientEmail);
            var message = new MailMessage(from, to);

            try
            {
                message.Subject         = mailSubj;
                message.Body            = mailBody;
                message.SubjectEncoding = Encoding.UTF8;
                message.BodyEncoding    = Encoding.UTF8;
                message.IsBodyHtml      = false;
                smtpClient.Timeout      = 10000;//in ms

                log.Debug(SendBatchEmailsOperation.GetLoggerRow(message));

                ThreadPool.QueueUserWorkItem(_ =>
                {
                    try
                    {
                        smtpClient.Send(message);
                        message.Dispose();
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex.Message);
                    }
                });
            }
            catch (Exception ex)
            {
                message.Dispose();
                log.Error(ex.Message);
            }
        }