mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-04-05 21:01:35 +08:00
- Added attachments logic
This commit is contained in:
parent
59a9f0bf03
commit
580b79c24d
@ -1,4 +1,6 @@
|
|||||||
namespace Orchard.Email.Models {
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Orchard.Email.Models {
|
||||||
public class EmailMessage {
|
public class EmailMessage {
|
||||||
public string Subject { get; set; }
|
public string Subject { get; set; }
|
||||||
public string Body { get; set; }
|
public string Body { get; set; }
|
||||||
@ -7,5 +9,9 @@
|
|||||||
public string From { get; set; }
|
public string From { get; set; }
|
||||||
public string Bcc { get; set; }
|
public string Bcc { get; set; }
|
||||||
public string Cc { get; set; }
|
public string Cc { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// IEnumerable of strings representing attachments paths
|
||||||
|
/// </summary>
|
||||||
|
public IEnumerable<string> Attachments { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,6 +9,8 @@ using Orchard.ContentManagement;
|
|||||||
using Orchard.DisplayManagement;
|
using Orchard.DisplayManagement;
|
||||||
using Orchard.Logging;
|
using Orchard.Logging;
|
||||||
using Orchard.Email.Models;
|
using Orchard.Email.Models;
|
||||||
|
using System.Linq;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace Orchard.Email.Services {
|
namespace Orchard.Email.Services {
|
||||||
public class SmtpMessageChannel : Component, ISmtpChannel, IDisposable {
|
public class SmtpMessageChannel : Component, ISmtpChannel, IDisposable {
|
||||||
@ -51,7 +53,8 @@ namespace Orchard.Email.Services {
|
|||||||
ReplyTo = Read(parameters, "ReplyTo"),
|
ReplyTo = Read(parameters, "ReplyTo"),
|
||||||
From = Read(parameters, "From"),
|
From = Read(parameters, "From"),
|
||||||
Bcc = Read(parameters, "Bcc"),
|
Bcc = Read(parameters, "Bcc"),
|
||||||
Cc = Read(parameters, "CC")
|
Cc = Read(parameters, "CC"),
|
||||||
|
Attachments = (IEnumerable<string>)(parameters.ContainsKey("Attachments") ? parameters["Attachments"] : new List<string>())
|
||||||
};
|
};
|
||||||
|
|
||||||
if (emailMessage.Recipients.Length == 0) {
|
if (emailMessage.Recipients.Length == 0) {
|
||||||
@ -105,8 +108,7 @@ namespace Orchard.Email.Services {
|
|||||||
|
|
||||||
if (!String.IsNullOrWhiteSpace(emailMessage.From)) {
|
if (!String.IsNullOrWhiteSpace(emailMessage.From)) {
|
||||||
mailMessage.From = new MailAddress(emailMessage.From);
|
mailMessage.From = new MailAddress(emailMessage.From);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// Take 'From' address from site settings or web.config.
|
// Take 'From' address from site settings or web.config.
|
||||||
mailMessage.From = !String.IsNullOrWhiteSpace(_smtpSettings.Address)
|
mailMessage.From = !String.IsNullOrWhiteSpace(_smtpSettings.Address)
|
||||||
? new MailAddress(_smtpSettings.Address)
|
? new MailAddress(_smtpSettings.Address)
|
||||||
@ -119,9 +121,15 @@ namespace Orchard.Email.Services {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach (var attachmentPath in emailMessage.Attachments) {
|
||||||
|
if (File.Exists(attachmentPath)) {
|
||||||
|
mailMessage.Attachments.Add(new Attachment(attachmentPath));
|
||||||
|
} else {
|
||||||
|
throw new FileNotFoundException(T("One or more attachments not found.").Text);
|
||||||
|
}
|
||||||
|
}
|
||||||
_smtpClientField.Value.Send(mailMessage);
|
_smtpClientField.Value.Send(mailMessage);
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
Logger.Error(e, "Could not send email");
|
Logger.Error(e, "Could not send email");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -129,7 +137,7 @@ namespace Orchard.Email.Services {
|
|||||||
private SmtpClient CreateSmtpClient() {
|
private SmtpClient CreateSmtpClient() {
|
||||||
// If no properties are set in the dashboard, use the web.config value.
|
// If no properties are set in the dashboard, use the web.config value.
|
||||||
if (String.IsNullOrWhiteSpace(_smtpSettings.Host)) {
|
if (String.IsNullOrWhiteSpace(_smtpSettings.Host)) {
|
||||||
return new SmtpClient();
|
return new SmtpClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
var smtpClient = new SmtpClient {
|
var smtpClient = new SmtpClient {
|
||||||
@ -155,7 +163,7 @@ namespace Orchard.Email.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<string> ParseRecipients(string recipients) {
|
private IEnumerable<string> ParseRecipients(string recipients) {
|
||||||
return recipients.Split(new[] {',', ';'}, StringSplitOptions.RemoveEmptyEntries);
|
return recipients.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user