Custom Email Provider with Encryption

Simon Miles asked on October 29, 2015 12:08


I am using a Custom Email Provider to send encrypted emails from our website. It works OK for the first couple of emails, however it will then throw an Error with the Event Code RUN, the description is below:

Message: Stream was not readable.

Exception type: System.ArgumentException
Stack Trace: 
at System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean leaveOpen)
at System.IO.StreamReader..ctor(Stream stream, Boolean detectEncodingFromByteOrderMarks)
at CustomEmailProvider.SendEmailAsyncInternal(String siteName, MailMessage message, SMTPServerInfo smtpServer, EmailToken emailToken)
at CMS.EmailEngine.EmailProvider.SendEmailAsync(String siteName, MailMessage message, SMTPServerInfo smtpServer, EmailToken emailToken)
at CMS.EmailEngine.ThreadSender.ProcessQueue()
at CMS.EmailEngine.ThreadSender.SendCompleted(Object sender, AsyncCompletedEventArgs e)
at CustomEmailProvider.OnSendCompleted(AsyncCompletedEventArgs e)
at CMS.Base.CMSThread.RunThread()
at CMS.Base.CMSThread.Run()

The function is below:

protected override void SendEmailAsyncInternal(string siteName, MailMessage message, SMTPServerInfo smtpServer, EmailToken emailToken)

        string CertificatePath = "path_to_certificate";

        X509Certificate2 EncryptCert =  new X509Certificate2(CertificatePath);

        string emailContent;
        var stream = message.AlternateViews[0].ContentStream;
        using (StreamReader reader = new StreamReader(stream))
        emailContent = reader.ReadToEnd();

        StringBuilder NewMessage = new StringBuilder();
        NewMessage.AppendLine("Content-Type: text/" +  "html" + "; charset=\"iso-8859-1\"");
        NewMessage.AppendLine("Content-Transfer-Encoding: 7bit");

        byte[] BodyBytes = Encoding.ASCII.GetBytes(NewMessage.ToString());

        EnvelopedCms Envelope = new EnvelopedCms(new ContentInfo(BodyBytes));
        CmsRecipient Recipient = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, EncryptCert);
        byte[] EncryptedBytes = Envelope.Encode();

        MemoryStream ms = new MemoryStream(EncryptedBytes);
        AlternateView av = new AlternateView(ms,"application/pkcs7-mime; smime-type=signed-data;name=smime.p7m");

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("support_email");
        string addresses = message.To.ToString();
        msg.To.Add(new MailAddress(addresses));
        msg.Subject = message.Subject;

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

        string detail = string.Format("E-mail from {0} was dispatched via {1} (asynchronously)", message.From.Address, smtpServer.ServerName);
        EventLogProvider.LogInformation("CMSCustom", "EMAIL SENDOUT", detail);

Once the error occurs it blocks the email queue and I need to reset the application to be able to delete the email from the queue. The error will then occur each time a new email is generated and block the email queue.

After a while it will start to work again, but there appears to be no correlation between clearing the system cache, resetting the application, or recycling the sites App Pool and this happening.

Does anyone have ideas that could help as I am stumped!?


Correct Answer

Simon Miles answered on October 29, 2015 13:48

Turns out it was an SMTP authentication problem, 2 invalid logins were allowed and emails subsequently sent, but then an autoban kicked in for an hour before it would accept any further connections, hence it stopping working. So problem solved!

1 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.