public bool EmailBarcodeFile(FastTrackOptions options, IBarcodeFileGenerator barcodeFileGenerator) { try { if (options == null || barcodeFileGenerator == null || string.IsNullOrWhiteSpace(options.Email)) return false; EmailOptions emailOptions = new EmailOptions() { To = new List<string>(), From = "*****@*****.**", Subject = "GeneratedBarcode" + DateTime.Now.ToString("dd MMM yyyy HH mm") + ".pdf", AttachmentStreams = new List<AttachmentStream>() }; StringBuilder emailBody = new StringBuilder(); emailBody.Append(string.Format("Generated {0} barcode {1} for {2}.\n", options.Quantity, (options.Quantity > 1 ? "s " : " "), options.Name)); emailBody.Append(string.Format("Departure Airport: {0}\n", options.DepartureAirport)); emailBody.Append(string.Format("Destination Airport: {0}\n", options.DestinationAirport)); emailBody.Append(string.Format("User Type: {0}\n", options.UserType)); emailBody.Append(string.Format("Ticket Type: {0}\n", options.TicketType)); emailBody.Append(string.Format("From {0} to {1}", options.StartDate, options.EndDate)); emailOptions.Body = emailBody.ToString(); emailOptions.To.Add(options.Email); emailOptions.AttachmentStreams.Add(new AttachmentStream() { FileStream = new MemoryStream(barcodeFileGenerator.GenerateFile(options).ToArray()), FileName = emailOptions.Subject }); _emailer.Send(emailOptions); return true; } catch (Exception ex) { return false; } }
public void Send(EmailOptions options) { MailMessage email = new MailMessage(); //SmtpClient SmtpServer = new SmtpClient("mail.mhaltd.co.uk"); SmtpClient SmtpServer = new SmtpClient(Properties.Settings.Default.SMTPClient); //email.From = new MailAddress("*****@*****.**"); email.From = new MailAddress(Properties.Settings.Default.EmailFrom); foreach (string toEmail in options.To) { email.To.Add(toEmail); } email.Subject = options.Subject; email.Body = options.Body; // If any string attachments if(options.AttachmentLocations != null && options.AttachmentLocations.Count > 0) { foreach(string attachment in options.AttachmentLocations) { email.Attachments.Add(new Attachment(attachment)); } } // If any stream attachments if (options.AttachmentStreams != null && options.AttachmentStreams.Count > 0) { foreach (AttachmentStream attachment in options.AttachmentStreams) { email.Attachments.Add(new Attachment(attachment.FileStream, attachment.FileName)); } } // Send Email SmtpServer.Send(email); /* * This worked with SCM, but not got permission to send as this person using (SmtpClient smtp = new SmtpClient("mail.mhaltd.co.uk", 25)) { smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential("noreply", "Mach453237!"); smtp.EnableSsl = false; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Send(email); }*/ }