Skip to content

Commit 14e4c59

Browse files
committed
Added initial set of mail merge examples.
0 parents  commit 14e4c59

File tree

83 files changed

+1906
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1906
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.539
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Create-Envelopes-for-mailing", "Create-Envelopes-for-mailing\Create-Envelopes-for-mailing.csproj", "{E2EA3F53-C833-470F-B9FA-973E88BD963A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{E2EA3F53-C833-470F-B9FA-973E88BD963A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E2EA3F53-C833-470F-B9FA-973E88BD963A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E2EA3F53-C833-470F-B9FA-973E88BD963A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E2EA3F53-C833-470F-B9FA-973E88BD963A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {AEC106DA-724A-4C7B-B93C-560F32DF1F98}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
<RootNamespace>Create_Envelopes_for_mailing</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
7+
namespace Create_Envelopes_for_mailing
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
//Creates new Word document instance for Word processing
14+
using (WordDocument document = new WordDocument())
15+
{
16+
//Opens the Word template document
17+
Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Template.docx"));
18+
document.Open(docStream, FormatType.Docx);
19+
docStream.Dispose();
20+
21+
//Gets the recipient details as "IEnumerable" collection of .NET objects
22+
List<Recipient> recipients = GetRecipients();
23+
24+
//Performs the mail merge
25+
document.MailMerge.Execute(recipients);
26+
27+
//Saves the file in the given path
28+
docStream = File.Create(Path.GetFullPath(@"../../../Sample.docx"));
29+
document.Save(docStream, FormatType.Docx);
30+
docStream.Dispose();
31+
}
32+
}
33+
34+
#region Helper methods
35+
/// <summary>
36+
/// Gets the data to perform mail merge.
37+
/// </summary>
38+
/// <returns></returns>
39+
private static List<Recipient> GetRecipients()
40+
{
41+
List<Recipient> recipients = new List<Recipient>();
42+
//Initializes the recipient details
43+
recipients.Add(new Recipient("Nancy", "Davolio", "507 - 20th Ave. E.Apt. 2A", "Seattle", "WA", "98122", "USA"));
44+
recipients.Add(new Recipient("Andrew", "Fuller", "908 W. Capital Way", "Tacoma", "WA", "98401", "USA"));
45+
recipients.Add(new Recipient("Janet", "Leverling", "722 Moss Bay Blvd.", "Kirkland", "WA", "98033", "USA"));
46+
recipients.Add(new Recipient("Margaret", "Peacock", "4110 Old Redmond Rd.", "Redmond", "WA", "98052", "USA"));
47+
recipients.Add(new Recipient("Steven", "Buchanan", "14 Garrett Hil", "London", "", "SW1 8JR", "UK"));
48+
49+
return recipients;
50+
}
51+
#endregion
52+
53+
}
54+
55+
#region Helper class
56+
/// <summary>
57+
/// Represents the Recipient details.
58+
/// </summary>
59+
class Recipient
60+
{
61+
#region Properties
62+
public string FirstName { get; set; }
63+
public string LastName { get; set; }
64+
public string Address { get; set; }
65+
public string City { get; set; }
66+
public string State { get; set; }
67+
public string ZipCode { get; set; }
68+
public string Country { get; set; }
69+
#endregion
70+
71+
#region Constructor
72+
public Recipient(string firstName, string lastName, string address, string city, string state, string zipCode, string country)
73+
{
74+
FirstName = firstName;
75+
LastName = lastName;
76+
Address = address;
77+
City = city;
78+
State = state;
79+
ZipCode = zipCode;
80+
Country = country;
81+
}
82+
#endregion
83+
}
84+
#endregion
85+
}
Loading
Loading
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Create envelopes for mailings
2+
3+
This example illustrates how to create envelopes for mailing to a list of recipients using the [Execute(IEnumerable dataSource)](https://help.syncfusion.com/cr/cref_files/file-formats/Syncfusion.DocIO.Base~Syncfusion.DocIO.DLS.MailMerge~Execute(IEnumerable).html) API.
4+
5+
# How to run the project
6+
7+
1. Download this project to a location in your disk.
8+
9+
2. Open the solution file using Visual Studio.
10+
11+
3. Rebuild the solution to install the required NuGet packages.
12+
13+
4. Run the application.
14+
15+
# Screenshots
16+
17+
By running this application, you will get the envelopes for mailing as follows.
18+
19+
<p align="center">
20+
<img src="Images/Envelopes-for-mailing-output.png" alt="Envelopes-for-mailing-output"/>
21+
</p>
22+
23+
To generate envelopes, design your Word document template with the required layout, formatting, graphics, and merge fields to personalize information using Microsoft Word as follows.
24+
25+
<p align="center">
26+
<img src="Images/Envelopes-for-mailing-template.png" alt="Envelopes-for-mailing-template"/>
27+
</p>
28+
29+
Take a moment to peruse the [documentation](https://help.syncfusion.com/file-formats/docio/getting-started), where you will find other Word document processing operations along with features like [mail merge](https://help.syncfusion.com/file-formats/docio/working-with-mailmerge), [merge](https://help.syncfusion.com/file-formats/docio/working-with-word-document#merging-word-documents), and split documents, [find and replace](https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace) text in the Word document, [protect](https://help.syncfusion.com/file-formats/docio/working-with-security) Word documents, and most importantly [PDF](https://help.syncfusion.com/file-formats/docio/word-to-pdf) and [image](https://help.syncfusion.com/file-formats/docio/word-to-image) conversions with code examples.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27703.2000
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Create-and-send-email-messages", "Create-and-send-email-messages\Create-and-send-email-messages.csproj", "{2071F614-2A3E-420E-B70B-433038A1B3DA}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{2071F614-2A3E-420E-B70B-433038A1B3DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{2071F614-2A3E-420E-B70B-433038A1B3DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{2071F614-2A3E-420E-B70B-433038A1B3DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{2071F614-2A3E-420E-B70B-433038A1B3DA}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {861D988A-5C1D-4BFE-A1BA-BA5FAC2A5622}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
<PackageReference Include="System.Net.Http" Version="4.3.4" />
12+
</ItemGroup>
13+
14+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"Customers": [
3+
{
4+
"CustomerName": "Paul Henriot",
5+
"Address": "59 rue de l'Abbaye",
6+
"City": "Reims",
7+
"PostalCode": "51100",
8+
"Country": "France",
9+
"Phone": "26.47.15.10",
10+
"OrderID": "10295",
11+
"OrderDate": "09/02/2018",
12+
"ExpectedDeliveryDate": "09/30/2018",
13+
"ShippedDate": "09/10/2018"
14+
},
15+
{
16+
"CustomerName": "Maria Anders",
17+
"Address": "Obere Str. 57",
18+
"City": "Berlin",
19+
"PostalCode": "12209",
20+
"Country": "Germany",
21+
"Phone": "030-0074321",
22+
"OrderID": "10702",
23+
"OrderDate": "10/13/2018",
24+
"ExpectedDeliveryDate": "11/24/2018",
25+
"ShippedDate": "10/21/2018"
26+
},
27+
{
28+
"CustomerName": "Pedro Afonso",
29+
"Address": "Av. dos Lusíadas, 23",
30+
"City": "São Paulo",
31+
"PostalCode": "05432-043",
32+
"Country": "Brazil",
33+
"Phone": "(11) 555-7647",
34+
"OrderID": "10969",
35+
"OrderDate": "03/23/2019",
36+
"ExpectedDeliveryDate": "04/20/2019",
37+
"ShippedDate": "03/30/2019"
38+
},
39+
{
40+
"CustomerName": "Alexander Feuer",
41+
"Address": "Heerstr. 22",
42+
"City": "Leipzig",
43+
"PostalCode": "04179",
44+
"Country": "Germany",
45+
"Phone": "0342-023176",
46+
"OrderID": "10277",
47+
"OrderDate": "08/09/2018",
48+
"ExpectedDeliveryDate": "09/06/2018",
49+
"ShippedDate": "08/13/2018"
50+
},
51+
{
52+
"CustomerName": "Sergio Gutiérrez",
53+
"Address": "Av. del Libertador 900",
54+
"City": "Buenos Aires",
55+
"PostalCode": "1010",
56+
"Country": "Argentina",
57+
"Phone": "(1) 123-5555",
58+
"OrderID": "10916",
59+
"OrderDate": "02/27/2019",
60+
"ExpectedDeliveryDate": "03/27/2019",
61+
"ShippedDate": "03/09/2019"
62+
}
63+
]
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using Newtonsoft.Json.Linq;
2+
using Syncfusion.DocIO;
3+
using Syncfusion.DocIO.DLS;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Net.Mail;
7+
8+
namespace Create_and_send_email_messages
9+
{
10+
class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
//Creates new Word document instance for Word processing
15+
using (WordDocument template = new WordDocument())
16+
{
17+
//Opens the Word template document
18+
Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Template.docx"));
19+
template.Open(docStream, FormatType.Docx);
20+
docStream.Dispose();
21+
22+
//Gets the recipient details as DataTable
23+
List<object> recipients = GetRecipients();
24+
foreach (var dataRecord in recipients)
25+
{
26+
//Clones the template document for creating new document for each record in the data source
27+
WordDocument document = template.Clone();
28+
29+
//Performs the mail merge
30+
document.MailMerge.Execute(new List<object>() { dataRecord as Dictionary<string, object> });
31+
32+
//Save the HTML file as string
33+
docStream = new MemoryStream();
34+
document.SaveOptions.HtmlExportOmitXmlDeclaration = true;
35+
document.Save(docStream, FormatType.Html);
36+
//Releases the resources occupied by WordDocument instance
37+
document.Dispose();
38+
docStream.Position = 0;
39+
StreamReader reader = new StreamReader(docStream);
40+
string mailBody = reader.ReadToEnd();
41+
docStream.Dispose();
42+
if (mailBody.StartsWith("<!DOCTYPE"))
43+
mailBody = mailBody.Remove(0, 97);
44+
//Sends the email message
45+
//Update the required e-mail id here
46+
SendEMail("[email protected]", "[email protected]", "You order #" + (dataRecord as Dictionary<string, object>)["OrderID"].ToString() + " has been shipped", mailBody);
47+
}
48+
}
49+
}
50+
#region Helper methods
51+
private static void SendEMail(string from, string recipients, string subject, string body)
52+
{
53+
//Creates the email message
54+
var emailMessage = new MailMessage(from, recipients);
55+
//Adds the subject for email
56+
emailMessage.Subject = subject;
57+
//Sets the HTML string as email body
58+
emailMessage.IsBodyHtml = true;
59+
emailMessage.Body = body;
60+
//Sends the email with prepared message
61+
using (var client = new SmtpClient())
62+
{
63+
//Update your SMTP Server address here
64+
client.Host = "smtp.live.com";
65+
client.UseDefaultCredentials = false;
66+
//Update your email credentials here
67+
client.Credentials = new System.Net.NetworkCredential(from, "password");
68+
client.Port = 587;
69+
client.EnableSsl = true;
70+
client.Send(emailMessage);
71+
}
72+
}
73+
/// <summary>
74+
/// Gets the data to perform mail merge.
75+
/// </summary>
76+
/// <returns></returns>
77+
private static List<object> GetRecipients()
78+
{
79+
//Reads the JSON object from JSON file.
80+
JObject jsonObject = JObject.Parse(File.ReadAllText(@"../../../CustomerDetails.json"));
81+
//Converts JSON object to Dictionary.
82+
IDictionary<string, object> data = GetData(jsonObject);
83+
return data["Customers"] as List<object>;
84+
}
85+
86+
/// <summary>
87+
/// Gets data from JSON object.
88+
/// </summary>
89+
/// <param name="jsonObject">JSON object.</param>
90+
/// <returns>Dictionary of data.</returns>
91+
private static IDictionary<string, object> GetData(JObject jsonObject)
92+
{
93+
Dictionary<string, object> dictionary = new Dictionary<string, object>();
94+
foreach (var item in jsonObject)
95+
{
96+
object keyValue = null;
97+
if (item.Value is JArray)
98+
keyValue = GetData((JArray)item.Value);
99+
else if (item.Value is JToken)
100+
keyValue = ((JToken)item.Value).ToObject<string>();
101+
dictionary.Add(item.Key, keyValue);
102+
}
103+
return dictionary;
104+
}
105+
/// <summary>
106+
/// Gets array of items from JSON array.
107+
/// </summary>
108+
/// <param name="jArray">JSON array.</param>
109+
/// <returns>List of objects.</returns>
110+
private static List<object> GetData(JArray jArray)
111+
{
112+
List<object> jArrayItems = new List<object>();
113+
foreach (var item in jArray)
114+
{
115+
object keyValue = null;
116+
if (item is JObject)
117+
keyValue = GetData((JObject)item);
118+
jArrayItems.Add(keyValue);
119+
}
120+
return jArrayItems;
121+
}
122+
#endregion
123+
}
124+
}
Loading
Loading

0 commit comments

Comments
 (0)