private void SendEmail() { try { // Build message MailMessage msg = new MailMessage(); msg.From = txtFrom.Text; msg.Subject = txtSubject.Text; msg.HtmlBody = txtHtmlBody.Text; msg.To.Add(new MailAddress("#Recipient#", true)); // create a message template engine with the above message TemplateEngine msgTemplateEngine = new TemplateEngine(msg); // create data table for the template DataTable dt = new DataTable(); // Add 3 columns dt.Columns.Add("Recipient", typeof(string)); dt.Columns.Add("FirstName", typeof(string)); dt.Columns.Add("LastName", typeof(string)); // add rows to the table DataRow row; foreach (ListItem item in lstRecipients.Items) { string recipientEmail = item.Text; string firstName = item.Value.Split(delimeter, StringSplitOptions.None)[0]; string lastName = item.Value.Split(delimeter, StringSplitOptions.None)[1]; // add rows to the data table row = dt.NewRow(); row["Recipient"] = recipientEmail; row["FirstName"] = firstName; row["LastName"] = lastName; dt.Rows.Add(row); } MailMessageCollection msgCollection; msgCollection = msgTemplateEngine.Instantiate(dt); // add recipients // Send message SmtpClient smtp = new SmtpClient( txtHost.Text, int.Parse(txtPort.Text), txtUsername.Text, txtPassword.Text); // SSL settings if (chSSL.Checked == true) { smtp.EnableSsl = true; smtp.SecurityMode = SmtpSslSecurityMode.Explicit; } // send bulk emails smtp.BulkSend(msgCollection); // show message on screen that email has been sent lblMessage.ForeColor = System.Drawing.Color.Green; lblMessage.Text = "Email sent successfully<br><hr>"; } catch (Exception ex) { // display error message lblMessage.ForeColor = System.Drawing.Color.Red; lblMessage.Text = "Error: " + ex.Message + "<br><hr>"; } }
public static void Run() { // The path to the documents directory. string dataDir = RunExamples.GetDataDir_SMTP(); string dstEmail = dataDir + "EmbeddedImage.msg"; //Create a new MailMessage instance MailMessage msg = new MailMessage(); //Add subject and from address msg.Subject = "Hello, #FirstName#"; msg.From = "*****@*****.**"; //Add email address to send email msg.To.Add("*****@*****.**"); //Add mesage field to HTML body msg.HtmlBody = "Your message here"; msg.HtmlBody += "Thank you for your interest in <STRONG>Aspose.Email</STRONG>."; //Use GetSignment as the template routine, which will provide the same signature msg.HtmlBody += "<br><br>Have fun with it.<br><br>#GetSignature()#"; //Create a new TemplateEngine with the MSG message. TemplateEngine engine = new TemplateEngine(msg); // Register GetSignature routine. It will be used in MSG. engine.RegisterRoutine("GetSignature", new TemplateRoutine(GetSignature)); //Create an instance of DataTable //Fill a DataTable as data source DataTable dt = new DataTable(); dt.Columns.Add("Receipt", typeof(string)); dt.Columns.Add("FirstName", typeof(string)); dt.Columns.Add("LastName", typeof(string)); //Create an instance of DataRow DataRow dr; dr = dt.NewRow(); dr["Receipt"] = "abc<*****@*****.**>"; dr["FirstName"] = "a"; dr["LastName"] = "bc"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Receipt"] = "John<*****@*****.**>"; dr["FirstName"] = "John"; dr["LastName"] = "Doe"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Receipt"] = "Third Recipient<*****@*****.**>"; dr["FirstName"] = "Third"; dr["LastName"] = "Recipient"; dt.Rows.Add(dr); MailMessageCollection messages; try { //Create messages from the message and datasource. messages = engine.Instantiate(dt); //Create an instance of SmtpClient and specify server, port, username and password SmtpClient client = GetSmtpClient(); //Send messages in bulk client.BulkSend(messages); } catch (MailException ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } catch (SmtpException ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } Console.WriteLine(Environment.NewLine + "Message sent after performing mail merge."); }