Skip to content

update project files #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions CS/BlazorAppWordProcessing.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35527.113 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorAppWordProcessing", "BlazorAppWordProcessing\BlazorAppWordProcessing.csproj", "{3E860A22-CEDF-4FC1-A9D4-52102B917229}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3E860A22-CEDF-4FC1-A9D4-52102B917229}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E860A22-CEDF-4FC1-A9D4-52102B917229}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E860A22-CEDF-4FC1-A9D4-52102B917229}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E860A22-CEDF-4FC1-A9D4-52102B917229}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -7,9 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DevExpress.Blazor" Version="24.2.*" />
<PackageReference Include="DevExpress.Document.Processor" Version="24.2.*" />
<PackageReference Include="MailKit" Version="4.7.1.1" />
<PackageReference Include="DevExpress.Blazor" Version="24.2.3" />
<PackageReference Include="DevExpress.Document.Processor" Version="24.2.3" />
<PackageReference Include="DevExpress.Drawing.Skia" Version="24.2.3" />
<PackageReference Include="MailKit" Version="4.9.0" />
</ItemGroup>

<ItemGroup>
Expand Down
22 changes: 22 additions & 0 deletions CS/BlazorAppWordProcessing/Components/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="/" />
<link rel="stylesheet" href="bootstrap/bootstrap.min.css" />
<link rel="stylesheet" href="app.css" />
<link rel="stylesheet" href="BlazorAppWordProcessing.styles.css" />
<link rel="icon" type="image/png" href="favicon.png" />
<link href="_content/DevExpress.Blazor.Themes/blazing-berry.bs5.css" rel="stylesheet" />
<script src="js/exportToPdf.js"></script>
<HeadOutlet />
</head>

<body>
<Routes />
<script src="_framework/blazor.web.js"></script>
</body>

</html>
23 changes: 23 additions & 0 deletions CS/BlazorAppWordProcessing/Components/Layout/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@inherits LayoutComponentBase

<div class="page">
<div class="sidebar">
<NavMenu />
</div>

<main>
<div class="top-row px-4">
<a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a>
</div>

<article class="content px-4">
@Body
</article>
</main>
</div>

<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
36 changes: 36 additions & 0 deletions CS/BlazorAppWordProcessing/Components/Pages/Error.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@page "/Error"
@using System.Diagnostics

<PageTitle>Error</PageTitle>

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>

