Monday, August 8, 2016

Converting MailMessage to Memory stream remove BCC

I had a problem using SendRawMessage with Amazon SES & not able to send BCC, later I figured out that the problem is in converting the mail message to memory stream.

So this is the detailed method required to convert mail message to memory stream and include BCC also. It for .Net 4.5


1 - In SendMessageUsingAWSProfileAsync(, Remove the desintation as it does not entertain BCC
                var sendRequest = new SendRawEmailRequest(rawMessage)
                {
                    Source = mailMessage.From.Address, // Destination is removed from here intenationally as it stop doing BCC
}
2 - In ConvertMailMessageToMemoryStream, Include BCC headers while creating the stream from mail message
// BCC is not included by default, so need to include it.
        if (message.Bcc.Count > 0)
        {
            MethodInfo writeHeadersMethod = mailWriter.GetType().GetMethod("WriteHeaders", BindingFlags.Instance | BindingFlags.NonPublic);
            System.Collections.Specialized.NameValueCollection bccHeaders = new System.Collections.Specialized.NameValueCollection();
            bccHeaders.Add("BCC", string.Join(",", message.Bcc.Select(b => b.Address)));
            writeHeadersMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { bccHeaders, false }, null);
        }

Wednesday, February 17, 2016

Add a trailing slash to incoming HTTP requests



In some questions you always want a backslash in your HTTP request, you can easily do that with IIS rewrite rules

First you need to identify if incoming request does not contain an ending slash, that can be done by the following regex. We are only focusing on a special incoming pattern  like api/a/b etc



if true redirect attach a slash with it.
 

Add some conditions so that there is no infinite loop


   
   
   
 

The entire code will look like following (Add the following code in your Web.Config  in System.WebServer)


     
           
 
 
   
   
   
 
 

     
   

IIS URL Rewrite Rules : Redirect HTTP requests to HTTPS



Many times we want to open up some pages to only HTTPS request, so in that case we need to redirect all non HTTPS incoming requests to HTTPS.

Add the following code in your Web.Config (The italic code is optional)