Kentico自定义电子邮件提供商 - 阻止发送电子邮件

时间:2018-02-26 20:57:23

标签: asp.net .net email kentico

致力于自定义电子邮件提供商,在某些情况下,我会阻止发送电子邮件。

注意:当前通过Kentico电子邮件队列发送的所有电子邮件都需要保持这种状态。

我尝试删除/存档/停止发送电子邮件的所有选项都会导致电子邮件停留在Kentioc电子邮件队列中并发送或等待状态。

这是我正在尝试的代码,请参阅“ if(stopEmail){..}

中的选项
 protected override void SendEmailAsyncInternal(string siteName, MailMessage message, SMTPServerInfo smtpServer, EmailToken emailToken)
{

    string contentAll = "";
    bool stopEmail = false;

    // read email content
    try
    {
        if (message.AlternateViews.Count > 0)
        {

            for (int i = 0; i < message.AlternateViews.Count; i++)
            {

                var stream = message.AlternateViews[i].ContentStream;
                if (stream.CanRead)
                {
                    using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8, false, 1000, true)) 
                    {
                        contentAll += reader.ReadToEnd();
                    }
                }

            }


        }
    }
    catch (Exception ex)
    {
        EventLogProvider.LogInformation("CMSCustom baned emails", "EMAIL SENDOUT", ex.Message + ", <br /> email content:<br /> " + contentAll);
    }


    // check if BANNED dmoains are in email
    string bannedEmails = "@somedomain.com";

    if (contentAll.Contains(bannedEmails))
    {
        stopEmail = true;

    }

    // if banned email found, stop email from being sent
    if (stopEmail)
    {
        //
        var m = new EmailMessage(message);
        var mi = EmailInfoProvider.SetEmailInfo(m, SiteContext.CurrentSiteID);
        //opt 4  - Stuck in email queue - sending/waiting
        //SmtpClient client = new SmtpClient(smtpServer.ServerName);
        //client.SendAsyncCancel();


        // opt 3 - Stuck in email queue - sending/waiting
        //EmailHelper.Queue.CancelSending();

        // opt 2 - Stuck in email queue - sending/waiting
       // EmailHelper.Queue.Delete(mi.EmailID);


        //opt 1 - Stuck in email queue - sending/waiting
        //mi.EmailStatus = EmailStatusEnum.Archived;
        //mi.Update();

        return;
    }



    base.SendEmailAsyncInternal(siteName, message, smtpServer, emailToken);




}

我知道如何成功删除/删除特定电子邮件吗?

谢谢):

2 个答案:

答案 0 :(得分:0)

请尝试以下行:

EmailInfoProvider.DeleteEmailInfo(mi.EmailID);

使用EmailHelper.Queue.CancelSending()会产生更广泛的影响并影响当前的邮件发送,而不仅仅是当前的电子邮件。

查看Kentico管理页面中的代码,EmailHelper.Queue.Delete(mi.EmailID);应该可以工作,并最终调用相同的方法。您是否在事件日志中看到任何错误,告知您电子邮件被删除的原因?

答案 1 :(得分:0)

要将电子邮件移至“已发送电子邮件”,您需要致电EmailProvider.OnSendCompleted(AsyncCompletedEventArgs)

我使用以下功能在Kentico 9中将电子邮件标记为已完成。电子邮件仍然显示在“已发送电子邮件”选项卡下,但从未真正发送过电子邮件。

private void CancelSend(EmailToken eToken)
{
    var completedEvent = new AsyncCompletedEventArgs(null, false, eToken);
    base.OnSendCompleted(completedEvent);
}

结合您的代码,我们得到:

// if banned email found, stop email from being sent
if (stopEmail)
{
    CancelSend(emailToken);
    return;
}