Sending a Form by Email
Our sample projects and report templates can help you learn the basics of working with our products.Personalize a form for each recipient, export every copy in memory, and send it as a PDF attachment.
Preparing each message. For every recipient, the code updates the form label and exports a fresh PDF with that person’s data.
Attaching the document. The mail loop in
Fresh streams. A separate attachment stream is created for every message, preventing one recipient’s document from being reused for another.
Preparing each message. For every recipient, the code updates the form label and exports a fresh PDF with that person’s data.
Attaching the document. The mail loop in
Sending a Form by Email/Program.cs creates the message and attaches the in-memory PDF before sending it:static Hashtable Recipients = new Hashtable()
{
["Alex@gmail.com"] = "Alex",
["Tom@gmail.com"] = "Tom",
// ...more recipients
};
var smtpClient = new SmtpClient("smtp.gmail.com")
{
Port = 587,
Credentials = new NetworkCredential("username", "password"),
EnableSsl = true,
};
var form = new StiForm();
form.Load(@"Data\CustomerSatisfactionSurvey.mrt");
var label = form.GetElementByName("RecipientLabel") as StiLabelElement;
var settings = new StiPdfExporterSettings { UsePdfA = false, ReadOnly = false };
var pdfExporter = new StiPdfExporter(settings);
foreach (string mail in Recipients.Keys)
{
label.Text.Expression = $"Hello, {Recipients[mail]}";
var pdf = pdfExporter.ExportForm(form);
var mailMessage = new MailMessage
{
From = new MailAddress("mail@gmail.com"),
Subject = "subject",
Body = "<h1>Hello</h1>",
IsBodyHtml = true,
};
mailMessage.To.Add(mail);
mailMessage.Attachments.Add(new Attachment(new MemoryStream(pdf), MediaTypeNames.Application.Pdf));
smtpClient.Send(mailMessage);
}Fresh streams. A separate attachment stream is created for every message, preventing one recipient’s document from being reused for another.