Im coming from this thread: http://www.thescripts.com/forum/thread117164.html
Cant reply there, so im making a new one, also the last post is very old.
Im trying to sing an email message.
Ive tryed with System.net.mail, and i can do it, but it dont has support for attachments (that i know of).
So i went to CDO.
After checking a lot in the net, come up with that post, and try it.
It does send the mail, and when i open it with Evolution Mail (Linux) the signature is recognized and it says valid signature.
But when i send the mail, and open it with Outlook, it does recognize the signature, but it says the mail has been altered (and it dont)
Also a "funny" thing, the signing date for that mail, in details, is somewhere in 1601...
Do my only gess is that the signature is bad.
if anyone can help, i really apreciate.
Regards
Following is the code im usign:
Expand|Select|Wrap|Line Numbers
- using System.Collections.Generic;
- using System;
- using System.IO;
- using System.Text;
- using System.Net.Mail;
- using System.Security.Cryptography.Pkcs;
- using System.Security.Cryptography.X509Certificates;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Security.Cryptography;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- /* ADD REFERENCES TO
- * ADOBD
- * Interop.CDO
- *
- */
- namespace EmailTesteCsharp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Program x = new Program();
- x.mySendMail("askme@ask.pt", "askme@ask.pt", "askme@ask.pt", "Novo mail", "body do email");
- }
- private void mySendMail(string to, string from, string cc, string subject,string body)
- {
- CDO.Message oSignedMsg = new CDO.Message();
- oSignedMsg.From = "askme@ask.pt";
- oSignedMsg.To = to;
- if(cc != null && cc.Trim() != "")
- {
- oSignedMsg.CC = cc;
- }
- oSignedMsg.Subject = subject;
- oSignedMsg.MimeFormatted = true;
- oSignedMsg.BodyPart.ContentMediaType = "multipart/signed;\rprotocol=" +
- '\u0022' + "application/x-pkcs7-signature" + '\u0022' + ";\rmicalg=SHA1;";
- oSignedMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
- oSignedMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "10.0.0.2";
- oSignedMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 25;
- oSignedMsg.Configuration.Fields.Update();
- CDO.IBodyPart oBodyPart = oSignedMsg.BodyPart.AddBodyPart(oSignedMsg.BodyPart.BodyParts.Count + 1);
- oBodyPart.ContentMediaType = "multipart/mixed";
- CDO.IBodyPart oBodyPart2 = oBodyPart.AddBodyPart(oBodyPart.BodyParts.Count + 1);
- oBodyPart2.ContentMediaType = "text/plain;charset=\"iso-8859-1\"";
- ADODB.Stream oStream = oBodyPart2.GetDecodedContentStream();
- oStream.WriteText(body, 0);
- oStream.Flush();
- oStream.Close();
- CDO.IBodyPart oSigniturePart = oSignedMsg.BodyPart.AddBodyPart(oSignedMsg.BodyPart.BodyParts.Count + 1);
- oSigniturePart.ContentMediaType = "application/x-pkcs7-signature";
- oSigniturePart.ContentTransferEncoding = "base64";
- oSigniturePart.Fields["urn:schemas:mailheader:content-disposition"].Value
- = "attachment;\rFileName=" + '\u0022' + "smime.p7s" + '\u0022' + "";
- oSigniturePart.Fields.Update();
- oStream = oSignedMsg.GetStream();
- string messageToSign = oStream.ReadText(oStream.Size);
- byte[] byteSignature = signMessage1(messageToSign);
- oStream = oSigniturePart.GetDecodedContentStream();
- oStream.Type = ADODB.StreamTypeEnum.adTypeBinary;
- oStream.Write(byteSignature);
- oStream.Flush();
- oStream.Close();
- oSignedMsg.Send();
- }
- public byte[] signMessage1(string msg)
- {
- X509Certificate2 signerCert = new X509Certificate2("MyPfxFile.pfx", "MyPfxPassword");
- byte[] msgBytes = Encoding.Unicode.GetBytes(msg);
- byte[] encodedSignedCms = SignMsg1(msgBytes, signerCert);
- return encodedSignedCms;
- }
- static public byte[] SignMsg1(Byte[] msg, X509Certificate2 signerCert)
- {
- // Place message in a ContentInfo object.
- // This is required to build a SignedCms object.
- ContentInfo contentInfo = new ContentInfo(msg);
- // Instantiate SignedCms object with the ContentInfo above.
- // Has default SubjectIdentifierType IssuerAndSerialNumber.
- // Has default Detached property value false, so message is
- // included in the encoded SignedCms.
- SignedCms signedCms = new SignedCms(contentInfo);
- // Formulate a CmsSigner object for the signer.
- CmsSigner cmsSigner = new CmsSigner(signerCert);
- // Sign the CMS/PKCS #7 message.
- //Console.Write("Computing signature with signer subject " + "name {0} ... ", signerCert.SubjectName.Name);
- signedCms.ComputeSignature(cmsSigner);
- //Console.WriteLine("Done.");
- // Encode the CMS/PKCS #7 message.
- return signedCms.Encode();
- }
- }
- }