0 votes

Hi,

is it possible to create a mail with a table (like in the template sample), but with a attachment in each line (referenced e.x. in the data source as a UNC path etc)?

It should look somehow like this:


| Name | Type | Attachment |

| acb | xls | abc.xls |

| xyz | txt | xyz.txt |

Thanks in advance

by (300 points)

1 Answer

0 votes

The code below should help. It uses Mail.dll email template engine and MeilBuilder class to create an email with rendered template and attachments:

public class AttchmentInfo
{
    public string FullPath;
    public string Name => Path.GetFileNameWithoutExtension(FullPath);
    public string Extension => Path.GetExtension(FullPath);
}

public class TemplateData
{
    public List<AttchmentInfo> Attachments;
};

[Test]
public void Method_Condition_Result()
{

    string template = @"<table>
        [foreach Attachments]
        <tr>
            <td>[Name]</td>
            <td>[Extension]</td>
        </tr>
        [end]

        </table>";


    TemplateData templateData = new TemplateData
    {
        Attachments = new List<AttchmentInfo>
        {
            new AttchmentInfo {FullPath = "c:\\Test.pdf"},
            new AttchmentInfo {FullPath = "c:\\Test2.pdf"},
        }
    };
    string rendered = Template.Create(template)
        .DataFrom(data)
        .Render();

    var builder = new MailBuilder();
    builder.Subject = "Subject";
    builder.From.Add(new MailBox("from@example.com"));
    builder.To.Add(new MailBox("from@example.com"));
    builder.Html = rendered;

    foreach (var info in templateData.Attachments)
    {
        builder.AddAttachment(info.FullPath);
    }
    IMail mail = builder.Create();

}
by (297k points)
Attachments inline in table column
...