-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes in EmailServiceEntity after merge
- Loading branch information
1 parent
adb455f
commit d9d219a
Showing
15 changed files
with
197 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 69 additions & 69 deletions
138
Signum.Engine.Extensions/Mailing/Senders/ExchangeSender.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,76 @@ | ||
using Signum.Entities.Mailing; | ||
using Signum.Engine.Authorization; | ||
using Signum.Engine.Files; | ||
using Microsoft.Exchange.WebServices.Data; | ||
using Signum.Entities.Mailing; | ||
using Signum.Engine.Authorization; | ||
using Signum.Engine.Files; | ||
using Microsoft.Exchange.WebServices.Data; | ||
|
||
namespace Signum.Engine.Mailing.Senders; | ||
|
||
public class ExchangeWebServiceSender : BaseEmailSender | ||
{ | ||
ExchangeWebServiceEntity exchange; | ||
namespace Signum.Engine.Mailing.Senders; | ||
|
||
public ExchangeWebServiceSender(EmailSenderConfigurationEntity senderConfig, ExchangeWebServiceEntity service) : base(senderConfig) | ||
public class ExchangeWebServiceSender : BaseEmailSender | ||
{ | ||
ExchangeWebServiceEmailServiceEntity exchange; | ||
|
||
public ExchangeWebServiceSender(EmailSenderConfigurationEntity senderConfig, ExchangeWebServiceEmailServiceEntity service) : base(senderConfig) | ||
{ | ||
exchange = service; | ||
} | ||
|
||
protected override void SendInternal(EmailMessageEntity email) | ||
{ | ||
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); | ||
service.UseDefaultCredentials = exchange.UseDefaultCredentials; | ||
service.Credentials = exchange.Username.HasText() ? new WebCredentials(exchange.Username, exchange.Password) : null; | ||
//service.TraceEnabled = true; | ||
//service.TraceFlags = TraceFlags.All; | ||
|
||
if (exchange.Url.HasText()) | ||
service.Url = new Uri(exchange.Url); | ||
else | ||
service.AutodiscoverUrl(email.From.EmailAddress, RedirectionUrlValidationCallback); | ||
|
||
EmailMessage message = new EmailMessage(service); | ||
|
||
foreach (var a in email.Attachments.Where(a => a.Type == EmailAttachmentType.Attachment)) | ||
{ | ||
var fa = message.Attachments.AddFileAttachment(a.File.FileName, a.File.GetByteArray()); | ||
fa.ContentId = a.ContentId; | ||
} | ||
message.ToRecipients.AddRange(email.Recipients.Where(r => r.Kind == EmailRecipientKind.To).Select(r => r.ToEmailAddress()).ToList()); | ||
message.CcRecipients.AddRange(email.Recipients.Where(r => r.Kind == EmailRecipientKind.Cc).Select(r => r.ToEmailAddress()).ToList()); | ||
message.BccRecipients.AddRange(email.Recipients.Where(r => r.Kind == EmailRecipientKind.Bcc).Select(r => r.ToEmailAddress()).ToList()); | ||
message.Subject = email.Subject; | ||
message.Body = new MessageBody(email.IsBodyHtml ? BodyType.HTML : BodyType.Text, email.Body.Text); | ||
message.Send(); | ||
} | ||
|
||
protected virtual bool RedirectionUrlValidationCallback(string redirectionUrl) | ||
{ | ||
// The default for the validation callback is to reject the URL. | ||
Uri redirectionUri = new Uri(redirectionUrl); | ||
// Validate the contents of the redirection URL. In this simple validation | ||
// callback, the redirection URL is considered valid if it is using HTTPS | ||
// to encrypt the authentication credentials. | ||
return redirectionUri.Scheme == "https"; | ||
} | ||
} | ||
|
||
public static class ExchangeExtensions | ||
{ | ||
public static EmailAddress ToEmailAddress(this EmailAddressEmbedded address) | ||
{ | ||
if (address.DisplayName.HasText()) | ||
return new EmailAddress(address.DisplayName, address.EmailAddress); | ||
|
||
return new EmailAddress(address.EmailAddress); | ||
} | ||
|
||
public static EmailAddress ToEmailAddress(this EmailRecipientEmbedded recipient) | ||
{ | ||
if (!EmailLogic.Configuration.SendEmails) | ||
throw new InvalidOperationException("EmailConfigurationEmbedded.SendEmails is set to false"); | ||
|
||
if (recipient.DisplayName.HasText()) | ||
return new EmailAddress(recipient.DisplayName, EmailLogic.Configuration.OverrideEmailAddress.DefaultText(recipient.EmailAddress)); | ||
|
||
return new EmailAddress(EmailLogic.Configuration.OverrideEmailAddress.DefaultText(recipient.EmailAddress)); | ||
} | ||
} | ||
{ | ||
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); | ||
service.UseDefaultCredentials = exchange.UseDefaultCredentials; | ||
service.Credentials = exchange.Username.HasText() ? new WebCredentials(exchange.Username, exchange.Password) : null; | ||
//service.TraceEnabled = true; | ||
//service.TraceFlags = TraceFlags.All; | ||
|
||
if (exchange.Url.HasText()) | ||
service.Url = new Uri(exchange.Url); | ||
else | ||
service.AutodiscoverUrl(email.From.EmailAddress, RedirectionUrlValidationCallback); | ||
|
||
EmailMessage message = new EmailMessage(service); | ||
|
||
foreach (var a in email.Attachments.Where(a => a.Type == EmailAttachmentType.Attachment)) | ||
{ | ||
var fa = message.Attachments.AddFileAttachment(a.File.FileName, a.File.GetByteArray()); | ||
fa.ContentId = a.ContentId; | ||
} | ||
message.ToRecipients.AddRange(email.Recipients.Where(r => r.Kind == EmailRecipientKind.To).Select(r => r.ToEmailAddress()).ToList()); | ||
message.CcRecipients.AddRange(email.Recipients.Where(r => r.Kind == EmailRecipientKind.Cc).Select(r => r.ToEmailAddress()).ToList()); | ||
message.BccRecipients.AddRange(email.Recipients.Where(r => r.Kind == EmailRecipientKind.Bcc).Select(r => r.ToEmailAddress()).ToList()); | ||
message.Subject = email.Subject; | ||
message.Body = new MessageBody(email.IsBodyHtml ? BodyType.HTML : BodyType.Text, email.Body.Text); | ||
message.Send(); | ||
} | ||
|
||
protected virtual bool RedirectionUrlValidationCallback(string redirectionUrl) | ||
{ | ||
// The default for the validation callback is to reject the URL. | ||
Uri redirectionUri = new Uri(redirectionUrl); | ||
// Validate the contents of the redirection URL. In this simple validation | ||
// callback, the redirection URL is considered valid if it is using HTTPS | ||
// to encrypt the authentication credentials. | ||
return redirectionUri.Scheme == "https"; | ||
} | ||
} | ||
|
||
public static class ExchangeExtensions | ||
{ | ||
public static EmailAddress ToEmailAddress(this EmailAddressEmbedded address) | ||
{ | ||
if (address.DisplayName.HasText()) | ||
return new EmailAddress(address.DisplayName, address.EmailAddress); | ||
|
||
return new EmailAddress(address.EmailAddress); | ||
} | ||
|
||
public static EmailAddress ToEmailAddress(this EmailRecipientEmbedded recipient) | ||
{ | ||
if (!EmailLogic.Configuration.SendEmails) | ||
throw new InvalidOperationException("EmailConfigurationEmbedded.SendEmails is set to false"); | ||
|
||
if (recipient.DisplayName.HasText()) | ||
return new EmailAddress(recipient.DisplayName, EmailLogic.Configuration.OverrideEmailAddress.DefaultText(recipient.EmailAddress)); | ||
|
||
return new EmailAddress(EmailLogic.Configuration.OverrideEmailAddress.DefaultText(recipient.EmailAddress)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
d9d219a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cleanup in EmailSenderConfigurationEntity
This refactoring made by @rezanos (Thanks!) splits the different embedded entities in
EmailSenderConfigurationEntity
into a properImpementedBy
of different part entities inheriting from the abstract classEmailServiceEntity
:BEFORE
AFTER
Not many changes are expected in your code, except for fixing EnvironmentTest or some CSharp Migrations manually.
The SQL Migration however is a little bit complicated, so here is the script that migrates the information from the embedded to the new part entities that needs to be inserted appropiately in your SQL Script.
Additionally,
EmailMessageEntity
has a newSentBy
fields pointing to theEmailSenderConfigurationEntity
that was used. This script will work only in the simple case of having oneEmailSenderConfigurationEntity
. Otherwise, keep the field null or think is something better for your application.Conclusion
This change makes has three benefits:
EmailServiceEntity
and implement your own logic inheriting fromBaseEmailSender
and registering it inEmailLogic.EmailSenders
.Enjoy!
d9d219a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks to you @olmobrutall for your always great collaboration 😃 🙏
🚀 🎉