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

Stimulsoft Reports.JS discussion
Post Reply
mrapi
Posts: 277
Joined: Sat Dec 20, 2008 1:08 am

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

Post 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
HighAley
Posts: 8431
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

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

Post 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.
mrapi
Posts: 277
Joined: Sat Dec 20, 2008 1:08 am

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

Post 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
mrapi
Posts: 277
Joined: Sat Dec 20, 2008 1:08 am

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

Post 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
mrapi
Posts: 277
Joined: Sat Dec 20, 2008 1:08 am

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

Post 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);
        }

    }
Andrew
Posts: 4104
Joined: Fri Jun 09, 2006 3:58 am

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

Post by Andrew »

Hello,

Thank you for sharing your code!
mrapi
Posts: 277
Joined: Sat Dec 20, 2008 1:08 am

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

Post by mrapi »

you're welcome
Lech Kulikowski
Posts: 6247
Joined: Tue Mar 20, 2018 5:34 am

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

Post by Lech Kulikowski »

Hello

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