It was a request of one of my client to send the mail as encrypted so that this kept secure while sending to customers. So after a search I found Jason Niver's code to be very handy to use. So I am sharing this will all so next time it becomes little bit easy to get.
position += chunkSize;
Now its time to install the certificate, for this go with the bellow codes.
Sources:
1. Sending encrypted mail
2. Using certificates from the Windows certificate store
using System;
using System.Text;
using System.Net.Mail;
using System.IO;
using
System.Security.Cryptography.Pkcs;
using
System.Security.Cryptography.X509Certificates;
namespace CommonUtilities
{
//requires reference to System.Security
class EmailUtil
{
public static void SendEncryptedEmail(string[] to, string from, string subject, string body, string[] attachments)
{
MailMessage message = new MailMessage();
message.From = new MailAddress(from);
message.Subject = subject;
if (attachments != null &&
attachments.Length > 0)
{
StringBuilder buffer = new StringBuilder();
buffer.Append("MIME-Version:
1.0\r\n");
buffer.Append("Content-Type:
multipart/mixed; boundary=unique-boundary-1\r\n");
buffer.Append("\r\n");
buffer.Append("This is a
multi-part message in MIME format.\r\n");
buffer.Append("--unique-boundary-1\r\n");
buffer.Append("Content-Type:
text/plain\r\n"); //could use text/html
as well here if you want a html message
buffer.Append("Content-Transfer-Encoding:
7Bit\r\n\r\n");
buffer.Append(body);
if (!body.EndsWith("\r\n"))
buffer.Append("\r\n");
buffer.Append("\r\n\r\n");
foreach (string filename in attachments)
{
FileInfo fileInfo = new FileInfo(filename);
buffer.Append("--unique-boundary-1\r\n");
buffer.Append("Content-Type:
application/octet-stream; file=" + fileInfo.Name + "\r\n");
buffer.Append("Content-Transfer-Encoding:
base64\r\n");
buffer.Append("Content-Disposition:
attachment; filename=" + fileInfo.Name + "\r\n");
buffer.Append("\r\n");
byte[] binaryData = File.ReadAllBytes(filename);
string base64Value = Convert.ToBase64String(binaryData,
0, binaryData.Length);
int position = 0;
while (position <
base64Value.Length)
{
int chunkSize = 100;
if (base64Value.Length -
(position + chunkSize) < 0)
chunkSize =
base64Value.Length - position;
buffer.Append(base64Value.Substring(position, chunkSize));
buffer.Append("\r\n");
}
buffer.Append("\r\n");
}
body = buffer.ToString();
}
else
{
body = "Content-Type:
text/plain\r\nContent-Transfer-Encoding: 7Bit\r\n\r\n" + body;
}
byte[] messageData = Encoding.ASCII.GetBytes(body);
ContentInfo content = new
ContentInfo(messageData);
EnvelopedCms envelopedCms = new
EnvelopedCms(content);
CmsRecipientCollection toCollection
= new
CmsRecipientCollection();
foreach (string address in to)
{
message.To.Add(new
MailAddress(address));
X509Certificate2 certificate = null; //Need to load from
store or from file the client's cert
CmsRecipient recipient = new
CmsRecipient(SubjectIdentifierType.SubjectKeyIdentifier, certificate);
toCollection.Add(recipient);
}
envelopedCms.Encrypt(toCollection);
byte[] encryptedBytes = envelopedCms.Encode();
//add digital signature:
SignedCms signedCms = new SignedCms(new
ContentInfo(encryptedBytes));
X509Certificate2 signerCertificate
= null; //Need to load from
store or from file the signer's cert
CmsSigner signer = new
CmsSigner(SubjectIdentifierType.SubjectKeyIdentifier, signerCertificate);
signedCms.ComputeSignature(signer);
encryptedBytes =
signedCms.Encode();
//end digital signature section
MemoryStream stream = new MemoryStream(encryptedBytes);
AlternateView view = new AlternateView(stream,
"application/pkcs7-mime;
smime-type=signed-data;name=smime.p7m");
message.AlternateViews.Add(view);
SmtpClient client = new SmtpClient("your.smtp.mailhost");
//add authentication info if required by your
smtp server etc...
//client.Credentials =
CredentialCache.DefaultCredentials;
client.Send(message);
}
}
}
Now its time to install the certificate, for this go with the bellow codes.
using
System.Security.Cryptography.X509Certificates;
private X509Certificate
GetCertificate()
{
X509Store store = new
X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs =
store.Certificates.Find(X509FindType.FindBySerialNumber, “123456”, true);
store.Close();
return certs[0];
}
Sources:
1. Sending encrypted mail
2. Using certificates from the Windows certificate store
All the points you described so beautiful. Every time i read your i blog and i am so surprised that how you can write so well.
ReplyDeleteBest Dot Net Training Institute in Chennai
Best Software Testing Training Institute in Chennai With Placement
Java Certification in Chennai
PHP Training Institute in Chennai
IEEE Final Year Project centers make amazing deep learning final year projects ideas for final year students Final Year Projects for CSE to training and develop their deep learning experience and talents.
DeleteIEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation.
corporate training in chennai corporate training in chennai
corporate training companies in india corporate training companies in india
corporate training companies in chennai corporate training companies in chennai
I have read your blog its very attractive and impressive. I like it your blog. Digital Marketing Company in Chennai Project Centers in Chennai
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteTo select the best bulk SMS API available in the market I would suggest you go for Msgclub SMS platform where they have their bulk SMS API available in many programming languages like PHP, Java, .Net and more
ReplyDeleteNice information, valuable and excellent in Job, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.
ReplyDeletemobile phone repair in Canton
iphone repair in Canton
cell phone repair in Canton
tablet repair in Canton
ipad repair in Canton
phone repair in Canton
mobile phone repair canton
iphone repair canton
cell phone repair canton
phone repair canton
Thanks for taking the time to discuss this, I feel strongly about it and adore finding out additional on this topic.
ReplyDeleteIf feasible, as you acquire expertise, would you thoughts updating your blog with much more details? It is extremely useful for me.
buy bitcoin instant
Thanks for sharing this information.
ReplyDeleteYes Done is the best online site for on demand home services, professional service providers in Jaipur, Rajasthan. We are providing top services like carpenter, cleaning, TV, best pest control services in Jaipur, ro repair service in Jaipur, ac repair service in Jaipur online by Yes Done is there for services at your doorstep in Jaipur, Rajasthan. For more service visit our website. For more services visit our website.