Add `AdditionalEmailSendingArgs`.

pull/17582/head
maliming 2 years ago
parent e064a4f92c
commit b3527d6fed
No known key found for this signature in database
GPG Key ID: A646B9CB645ECEA4

@ -62,8 +62,7 @@ namespace MyProject
* **subject**: You can set the email subject.
* **body**: You can set the email body.
* **isBodyHtml**: Indicates whether the email body may contain HTML tags. **Default: true**.
* **attachments**: You can pass a list of `EmailAttachment` to add attachments to the email.
* **extraProperties**: You can pass extra properties and use these properties in your own `IEmailSender` implementation to implement more mail sending features.
* **additionalEmailSendingArgs**: This parameter is used to pass additional arguments to the `IEmailSender` implementation. Include: CC(Carbon copy), a list of `EmailAttachment` and an extra properties.
> `IEmailSender` is the suggested way to send emails, since it makes your code provider independent.

@ -0,0 +1,13 @@
using System.Collections.Generic;
using Volo.Abp.Data;
namespace Volo.Abp.Emailing;
public class AdditionalEmailSendingArgs
{
public List<string>? CC { get; set; }
public List<EmailAttachment>? Attachments { get; set; }
public ExtraPropertyDictionary? ExtraProperties { get; set; }
}

@ -19,11 +19,11 @@ public class BackgroundEmailSendingJob : AsyncBackgroundJob<BackgroundEmailSendi
{
if (args.From.IsNullOrWhiteSpace())
{
await EmailSender.SendAsync(args.To, args.Subject, args.Body, args.IsBodyHtml, args.Attachments, args.ExtraProperties);
await EmailSender.SendAsync(args.To, args.Subject, args.Body, args.IsBodyHtml, args.AdditionalEmailSendingArgs);
}
else
{
await EmailSender.SendAsync(args.From!, args.To, args.Subject, args.Body, args.IsBodyHtml, args.Attachments, args.ExtraProperties);
await EmailSender.SendAsync(args.From!, args.To, args.Subject, args.Body, args.IsBodyHtml, args.AdditionalEmailSendingArgs);
}
}
}

@ -20,7 +20,5 @@ public class BackgroundEmailSendingJobArgs
/// </summary>
public bool IsBodyHtml { get; set; } = true;
public List<EmailAttachment>? Attachments { get; set; }
public ExtraPropertyDictionary? ExtraProperties { get; set; }
public AdditionalEmailSendingArgs? AdditionalEmailSendingArgs { get; set; }
}

