Sending email

Hi all

Need code that enable me to send report as email attachment without using fastreport preview control wizard and filling dialog form requested data


Thanks

Comments

  • edited April 2017
    sending email is a long running process and you dont want to block the UI, use background thread.
    using System.Threading.Tasks;
    using System.Net;
    using System.Net.Mail;
    
    TaskScheduler ts = TaskScheduler.FromCurrentSynchronizationContext();
    Task.Run(() =>
    {
        using (SmtpClient smtp = new SmtpClient())
        {
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.Port = 587;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential("mymailaddress@gmail.com", "password");
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            using (MailMessage message = new MailMessage("mymailaddress@gmail.com", "recipient@mail.com", "subject", "content"))
            {
                message.IsBodyHtml = false;
                message.Priority = MailPriority.High;
                using (Attachment data = new Attachment(@"d:\attachedfile.txt"))
                {
                    message.Attachments.Add(data);
                    smtp.Send(message);
                }
            }
        }
    }).ContinueWith(t =>
                    {
                        MessageBox.Show("Delivered...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }, ts);
    

Leave a Comment