Browse Source

Merge pull request #17582 from abpframework/AdditionalEmailSendingArgs

Add `AdditionalEmailSendingArgs`.
pull/17602/head
maliming 3 years ago
committed by GitHub
parent
commit
b7ca9b71c6
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      docs/en/Emailing.md
  2. 15
      framework/src/Volo.Abp.Emailing/Volo/Abp/Emailing/AdditionalEmailSendingArgs.cs
  3. 4
      framework/src/Volo.Abp.Emailing/Volo/Abp/Emailing/BackgroundEmailSendingJob.cs
  4. 4
      framework/src/Volo.Abp.Emailing/Volo/Abp/Emailing/BackgroundEmailSendingJobArgs.cs
  5. 47
      framework/src/Volo.Abp.Emailing/Volo/Abp/Emailing/EmailSenderBase.cs
  6. 16
      framework/src/Volo.Abp.Emailing/Volo/Abp/Emailing/IEmailSender.cs

3
docs/en/Emailing.md

@ -62,8 +62,7 @@ namespace MyProject
* **subject**: You can set the email subject. * **subject**: You can set the email subject.
* **body**: You can set the email body. * **body**: You can set the email body.
* **isBodyHtml**: Indicates whether the email body may contain HTML tags. **Default: true**. * **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. * **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.
* **extraProperties**: You can pass extra properties and use these properties in your own `IEmailSender` implementation to implement more mail sending features.
> `IEmailSender` is the suggested way to send emails, since it makes your code provider independent. > `IEmailSender` is the suggested way to send emails, since it makes your code provider independent.

15
framework/src/Volo.Abp.Emailing/Volo/Abp/Emailing/AdditionalEmailSendingArgs.cs

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

4
framework/src/Volo.Abp.Emailing/Volo/Abp/Emailing/BackgroundEmailSendingJob.cs

@ -19,11 +19,11 @@ public class BackgroundEmailSendingJob : AsyncBackgroundJob<BackgroundEmailSendi
{ {
if (args.From.IsNullOrWhiteSpace()) 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 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);
} }
} }
} }

4
framework/src/Volo.Abp.Emailing/Volo/Abp/Emailing/BackgroundEmailSendingJobArgs.cs

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

47
framework/src/Volo.Abp.Emailing/Volo/Abp/Emailing/EmailSenderBase.cs

@ -1,12 +1,10 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net.Mail; using System.Net.Mail;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.BackgroundJobs; using Volo.Abp.BackgroundJobs;
using Volo.Abp.Data;
namespace Volo.Abp.Emailing; namespace Volo.Abp.Emailing;
@ -28,29 +26,40 @@ public abstract class EmailSenderBase : IEmailSender
BackgroundJobManager = backgroundJobManager; 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 var message = from == null
? new MailMessage { To = { to }, Subject = subject, Body = body, IsBodyHtml = isBodyHtml } ? new MailMessage { To = { to }, Subject = subject, Body = body, IsBodyHtml = isBodyHtml }
: new MailMessage(from, to, subject, 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!); foreach (var attachment in additionalEmailSendingArgs.Attachments.Where(x => x.File != null))
fileStream.Seek(0, SeekOrigin.Begin); {
message.Attachments.Add(new Attachment(fileStream, attachment.Name)); 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); 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()) if (!BackgroundJobManager.IsAvailable())
{ {
await SendAsync(to, subject, body, isBodyHtml, attachments, extraProperties); await SendAsync(to, subject, body, isBodyHtml, additionalEmailSendingArgs);
return; return;
} }
@ -82,17 +91,16 @@ public abstract class EmailSenderBase : IEmailSender
Subject = subject, Subject = subject,
Body = body, Body = body,
IsBodyHtml = isBodyHtml, IsBodyHtml = isBodyHtml,
Attachments = attachments, AdditionalEmailSendingArgs = additionalEmailSendingArgs
ExtraProperties = extraProperties
} }
); );
} }
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()) if (!BackgroundJobManager.IsAvailable())
{ {
await SendAsync(from, to, subject, body, isBodyHtml, attachments, extraProperties); await SendAsync(from, to, subject, body, isBodyHtml, additionalEmailSendingArgs);
return; return;
} }
@ -104,8 +112,7 @@ public abstract class EmailSenderBase : IEmailSender
Subject = subject, Subject = subject,
Body = body, Body = body,
IsBodyHtml = isBodyHtml, IsBodyHtml = isBodyHtml,
Attachments = attachments, AdditionalEmailSendingArgs = additionalEmailSendingArgs
ExtraProperties = extraProperties
} }
); );
} }

16
framework/src/Volo.Abp.Emailing/Volo/Abp/Emailing/IEmailSender.cs

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

Loading…
Cancel
Save