Page 1 of 1

Angular 7 + .Net core 2 send report as email attachment

Posted: Mon Apr 22, 2019 8:56 am
by mrapi
Hi
Using Angular 7 + .Net core 2 it is possible to send a report as email attachment?
I found a sample https://github.com/stimulsoft/Samples-N ... by%20Email of you but it is for only .NET Core MVC
thanks

Re: Angular 7 + .Net core 2 send report as email attachment

Posted: Mon Apr 22, 2019 7:37 pm
by HighAley
Hello.

You could export the report from .Net Core or Angular.
In the first case, the .Net Core engine will be used.
In the second case, the JavaScript engine will be used.
We have one sample for your compination of the technologies only.
https://github.com/stimulsoft/Samples-N ... %20Angular

Thank you.

Re: Angular 7 + .Net core 2 send report as email attachment

Posted: Tue Apr 23, 2019 6:31 am
by mrapi
Hi
I've started from this sample https://github.com/stimulsoft/Samples-J ... by%20Email
and seems to work,just couple things:
how can I set default values from code for fields in email options window:
Image
thnks

Re: Angular 7 + .Net core 2 send report as email attachment

Posted: Tue Apr 23, 2019 7:30 am
by mrapi
I'found in manual:

Code: Select all

options.email.defaultEmailAddress = "recipient_address@gmail.com";
options.email.defaultEmailSubject = "New Invoice";
options.email.defaultEmailMessage = "Please check the new invoice in the attachment";
thanks

Re: Angular 7 + .Net core 2 send report as email attachment

Posted: Tue Apr 23, 2019 7:41 am
by mrapi
that's our final code in case someone needs it:
js:

Code: Select all

this.options.toolbar.showSendEmailButton = true;

  this.viewer.onEmailReport = function (args) {
            var xhr = new XMLHttpRequest();

            xhr.onreadystatechange = function () {
              if (this.readyState === 4) { // this.status
                if (this.responseText === 'OK') {
                  alert("Message sent!");
                }
                else {
                  alert(this.responseText);
                }
              }
            };

            xhr.open('POST', '/api/sendmail', true);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.send(JSON.stringify({
              fileName: args.fileName,
              format: args.format,
              data: Stimulsoft.System.Convert.toBase64String(args.data),
              email: args.settings.email,
              subject: args.settings.subject,
              message: args.settings.message
            }));
          };

       }


C#:

Code: Select all

    public class clientMailData
    {
        public string fileName { get; set; }
        public string format { get; set; }
        public string data { get; set; }
        public string email { get; set; }
        public string subject { get; set; }
        public string message { get; set; }
    }
    public class stiEmailOptions
    {
        public stiEmailOptions()
        {
            AddressFrom = string.Empty;
            AddressTo = string.Empty;
            Host = "localhost";
            Body = string.Empty;
            Subject = string.Empty;
            Port = 25;
            BCC = new ArrayList();
            CC = new ArrayList();
        }
        public string AddressFrom { get; set; }
        public string AddressTo { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public string Host { get; set; }
        public int Port { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public bool EnableSsl { get; set; }
        public ArrayList CC { get; set; }
        public ArrayList BCC { get; set; }
    }
    public class emailController : Controller
    {
        [Route("/api/sendmail")]
        [HttpPost]
        public IActionResult sendEmail([FromBody] clientMailData cld)
        {
            var data = cld.data;
            var stream = new MemoryStream(Convert.FromBase64String(data));

            var attachmentName = cld.fileName;
            var format = cld.format;
            switch (format)
            {
                case "Pdf":
                    attachmentName += ".pdf";
                    break;
                case "Html":
                case "Html5":
                    attachmentName += ".html";
                    break;
                case "Word2007":
                    attachmentName += ".docx";
                    break;
                case "Excel2007":
                    attachmentName += ".xlsx";
                    break;
                case "Csv":
                    attachmentName += ".csv";
                    break;
            }

         

            var options = new stiEmailOptions
            {   AddressTo = cld.email,
                Subject = cld.subject,
                Body = cld.message,

                AddressFrom = ".....",
                Host = ".....", //smtp 
                Port = 25,
                UserName = ".....",
                EnableSsl = true,
                Password = "....."
            };

            return Ok(sendReportEmail(stream, attachmentName, options));
        }
        public static string sendReportEmail(Stream stream, string attachmentName, stiEmailOptions options)
        {
            var result = "OK";

            try
            {
                stream.Seek(0, SeekOrigin.Begin);

            var attachment = new Attachment(stream, attachmentName);
            var message = new MailMessage(options.AddressFrom, options.AddressTo);
            message.Attachments.Add(attachment);
            message.Subject = options.Subject;
            foreach (object email in options.CC)
            {
              if (email is MailAddress)
                    message.CC.Add((MailAddress)email);
              else
                    message.CC.Add((string)email);
            }
            foreach (object email in options.BCC)
            {
                if (email is MailAddress)
                    message.Bcc.Add((MailAddress)email);
                else
                    message.Bcc.Add((string)email);
            }
            if (string.IsNullOrEmpty(options.Body))  
                message.Body =  string.Format("This Email contains the '{0}' report file.", attachmentName);
            else 
                message.Body = options.Body;
            
            SmtpClient client = new SmtpClient(options.Host, options.Port);
            client.EnableSsl = options.EnableSsl;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(options.UserName, options.Password);

                client.Send(message);
            }
            catch (Exception e)
            {
                result = e.InnerException != null ? e.InnerException.Message : e.Message;
            }

            return result;
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
            }
            base.Dispose(disposing);
        }

    }

Re: Angular 7 + .Net core 2 send report as email attachment

Posted: Thu Apr 25, 2019 5:02 am
by Andrew
Hello,

Thank you for sharing your code!

Re: Angular 7 + .Net core 2 send report as email attachment

Posted: Thu May 02, 2019 8:45 am
by mrapi
you're welcome

Re: Angular 7 + .Net core 2 send report as email attachment

Posted: Fri May 03, 2019 2:43 pm
by Lech Kulikowski
Hello

Thank you.
Please let us know if you need any additional help.