Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > How to get email template attachments from code View modes: 
User avatar
Member
Member
joeh42 - 12/1/2011 2:46:14 PM
   
How to get email template attachments from code
I have an email template, and I send it via code to some users. Works great.

I want to add an attachment to the email template. So I want into the email template and clicked attach. I upload the PDF, and hit save. It now shows beneath the email template.

When I send the email from the code, there is no attachment.
    private void SendEmail(UserInfo userInfo, string emailTemplateName)
{
EmailMessage msg = new CMS.EmailEngine.EmailMessage();
EmailTemplateInfo eti = EmailTemplateProvider.GetEmailTemplate(emailTemplateName, CMSContext.CurrentSiteID);

MacroResolver mcr = new MacroResolver();
mcr.SpecialMacros = new String[,] { { "#firstname#", userInfo.FirstName }, { "#lastname#", userInfo.LastName } };

msg.EmailFormat = EmailFormatEnum.Default;
msg.From = eti.TemplateFrom;
msg.Recipients = userInfo.Email;
msg.Body = CMSContext.CurrentResolver.ResolveMacros(msg.Body);
msg.Subject = eti.TemplateSubject;

EmailSender.SendEmailWithTemplateText(CMSContext.CurrentSiteName, msg, eti, mcr, true);
}

I see that there is EmailMessage.Attachments, but how can I set that field to the attachment from the template?

Thanks,
Joe

User avatar
Member
Member
kentico_michal - 12/5/2011 12:53:14 AM
   
RE:How to get email template attachments from code
Hello Joe,

You need to execute the MetaFileInfoProvider.ResolveMetaFileImages method before you send the email:
// Add template metafiles to e-mail
MetaFileInfoProvider.ResolveMetaFileImages(msg, eti.TemplateID, EmailObjectType.EMAILTEMPLATE, MetaFileInfoProvider.OBJECT_CATEGORY_TEMPLATE);

EmailSender.SendEmailWithTemplateText(CMSContext.CurrentSiteName, msg, eti, mcr, true);

Best regards,
Michal Legen

User avatar
Certified Developer v7
Certified  Developer v7
Vaclav.Novak-sprinx - 1/21/2013 7:10:27 AM
   
RE:How to get email template attachments from code
Hi Michal,

I have same problem as joeh. Code, which you posted above doesn't work for me. I seriously doubt about attaching file with extension pdf by calling function with suffix Images...

Could you post working code please?

My Kentico version is 6.

Thanks
Václav

User avatar
Certified Developer v7
Certified  Developer v7
Vaclav.Novak-sprinx - 1/21/2013 8:06:30 AM
   
RE:How to get email template attachments from code
Hi,
I already solved my issue. Problem was in wrong email template id. Sorry Michal.

Could you rename ResolveMetaFileImages to ResolveMetaFiles in next version? It will be helpfull.

Thanks.

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 1/22/2013 2:00:23 AM
   
RE:How to get email template attachments from code
Hi,

the metafiles are 99% images. Also, we prefer not to change methods if it is not required due to some architecture or performance changes since it makes the upgrades and backward compatibility harder. We already changed the API in some upgrades and some customers were pretty upset about it - so at this moment we try to change as less things as possible.

Best regards,
Juraj Ondrus