@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.Data;
namespace Volo.Abp.Emailing;
@ -28,29 +26,40 @@ public abstract class EmailSenderBase : IEmailSender
BackgroundJobManager = backgroundJobManager;
}
public virtual async Task SendAsync(string to, string? subject, string? body, bool isBodyHtml = true, List<EmailAttachment>? attachments = null, ExtraPropertyDictionary? extraProperties = null)
public virtual async Task SendAsync(string to, string? subject, string? body, bool isBodyHtml = true, AdditionalEmailSendingArgs? additionalEmailSendingArgs = null)
{
await SendAsync(BuildMailMessage(null, to, subject, body, isBodyHtml, attachments, extraProperties));
await SendAsync(BuildMailMessage(null, to, subject, body, isBodyHtml, additionalEmailSendingArgs));
}
public virtual async Task SendAsync(string from, string to, string? subject, string? body, bool isBodyHtml = true, List<EmailAttachment>? attachments = null, ExtraPropertyDictionary? extraProperties = null)
public virtual async Task SendAsync(string from, string to, string? subject, string? body, bool isBodyHtml = true, AdditionalEmailSendingArgs? additionalEmailSendingArgs = null)
{
await SendAsync(BuildMailMessage(from, to, subject, body, isBodyHtml, attachments, extraProperties));
await SendAsync(BuildMailMessage(from, to, subject, body, isBodyHtml, additionalEmailSendingArgs));
}
protected virtual MailMessage BuildMailMessage(string? from, string to, string? subject, string? body, bool isBodyHtml = true, List<EmailAttachment>? attachments = null, ExtraPropertyDictionary? extraProperties = null)
protected virtual MailMessage BuildMailMessage(string? from, string to, string? subject, string? body, bool isBodyHtml = true, AdditionalEmailSendingArgs? additionalEmailSendingArgs = null)
{
var message = from == null
? new MailMessage { To = { to }, Subject = subject, Body = body, IsBodyHtml = isBodyHtml }
: new MailMessage(from, to, subject, body) { IsBodyHtml = isBodyHtml };
if (attachments != null)
if (additionalEmailSendingArgs != null)
{
foreach (var attachment in attachments.Where(x => x.File != null))
if (additionalEmailSendingArgs.Attachments != null)
{
var fileStream = new MemoryStream(attachment.File!);
fileStream.Seek(0, SeekOrigin.Begin);
message.Attachments.Add(new Attachment(fileStream, attachment.Name));
foreach (var attachment in additionalEmailSendingArgs.Attachments.Where(x => x.File != null))
{
var fileStream = new MemoryStream(attachment.File!);
fileStream.Seek(0, SeekOrigin.Begin);
message.Attachments.Add(new Attachment(fileStream, attachment.Name));
}
}
if (additionalEmailSendingArgs.CC != null)
{
foreach (var cc in additionalEmailSendingArgs.CC)
{
message.CC.Add(cc);
}
}
}
@ -67,11 +76,11 @@ public abstract class EmailSenderBase : IEmailSender
await SendEmailAsync(mail);
}
public virtual async Task QueueAsync(string to, string subject, string body, bool isBodyHtml = true, List<EmailAttachment>? attachments = null, ExtraPropertyDictionary? extraProperties = null)
public virtual async Task QueueAsync(string to, string subject, string body, bool isBodyHtml = true, AdditionalEmailSendingArgs? additionalEmailSendingArgs = null)
{
if (!BackgroundJobManager.IsAvailable())
{
await SendAsync(to, subject, body, isBodyHtml, attachments, extraProperties);
await SendAsync(to, subject, body, isBodyHtml, additionalEmailSendingArgs);
return;
}
@ -82,17 +91,16 @@ public abstract class EmailSenderBase : IEmailSender
Subject = subject,
Body = body,
IsBodyHtml = isBodyHtml,
Attachments = attachments,
ExtraProperties = extraProperties
AdditionalEmailSendingArgs = additionalEmailSendingArgs
}
);
}
public virtual async Task QueueAsync(string from, string to, string subject, string body, bool isBodyHtml = true, List<EmailAttachment>? attachments = null, ExtraPropertyDictionary? extraProperties = null)
public virtual async Task QueueAsync(string from, string to, string subject, string body, bool isBodyHtml = true, AdditionalEmailSendingArgs? additionalEmailSendingArgs = null)
{
if (!BackgroundJobManager.IsAvailable())
{
await SendAsync(from, to, subject, body, isBodyHtml, attachments, extraProperties);
await SendAsync(from, to, subject, body, isBodyHtml, additionalEmailSendingArgs);
return;
}
@ -104,8 +112,7 @@ public abstract class EmailSenderBase : IEmailSender
Subject = subject,
Body = body,
IsBodyHtml = isBodyHtml,
Attachments = attachments,
ExtraProperties = extraProperties
AdditionalEmailSendingArgs = additionalEmailSendingArgs
}
);
}

@ -1,7 +1,5 @@
using System.Collections.Generic;
using System.Net.Mail;
using System.Net.Mail;
using System.Threading.Tasks;
using Volo.Abp.Data;
namespace Volo.Abp.Emailing;
@ -18,8 +16,7 @@ public interface IEmailSender
string? subject,
string? body,
bool isBodyHtml = true,
List<EmailAttachment>? attachments = null,
ExtraPropertyDictionary? extraProperties = null
AdditionalEmailSendingArgs? additionalEmailSendingArgs = null
);
/// <summary>
@ -31,8 +28,7 @@ public interface IEmailSender
string? subject,
string? body,
bool isBodyHtml = true,
List<EmailAttachment>? attachments = null,
ExtraPropertyDictionary? extraProperties = null
AdditionalEmailSendingArgs? additionalEmailSendingArgs = null
);
/// <summary>
@ -56,8 +52,7 @@ public interface IEmailSender
string subject,
string body,
bool isBodyHtml = true,
List<EmailAttachment>? attachments = null,
ExtraPropertyDictionary? extraProperties = null
AdditionalEmailSendingArgs? additionalEmailSendingArgs = null
);
/// <summary>
@ -69,7 +64,6 @@ public interface IEmailSender
string subject,
string body,
bool isBodyHtml = true,
List<EmailAttachment>? attachments = null,
ExtraPropertyDictionary? extraProperties = null
AdditionalEmailSendingArgs? additionalEmailSendingArgs = null
);
}

Loading…
Cancel
Save