private void materialFlatButton1_Click_1(object sender, EventArgs e)
        {
            try
            {
                EmailAttachments emailAttachments = new EmailAttachments();
                var attach       = emailAttachments.BrowseForFile();
                var notAddedList = new List <string>();
                if (attach != (null, null))
                {
                    foreach (var files in attach.Item1.Zip(attach.Item2, (paths, names) => (paths, names)))
                    {
                        var bytes = File.ReadAllBytes(files.paths);
                        var file  = Convert.ToBase64String(bytes);
                        var type  = emailAttachments.GetMIMEType(files.paths);
                        if (type == null)
                        {
                            notAddedList.Add(files.names);
                            continue;
                        }
                        SendGrid.Helpers.Mail.Attachment attachment = new SendGrid.Helpers.Mail.Attachment()
                        {
                            Content  = file,
                            Filename = files.names,
                            Type     = type
                        };
                        attachmentList.Add(attachment);
                    }
                    if (attachmentList.Count >= 1)
                    {
                        AttachmentsGrid.DataSource = null;
                        AttachmentsGrid.DataSource = attachmentList;
                        AttachmentsGrid.Columns["Content"].Visible     = false;
                        AttachmentsGrid.Columns["Type"].Visible        = false;
                        AttachmentsGrid.Columns["ContentId"].Visible   = false;
                        AttachmentsGrid.Columns["Disposition"].Visible = false;
                    }
                    if (notAddedList.Count >= 1)
                    {
                        string list = null;

                        foreach (var x in notAddedList)
                        {
                            list += x + " ";
                        }
                        var msg = $"Súbor/y {list} nebol pridaný, lebo tento typ súboru nie je možné odoslať emailom. Pre informácie o tom, ktoré " +
                                  "súbory nie je možné odoslať otvorte BlockedExtensions.txt";
                        MessageBox.Show(msg);
                    }
                    AttachmentsGrid.ClearSelection();
                }
                else
                {
                    return;
                }
            }
Example #2
0
        // Vytvorenie zoznamu blokovaných extensions súborov
        private void CreateBlockedExtensionFile()
        {
            var path = AppDomain.CurrentDomain.BaseDirectory + "/BlockedExtensions.txt";

            if (!File.Exists(path))
            {
                using (StreamWriter sw = File.AppendText(path))
                {
                    EmailAttachments attachments = new EmailAttachments();
                    foreach (var line in attachments.blockedExtensions)
                    {
                        sw.WriteLine(line);
                    }
                }
            }
        }
        //private void materialFlatButton1_Click(object sender, EventArgs e)
        //{
        //    this.Close();
        //}

        private void AddEmailTemplateAttachmentsBtn_Click(object sender, EventArgs e)
        {
            try
            {
                EmailAttachments emailAttachments = new EmailAttachments();
                var attach       = emailAttachments.BrowseForFile();
                var notAddedList = new List <string>();
                if (attach != (null, null))
                {
                    foreach (var files in attach.Item1.Zip(attach.Item2, (paths, names) => (paths, names)))
                    {
                        var type = emailAttachments.GetMIMEType(files.paths);
                        if (type == null)
                        {
                            notAddedList.Add(files.names);
                            continue;
                        }

                        Attachment attachment = new Attachment()
                        {
                            FilePath = files.paths,
                            FileName = files.names,
                            IdUser   = currentUser.Id,
                        };

                        if (editMode)
                        {
                            using (StudentDBDataContext con = new StudentDBDataContext(conn_str))
                            {
                                var att = attachment;
                                att.IdEmailTemplate = (int)TempGridView.CurrentRow.Cells[2].Value;
                                var emailTemp = con.GetTable <EmailTemplate>().First(x => x.Id == (int)TempGridView.CurrentRow.Cells[2].Value);
                                if (!AttachmentExists(att, emailTemp))
                                {
                                    con.Attachments.InsertOnSubmit(att);
                                    con.SubmitChanges();

                                    AttachmentsGrid.DataSource = null;
                                    AttachmentsGrid.DataSource = con.GetTable <Attachment>().Where(x => x.IdEmailTemplate == (int)TempGridView.CurrentRow.Cells[2].Value);
                                    AttachmentsGrid.Columns["FilePath"].Visible        = false;
                                    AttachmentsGrid.Columns["IdUSer"].Visible          = false;
                                    AttachmentsGrid.Columns["IdEmailTemplate"].Visible = false;
                                    AttachmentsGrid.Columns["Id"].Visible            = false;
                                    AttachmentsGrid.Columns["EmailTemplate"].Visible = false;
                                    AttachmentsGrid.Columns["User"].Visible          = false;

                                    if (notAddedList.Count >= 1)
                                    {
                                        string list = null;

                                        foreach (var x in notAddedList)
                                        {
                                            list += x + " ";
                                        }
                                        var msg = $"Súbor/y {list} nebol pridaný, lebo tento typ súboru nie je možné odoslať emailom. Pre informácie o tom, ktoré " +
                                                  "súbory nie je možné odoslať otvorte BlockedExtensions.txt";
                                        MessageBox.Show(msg);
                                    }
                                }
                                return;
                            }
                        }
                        else if (!attachmentList.Contains(attachment))
                        {
                            attachmentList.Add(attachment);
                        }
                    }
                    if (attachmentList.Count >= 1)
                    {
                        AttachmentsGrid.DataSource = null;
                        AttachmentsGrid.DataSource = attachmentList;
                        AttachmentsGrid.Columns["FilePath"].Visible        = false;
                        AttachmentsGrid.Columns["IdUSer"].Visible          = false;
                        AttachmentsGrid.Columns["IdEmailTemplate"].Visible = false;
                        AttachmentsGrid.Columns["Id"].Visible            = false;
                        AttachmentsGrid.Columns["EmailTemplate"].Visible = false;
                        AttachmentsGrid.Columns["User"].Visible          = false;
                    }
                    if (notAddedList.Count >= 1)
                    {
                        string list = null;

                        foreach (var x in notAddedList)
                        {
                            list += x + " ";
                        }
                        var msg = $"Súbor/y {list} nebol pridaný, lebo tento typ súboru nie je možné odoslať emailom. Pre informácie o tom, ktoré " +
                                  "súbory nie je možné odoslať otvorte BlockedExtensions.txt";
                        MessageBox.Show(msg);
                    }
                }
                else
                {
                    return;
                }
                AttachmentsGrid.ClearSelection();
            }
Example #4
0
        private async Task <Response> SendEmails(EmailTemplate emailTemplate, int numberOfDays, List <EmailAddress> emailAddresses, User user, DateTime date)
        {
            try
            {
                EmailClient    eClient = new EmailClient();
                SendGridClient client  = new SendGridClient(eClient.SetEnvironmentVar(user));
                List <SendGrid.Helpers.Mail.Attachment> attachmentList = new List <SendGrid.Helpers.Mail.Attachment>();
                EmailAttachments emailAttachments = new EmailAttachments();
                EmailBody        body             = new EmailBody()
                {
                    /// HtmlContent je celkovo obsah emailu, <br/> je nutne pridat kvoli tomu, aby email obsahoval nove riadky. Na konci je priadnie
                    /// emailoveho podpisu.
                    HtmlContent = emailTemplate.EmailContent.Replace("\u00A0", "<br/>") + "<br/> <br/> " + user.Signature.Replace("\u00A0", "<br/>"),
                    Subject     = emailTemplate.EmailSubject,
                    To          = emailAddresses
                };

                using (StudentDBDataContext con = new StudentDBDataContext(conn_str))
                {
                    var attachments = con.GetTable <Attachment>().Where(x => x.IdUser == user.Id && x.IdEmailTemplate == emailTemplate.Id);

                    foreach (var files in attachments)
                    {
                        /// Zistovanie, ci subor, ktoreho cestu budeme brat z DB vobec existuje
                        if (File.Exists(files.FilePath))
                        {
                            var bytes = File.ReadAllBytes(files.FilePath);
                            var file  = Convert.ToBase64String(bytes);
                            // Ziskanie typu suboru, aby mohol byt uploadnuty
                            var type = emailAttachments.GetMIMEType(files.FilePath);

                            /// Pridanie prilohy,
                            SendGrid.Helpers.Mail.Attachment attachment = new SendGrid.Helpers.Mail.Attachment()
                            {
                                Content  = file,
                                Filename = (string)files.FileName,
                                Type     = type
                            };
                            attachmentList.Add(attachment);
                        }
                        else
                        {
                            continue;
                        }
                    }
                }

                /// Vytvorenie emailu pre viacero prijemcov
                var msg = MailHelper.CreateSingleEmailToMultipleRecipients(MailHelper.StringToEmailAddress(user.Email), body.To, body.Subject, body.HtmlContent, body.HtmlContent);

                // Dates je premenna, ktora urcuje o kolko dni skor ma byt odoslana sprava
                var dates = date.AddDays(-numberOfDays);
                // Prevod z datumu do Unix sekund ( Sendgrid takto akceptuje parameter SendAt
                var unixDate = new DateTimeOffset(dates.Year, dates.Month, dates.Day, dates.Hour, dates.Minute, dates.Second, TimeSpan.Zero).ToUnixTimeSeconds();
                /// Minus jedna hodina kvoli casovemu posunu Web API
                msg.SetSendAt((int)unixDate - 3600);
                if (attachmentList.Count >= 1)
                {
                    // Pridanie priloh do tela emailu
                    msg.AddAttachments(attachmentList);
                }
                Logger newLog = new Logger();

                newLog.LogEmail(date, body.To, body.Subject, body.HtmlContent, attachmentList);
                // asynchronne odoslanie poziadavky na web api
                var result = await client.SendEmailAsync(msg);

                attachmentList.Clear();
                emailAddresses.Clear();
                return(result);
            }
            catch (Exception ex)
            {
                Logger newLog = new Logger();
                newLog.LogError(ex);
                MessageBox.Show(ex.ToString());
                return(null);
            }
        }