Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > Email attachment View modes: 
User avatar
Certified Developer 8
Certified Developer 8
richard - 2/20/2012 12:31:04 PM
   
Email attachment
I am attempting to attach a file (a pdf that is stored in the database as a path) to an email but am unsure how to do it.

The URL path is stored in the database like this: ~/getmetafile/8d6b2163-0312-4b3f-9a28-facf19acc842/SecurePay_test_details-(4) (but I have tried ~/cmspages/getmetafile.aspx?fileguid=8d6b2163-0312-4b3f-9a28-facf19acc842) both of these return the file fine in the browser.

I have tried a number of approaches such as MetaFileInfoProvider.GetFileBinary and Stream (returns a null byte array) and MetaFileInfo mfi = MetaFileInfoProvider.GetMetaFileInfo(fileGuid,CMSContext.CurrentSiteName,true).InputStream (this is null also)

I have tried AttachmentManager.GetFilePhysicalPath but this returns an error (Could not load file or assembly 'CMS.GlobalHelper, Version=5.5.3996.30246, Culture=neutral, PublicKeyToken=834b12a258f213f9' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040))

I am really at a loss as to how to get the attachment onto the email.

Version v6.0.4297

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 2/29/2012 6:29:52 AM
   
RE:Email attachment
Hi,

here is a code example which attached a file to the e-mail.

 
protected void Button2_Click1(object sender, EventArgs e)
{
EmailMessage msg = new CMS.EmailEngine.EmailMessage();
EmailTemplateInfo eti = EmailTemplateProvider.GetEmailTemplate("template.codename", CMSContext.CurrentSiteID);

MacroResolver mcr = new MacroResolver();

msg.EmailFormat = EmailFormatEnum.Both;
msg.From = eti.TemplateFrom;
msg.Recipients = "to@somebody.com";
msg.Subject = eti.TemplateSubject;
System.Net.Mail.Attachment item = new System.Net.Mail.Attachment("filePath");
msg.Attachments.Add(item);

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


Best regards,
Ivana Tomanickova

User avatar
Certified Developer 8
Certified Developer 8
richard - 2/29/2012 1:06:24 PM
   
RE:Email attachment
Thanks Ivana

I had problems with this code (due to virtual paths I think) but i resolved the issue.



//metafile path as stored in database (in this case I am using SKUImagePath to store a pdf file)
string menuurl = "~/getmetafile/8d6b2163-0312-4b3f-9a28-facf19acc842/SecurePay_test_details-(4)"

//split url into array to extract the guid
string[] arrMenuUrl = menuurl.Split('/');

Guid gu = ValidationHelper.GetGuid(arrMenuUrl[2], Guid.Empty);

if (gu == Guid.Empty)
{
//whoops
return;
}

//get byte array from metafile
byte[] b = MetaFileInfoProvider.GetFile(gu, CMSContext.CurrentSiteName);

//push to stream
Stream s = new MemoryStream(b);

System.Net.Mail.Attachment item = new System.Net.Mail.Attachment(s,"menu.pdf");

emailMessage.Attachments.Add(item);

//send email