Skip to content
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

Support for embedded images. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions Mailzor/Email.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -23,6 +25,8 @@ public class Email:IDisposable
public dynamic ViewBag { get; set; }

public List<Attachment> Attachments { get; } = new List<Attachment>();
public List<LinkedResource> LinkedResources { get; } = new List<LinkedResource>();

private string FromMailAddress { get; set; }
private string FromDisplayName { get; set; }

Expand Down Expand Up @@ -99,6 +103,24 @@ public void SetFrom(string mailAddress, string displayName = null)
FromDisplayName = displayName;
}

public LinkedResource AddImageResource(Stream stream, string contentId, string contentType)
{
return AddImageResource(stream, contentId, new ContentType(contentType));
}

public LinkedResource AddImageResource(Stream stream, string contentId, ContentType contentType)
{
var imageResource = new LinkedResource(stream, MediaTypeNames.Image.Jpeg)
{
ContentId = contentId,
ContentType = contentType
};

LinkedResources.Add(imageResource);

return imageResource;
}

public void Dispose()
{
_smtpClient.Dispose();
Expand Down Expand Up @@ -126,6 +148,10 @@ private MailMessage ConfigureEmail(MailAddress[] toMails, string subject, MailAd
email.Attachments.Add(attachment);
}

var view = AlternateView.CreateAlternateViewFromString(emailHtmlBody, null, MediaTypeNames.Text.Html);
LinkedResources?.ForEach(res => view.LinkedResources.Add(res));
email.AlternateViews.Add(view);

foreach (var toMail in toMails ?? Enumerable.Empty<MailAddress>())
{
email.To.Add(toMail);
Expand Down
5 changes: 5 additions & 0 deletions Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ private static void NormalSenario()
// set Attachments
email.Attachments.Add(new Attachment("Attachments/attach1.pdf"));
email.Attachments.Add(new Attachment("Attachments/attach2.docx"));

// set image resource
var fileStream = new FileStream("Views/Emails/microsoft.png", FileMode.Open);
email.AddImageResource(fileStream, "microsoft.png", "image/png");

// set your desired display name (Optional)
email.SetFrom("test@outlook.com","King Of Mail Zone");
// send it
Expand Down
2 changes: 1 addition & 1 deletion Test/Views/Emails/hello.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<hr>

<img src="microsoft.png" alt="" />
<img src="cid:microsoft.png" alt="" height="150" />

<hr />

Expand Down