@code{
[CascadingParameter]
private HttpContext? HttpContext { get; set; }

private string? RequestId { get; set; }
private bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

protected override void OnInitialized() =>
RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@page "/"
@rendermode InteractiveServer
@inject IJSRuntime JS
@using System.IO
@using DevExpress.XtraRichEdit
@using MimeKit;
@using MailKit.Net.Smtp
@using Microsoft.Extensions.Logging
@inject ILogger<Index> logger

<div class="container">
<div class="card mt-3">
<div class="card-header">
Expand All @@ -16,7 +16,7 @@
<DxFormLayoutItem ColSpanMd="12">
<Template>
<p>
This example uses the
This example uses the
<a href="https://www.devexpress.com/products/net/office-file-api/word/">Word Processing Document API</a>
to generate personalized letters based on a template. Select a sender and click <b>Send emails</b> to send
letters to all recipients. Click <b>Download</b> to download a letter in PDF format for a specific recipient.
Expand All @@ -38,26 +38,26 @@
</DxFormLayoutItem>
</DxFormLayout>
<div class="col">
<DxGrid Data="@dataSource.RecipientList"
CssClass="mt-3" PagerVisible="false">
<Columns>
<DxGridDataColumn FieldName="@nameof(Recipient.ContactName)" Caption="Recipient" />
<DxGridDataColumn FieldName="@nameof(Recipient.Email)" Caption="Email" />
<DxGridDataColumn Caption="Attachment" Width="150px">
<CellDisplayTemplate>
<DxGrid Data="@dataSource.RecipientList"
CssClass="mt-3" PagerVisible="false">
<Columns>
<DxGridDataColumn FieldName="@nameof(Recipient.ContactName)" Caption="Recipient" />
<DxGridDataColumn FieldName="@nameof(Recipient.Email)" Caption="Email" />
<DxGridDataColumn Caption="Attachment" Width="150px">
<CellDisplayTemplate>
@{
var recipient = context.DataItem as Recipient;
@if (recipient != null)
{
<DxButton Text="Download"
CssClass="btn-block"
Click="@((MouseEventArgs args) => DownloadPdf(recipient))" />
<DxButton Text="Download"
CssClass="btn-block"
Click="@((MouseEventArgs args) => DownloadPdf(recipient))" />
}
}
</CellDisplayTemplate>
</DxGridDataColumn>
</Columns>
</DxGrid>
</CellDisplayTemplate>
</DxGridDataColumn>
</Columns>
</DxGrid>
</div>
</div>
</div>
Expand All @@ -68,20 +68,26 @@
SampleData dataSource = new();
Sender? selectedSender;

Sender? SelectedSender {
Sender? SelectedSender
{
get => selectedSender;
set { selectedSender = value;
InvokeAsync(StateHasChanged); }
set
{
selectedSender = value;
InvokeAsync(StateHasChanged);
}
}

protected override Task OnInitializedAsync() {
protected override Task OnInitializedAsync()
{
selectedSender = dataSource.SenderList[0];
documentServer.LoadDocument("Data/MailMerge.rtf", DocumentFormat.Rtf);
return base.OnInitializedAsync();
}

<!--#region CreateAttachment-->
MemoryStream CreateAttachment(Recipient recipient) {
MemoryStream CreateAttachment(Recipient recipient)
{
ArgumentNullException.ThrowIfNull(SelectedSender);

using (var resultDocumentServer = new RichEditDocumentServer())
Expand All @@ -100,8 +106,8 @@
}
}
<!--#endregion CreateAttachment-->

void DownloadPdf(Recipient recipient) {
void DownloadPdf(Recipient recipient)
{
// Execute mail merge.
using (MemoryStream stream = CreateAttachment(recipient))
{
Expand All @@ -110,15 +116,18 @@
Convert.ToBase64String(stream.ToArray()));
}
}

<!--#region SendMessages-->
void SendEmails(MouseEventArgs mouseEventArgs) {
void SendEmails(MouseEventArgs mouseEventArgs)
{
// Obtain a list of recipients.
List<Recipient> recipientList = dataSource.RecipientList;

for (int i = 0; i < recipientList.Count; i++) {
for (int i = 0; i < recipientList.Count; i++)
{
// Execute mail merge to generate a letter for each recipient.
using (MemoryStream attachmentStream = CreateAttachment(recipientList[i])) {
using (MemoryStream attachmentStream = CreateAttachment(recipientList[i]))
{
// Create e-mail message for each recipient.
// Attach letter to the message as a PDF file.
MimeMessage message = CreateMessage("YOUR_EMAIL_ADDRESS",
Expand All @@ -129,7 +138,8 @@
}
}

MimeMessage CreateMessage(string from, Recipient recipient, MemoryStream attachmentStream) {
MimeMessage CreateMessage(string from, Recipient recipient, MemoryStream attachmentStream)
{
var message = new MimeMessage();
// Specify the sender's address.
message.From.Add(new MailboxAddress(SelectedSender?.FullName, from));
Expand All @@ -138,17 +148,19 @@
message.Subject = "Your message subject";

// Create the body of your message.
var body = new TextPart() {
var body = new TextPart()
{
Text = "Your message text"
};
};

// Create a PDF attachment to send the generated letter.
var attachment = new MimePart("application", "pdf") {
var attachment = new MimePart("application", "pdf")
{
Content = new MimeContent(attachmentStream, ContentEncoding.Default),
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Base64,
FileName = "attachment.pdf"
};
};

var multipart = new Multipart("mixed");
multipart.Add(body);
Expand All @@ -159,23 +171,28 @@
return message;
}

void SendMessage(MimeMessage message) {
using (var client = new SmtpClient()) {
void SendMessage(MimeMessage message)
{
using (var client = new SmtpClient())
{
// Connect to your SMTP server to send the message.
// Use one of these ports: 25, 465, 587, or 2525.
client.Connect("MAIL_SERVER", 587, false);

// Use the code below if your SMTP server requires authentication.
client.Authenticate("USERNAME", "PASSWORD");

try {
try
{
// Send the message.
client.Send(message);
}
catch(Exception ex) {
catch (Exception ex)
{
logger.LogError(ex.Message);
}
finally {
finally
{
client.Disconnect(true);
}
}
Expand Down
6 changes: 6 additions & 0 deletions CS/BlazorAppWordProcessing/Components/Routes.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
<FocusOnNavigate RouteData="routeData" Selector="h1" />
</Found>
</Router>
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using BlazorApp_WordDocumentAPI
@using BlazorApp_WordDocumentAPI.Shared
@using BlazorAppWordProcessing
@using BlazorAppWordProcessing.Components
@using DevExpress.Blazor
@using System.IO
@using DevExpress.XtraRichEdit

Loading
Loading