|
1 | 1 | @page "/" |
| 2 | +@rendermode InteractiveServer |
2 | 3 | @inject IJSRuntime JS |
3 | | -@using System.IO |
4 | | -@using DevExpress.XtraRichEdit |
5 | 4 | @using MimeKit; |
6 | 5 | @using MailKit.Net.Smtp |
7 | 6 | @using Microsoft.Extensions.Logging |
8 | 7 | @inject ILogger<Index> logger |
| 8 | + |
9 | 9 | <div class="container"> |
10 | 10 | <div class="card mt-3"> |
11 | 11 | <div class="card-header"> |
|
16 | 16 | <DxFormLayoutItem ColSpanMd="12"> |
17 | 17 | <Template> |
18 | 18 | <p> |
19 | | - This example uses the |
| 19 | + This example uses the |
20 | 20 | <a href="https://www.devexpress.com/products/net/office-file-api/word/">Word Processing Document API</a> |
21 | 21 | to generate personalized letters based on a template. Select a sender and click <b>Send emails</b> to send |
22 | 22 | letters to all recipients. Click <b>Download</b> to download a letter in PDF format for a specific recipient. |
|
38 | 38 | </DxFormLayoutItem> |
39 | 39 | </DxFormLayout> |
40 | 40 | <div class="col"> |
41 | | -<DxGrid Data="@dataSource.RecipientList" |
42 | | - CssClass="mt-3" PagerVisible="false"> |
43 | | -<Columns> |
44 | | -<DxGridDataColumn FieldName="@nameof(Recipient.ContactName)" Caption="Recipient" /> |
45 | | -<DxGridDataColumn FieldName="@nameof(Recipient.Email)" Caption="Email" /> |
46 | | -<DxGridDataColumn Caption="Attachment" Width="150px"> |
47 | | -<CellDisplayTemplate> |
| 41 | + <DxGrid Data="@dataSource.RecipientList" |
| 42 | + CssClass="mt-3" PagerVisible="false"> |
| 43 | + <Columns> |
| 44 | + <DxGridDataColumn FieldName="@nameof(Recipient.ContactName)" Caption="Recipient" /> |
| 45 | + <DxGridDataColumn FieldName="@nameof(Recipient.Email)" Caption="Email" /> |
| 46 | + <DxGridDataColumn Caption="Attachment" Width="150px"> |
| 47 | + <CellDisplayTemplate> |
48 | 48 | @{ |
49 | 49 | var recipient = context.DataItem as Recipient; |
50 | 50 | @if (recipient != null) |
51 | 51 | { |
52 | | - <DxButton Text="Download" |
53 | | - CssClass="btn-block" |
54 | | - Click="@((MouseEventArgs args) => DownloadPdf(recipient))" /> |
| 52 | + <DxButton Text="Download" |
| 53 | + CssClass="btn-block" |
| 54 | + Click="@((MouseEventArgs args) => DownloadPdf(recipient))" /> |
55 | 55 | } |
56 | 56 | } |
57 | | -</CellDisplayTemplate> |
58 | | -</DxGridDataColumn> |
59 | | -</Columns> |
60 | | -</DxGrid> |
| 57 | + </CellDisplayTemplate> |
| 58 | + </DxGridDataColumn> |
| 59 | + </Columns> |
| 60 | + </DxGrid> |
61 | 61 | </div> |
62 | 62 | </div> |
63 | 63 | </div> |
|
68 | 68 | SampleData dataSource = new(); |
69 | 69 | Sender? selectedSender; |
70 | 70 |
|
71 | | - Sender? SelectedSender { |
| 71 | + Sender? SelectedSender |
| 72 | + { |
72 | 73 | get => selectedSender; |
73 | | - set { selectedSender = value; |
74 | | - InvokeAsync(StateHasChanged); } |
| 74 | + set |
| 75 | + { |
| 76 | + selectedSender = value; |
| 77 | + InvokeAsync(StateHasChanged); |
| 78 | + } |
75 | 79 | } |
76 | 80 |
|
77 | | - protected override Task OnInitializedAsync() { |
| 81 | + protected override Task OnInitializedAsync() |
| 82 | + { |
78 | 83 | selectedSender = dataSource.SenderList[0]; |
79 | 84 | documentServer.LoadDocument("Data/MailMerge.rtf", DocumentFormat.Rtf); |
80 | 85 | return base.OnInitializedAsync(); |
81 | 86 | } |
82 | | - |
| 87 | + |
83 | 88 | <!--#region CreateAttachment--> |
84 | | - MemoryStream CreateAttachment(Recipient recipient) { |
| 89 | + MemoryStream CreateAttachment(Recipient recipient) |
| 90 | + { |
85 | 91 | ArgumentNullException.ThrowIfNull(SelectedSender); |
86 | 92 |
|
87 | 93 | using (var resultDocumentServer = new RichEditDocumentServer()) |
|
100 | 106 | } |
101 | 107 | } |
102 | 108 | <!--#endregion CreateAttachment--> |
103 | | - |
104 | | - void DownloadPdf(Recipient recipient) { |
| 109 | + void DownloadPdf(Recipient recipient) |
| 110 | + { |
105 | 111 | // Execute mail merge. |
106 | 112 | using (MemoryStream stream = CreateAttachment(recipient)) |
107 | 113 | { |
|
110 | 116 | Convert.ToBase64String(stream.ToArray())); |
111 | 117 | } |
112 | 118 | } |
113 | | - |
| 119 | + |
114 | 120 | <!--#region SendMessages--> |
115 | | - void SendEmails(MouseEventArgs mouseEventArgs) { |
| 121 | + void SendEmails(MouseEventArgs mouseEventArgs) |
| 122 | + { |
116 | 123 | // Obtain a list of recipients. |
117 | 124 | List<Recipient> recipientList = dataSource.RecipientList; |
118 | 125 |
|
119 | | - for (int i = 0; i < recipientList.Count; i++) { |
| 126 | + for (int i = 0; i < recipientList.Count; i++) |
| 127 | + { |
120 | 128 | // Execute mail merge to generate a letter for each recipient. |
121 | | - using (MemoryStream attachmentStream = CreateAttachment(recipientList[i])) { |
| 129 | + using (MemoryStream attachmentStream = CreateAttachment(recipientList[i])) |
| 130 | + { |
122 | 131 | // Create e-mail message for each recipient. |
123 | 132 | // Attach letter to the message as a PDF file. |
124 | 133 | MimeMessage message = CreateMessage("YOUR_EMAIL_ADDRESS", |
|
129 | 138 | } |
130 | 139 | } |
131 | 140 |
|
132 | | - MimeMessage CreateMessage(string from, Recipient recipient, MemoryStream attachmentStream) { |
| 141 | + MimeMessage CreateMessage(string from, Recipient recipient, MemoryStream attachmentStream) |
| 142 | + { |
133 | 143 | var message = new MimeMessage(); |
134 | 144 | // Specify the sender's address. |
135 | 145 | message.From.Add(new MailboxAddress(SelectedSender?.FullName, from)); |
|
138 | 148 | message.Subject = "Your message subject"; |
139 | 149 |
|
140 | 150 | // Create the body of your message. |
141 | | - var body = new TextPart() { |
| 151 | + var body = new TextPart() |
| 152 | + { |
142 | 153 | Text = "Your message text" |
143 | | - }; |
| 154 | + }; |
144 | 155 |
|
145 | 156 | // Create a PDF attachment to send the generated letter. |
146 | | - var attachment = new MimePart("application", "pdf") { |
| 157 | + var attachment = new MimePart("application", "pdf") |
| 158 | + { |
147 | 159 | Content = new MimeContent(attachmentStream, ContentEncoding.Default), |
148 | 160 | ContentDisposition = new ContentDisposition(ContentDisposition.Attachment), |
149 | 161 | ContentTransferEncoding = ContentEncoding.Base64, |
150 | 162 | FileName = "attachment.pdf" |
151 | | - }; |
| 163 | + }; |
152 | 164 |
|
153 | 165 | var multipart = new Multipart("mixed"); |
154 | 166 | multipart.Add(body); |
|
159 | 171 | return message; |
160 | 172 | } |
161 | 173 |
|
162 | | - void SendMessage(MimeMessage message) { |
163 | | - using (var client = new SmtpClient()) { |
| 174 | + void SendMessage(MimeMessage message) |
| 175 | + { |
| 176 | + using (var client = new SmtpClient()) |
| 177 | + { |
164 | 178 | // Connect to your SMTP server to send the message. |
165 | 179 | // Use one of these ports: 25, 465, 587, or 2525. |
166 | 180 | client.Connect("MAIL_SERVER", 587, false); |
167 | 181 |
|
168 | 182 | // Use the code below if your SMTP server requires authentication. |
169 | 183 | client.Authenticate("USERNAME", "PASSWORD"); |
170 | 184 |
|
171 | | - try { |
| 185 | + try |
| 186 | + { |
172 | 187 | // Send the message. |
173 | 188 | client.Send(message); |
174 | 189 | } |
175 | | - catch(Exception ex) { |
| 190 | + catch (Exception ex) |
| 191 | + { |
176 | 192 | logger.LogError(ex.Message); |
177 | 193 | } |
178 | | - finally { |
| 194 | + finally |
| 195 | + { |
179 | 196 | client.Disconnect(true); |
180 | 197 | } |
181 | 198 | } |
|
0 commit comments