This is a C# source code that generates a signed, encrypted AS2 message with a request for an MDN. The complete program
can be downloaded at EDIINT AS2.
A detailed explanation of the algorithm can be read at "Creating
an AS2 client in stages".
private void btnEncryptSignedMdn_Click(object sender, EventArgs e) { ediDocument oEdiDoc = null; mailDocument oMailDoc = null; mailMessage oMailSubject = null; bool bSyncronous = true; string sPath = AppDomain.CurrentDomain.BaseDirectory; oEdiDoc = new ediDocument(); //object for AS2 file oMailDoc = oEdiDoc.GetMailDocument(); //Setting so that syntax for the field "Disposition-Notification-To" is not validated, but treated as a free form text field oMailDoc.AddDefinedHeader("Disposition-Notification-To", MailMessageHeaderTypeConstants.HeaderType_FieldText, RequirementTypeConstants.Requirement_Optional); //oject for subject/content in AS2 file oMailSubject = oMailDoc.GetMessageContent(); //add appropriate header to describe the content // - "Application/EDI-X12" for EDI X12 // - "Application/EDIFACT" for EDI UN/EDIFACT // - "Application/XML" for XML oMailSubject.set_HeaderFieldValue("Content-Type", "Application/EDI-X12"); //the content to be sent in this AS2 message is an EDI X12 file. The file is imported into the message object. oMailSubject.Import(sPath + "ediFile.X12"); // Configure the security of this message. ediSecurity oSecurity = oMailSubject.GetSecurity(); // Specify the type of encryption and signing algorithm to use oSecurity.EncryptionAlgorithm = "3DES"; oSecurity.AssuranceAlgorithm = "SHA-1"; // Your trading partner's public key certificate is required to encrypt the message. // Recipient's certificate to encrypt message. oSecurity.SetCertSubjectNameByLocation("TradingPartner_Cert", "CurrentUser", "My", "Microsoft Strong Cryptographic Provider"); // Your private key certificate is required to sign (and decrypt) the message // Sender's certificate to sign message. oSecurity.SetCertSignerNameByLocation("MyCert", "CurrentUser", "My", "Microsoft Strong Cryptographic Provider"); oSecurity.EnableEncryption = true; //if signed, enable assurance and calculate MIC oSecurity.EnableAssurance = true; txtMIC.Text = oMailSubject.GenerateDigest(MailMessagePartTypeConstants.Message_All, EncodingMechanismTypeConstants.EncodeType_Base64); txtMIC.Refresh(); Cursor = Cursors.WaitCursor; //Put all things together if (oMailSubject.Prepare() != 1) { MessageBox.Show("Failed to prepare mail subject"); } else { //These headers were moved after the Prepare method so that they don't get included in the encrypted message. // Put globally unique ID as Message-ID. oMailSubject.set_HeaderFieldValue("Message-ID", "1234567890@evalusercompany.com"); //txtMessageID.Text // Put AS2 version. oMailSubject.set_HeaderFieldValue("AS2-Version", "1.0"); // Put AS2-To header value. oMailSubject.set_HeaderFieldValue("AS2-To", "ToCompanyABC"); // Put AS2-From header value. oMailSubject.set_HeaderFieldValue("AS2-From", "FromCompanyXYZ"); // MDN can be requested to be sent by the following ways: // - Synchronously - use the same connection to receive the MDN that was used to send the AS2 message. // - Asynchronously - use a different connection to receive the MDN. if (!bSyncronous) // asyschronous { // For asynchronous MDN, must set a syntactically correct URI at 'Receipt-delivery-option'. oMailSubject.set_HeaderFieldValue("Receipt-delivery-option", "http://domain.com:9999/testsite/AsyncMDN/"); } else { // Request MDN Acknowledgment by adding "Disposition-Notification-To" header. // Value must be present when requesting for MDN, but holds no meaning in an AS2 environment. oMailSubject.set_HeaderFieldValue("Disposition-Notification-To", "<evaluser@evalusercompany.com>"); } //This is the file that you would send by http or https oMailDoc.Save(sPath + @"\MailFolder\AS2EncryptedSignedMdnRequest.bin"); TransmitFileByHttp(ref oMailDoc, "AS2EncryptedSignedMdnRequest.bin"); if (bSyncronous) // MDN syschronous { mailMessage oMDN = oMailDoc.GetMDN(); // Gets the MDN that was received syncronously oMDN.Save(sPath + @"MailFolder\Received_MDN.bin"); // Saves the MDN to a file ReadMdnMessage(ref oMDN); oMDN.Dispose(); } else // MDN asyschronous { mailMessage oMDN = new mailMessage(); oMDN.Import(sPath + @"MailFolder\Received_MDN.bin"); //This is the MDN received asyncronously so it needs to be loaded into the oMDN object. ReadMdnMessage(ref oMDN); oMDN.Dispose(); } // if (bSyncronous) } // if (oMailSubject.Prepare()) oSecurity.Dispose(); oMailSubject.Dispose(); oMailDoc.Dispose(); oEdiDoc.Dispose(); Cursor = Cursors.Default; MessageBox.Show("Done"); }