using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace MailMerge { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //MailMerge // setup EMsgAPI object - EM MMCoverSheetCreate2011.MMCoverSheetCreateService EM = new MMCoverSheetCreate2011.MMCoverSheetCreateService(); // setup Authentication objects MMCoverSheetCreate2011.RequestAuthentication reqAuth = new MMCoverSheetCreate2011.RequestAuthentication(); MMCoverSheetCreate2011.XDDSAuthType XDDSAuth = new MMCoverSheetCreate2011.XDDSAuthType(); MMCoverSheetCreate2011.UIDType uid = new MMCoverSheetCreate2011.UIDType(); // init Authentication objects XDDSAuth.Password = "password"; uid.Value = "userid"; XDDSAuth.RequesterID = uid; reqAuth.Item = XDDSAuth; MMCoverSheetCreate2011.Request Request = new MMCoverSheetCreate2011.Request(); MMCoverSheetCreate2011.Response Response = new MMCoverSheetCreate2011.Response(); MMCoverSheetCreate2011.MMCoverSheetCreateRequest Submit_Request = new MMCoverSheetCreate2011.MMCoverSheetCreateRequest(); MMCoverSheetCreate2011.MMCoverSheetCreateResult Submit_Result = new MMCoverSheetCreate2011.MMCoverSheetCreateResult(); // Setup Documents MMCoverSheetCreate2011.DocumentType Doc = new MMCoverSheetCreate2011.DocumentType(); MMCoverSheetCreate2011.DocDataType DocData = new MMCoverSheetCreate2011.DocDataType(); string File; File = ReadAndEncodedFile("SAMPLECOVER.doc", true); DocData.format = MMCoverSheetCreate2011.DocEncodingFormat.base64; DocData.Value = File; Doc.@ref = "MSW"; Doc.DocType = "MSW"; Doc.Filename = "MyCover"; Doc.Item = DocData; Doc.ItemElementName = MMCoverSheetCreate2011.ItemChoiceType.DocData; DocData = null; // Setup Proxy server and EMsgAPIConnect URL for posting data Set_Server_and_Proxy(EM); // Setup Reqeust Object // Setup Reqeust Object Request.ReceiverKey = EM.Url; Request.Authentication = reqAuth; // Assign Reqeust and Response objects to EM Object EM.RequestValue = Request; EM.ResponseValue = Response; Submit_Request.SubmitId = "TomCoverPageTest"; Submit_Request.MainDocument = Doc; Submit_Request.DisplayName = "MyCoverMM"; Submit_Request.ReplaceExisting = false; Submit_Request.ObjectStore = "XDDS"; MMCoverSheetCreate2011.StoredObjectIdType mmcsid = new MMCoverSheetCreate2011.StoredObjectIdType(); mmcsid.ownership = MMCoverSheetCreate2011.StoredObjectLevelType.customer; mmcsid.ownershipSpecified = true; mmcsid.objKind = "mmcover"; mmcsid.Value = "MyCoverMM"; Submit_Request.MMCoverSheetId = mmcsid; Submit_Request.ReplaceExisting = false; Submit_Request.MappingField = new string[5]; Submit_Request.MappingField[0] = "TO"; Submit_Request.MappingField[1] = "PHONE"; Submit_Request.MappingField[2] = "FROM"; Submit_Request.MappingField[3] = "DOC_PAGES"; Submit_Request.MappingField[4] = "ATTN"; try { Submit_Result = EM.MMCoverSheetCreate(Submit_Request); } catch (Exception ex) { System.Console.WriteLine(ex.Message); System.Console.Read(); return; } if (Submit_Result != null) { if (Submit_Result.Status.StatusCode != "0") { MessageBox.Show(Submit_Result.Status.StatusMessage, "Error"); } } string emfilename = "emresponse.xml"; string resfilename = "result.xml"; string reqfilename = "request.xml"; try { //This will output the response, result and request(if uncommented) to XML files in the //installed directory. This can be very usefull for debugging or ir help is requested from //the EMAPI support team serializeResult(emfilename, EM.ResponseValue); serializeResult(resfilename, Submit_Result); serializeResult(reqfilename, Submit_Request); } catch (IOException io_error) { MessageBox.Show("Error - " + io_error, "Error", MessageBoxButtons.OK); } } private string ReadAndEncodedFile(string FullFileName) { string return_value; return_value = ReadAndEncodedFile(FullFileName, true); return return_value; } private string ReadAndEncodedFile(string FullFileName, bool bBase64Encode) { // read the file System.IO.FileStream inFile; byte[] binaryData; try { inFile = new System.IO.FileStream(FullFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); binaryData = new byte[inFile.Length]; long bytesRead = inFile.Read(binaryData, 0, System.Convert.ToInt32(inFile.Length)); inFile.Close(); } catch (System.Exception exp) { // Error creating stream or reading from it. System.Console.WriteLine("{0}", exp.Message); return "FAIL"; } string base64String; if (bBase64Encode) { // Convert the binary input into Base64 UUEncoded output. try { base64String = System.Convert.ToBase64String(binaryData, 0); } catch (System.ArgumentNullException) { System.Console.WriteLine("Binary data array is null."); return "FAIL"; } // done Encoding file } else { base64String = binaryData.ToString(); } return base64String; } private void Set_Server_and_Proxy(System.Web.Services.Protocols.SoapHttpClientProtocol EM) { EM.Url = "https://messaging.easylink.com/soap/sync"; //the EasyLink URL to submit to. } //used to extract XML static void serializeResult(String filename, System.Object res) { System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(res.GetType()); System.IO.TextWriter w = new System.IO.StreamWriter(filename); x.Serialize(w, res); w.Close(); } } }