Thursday, January 21, 2016

Batch Mail through VS.. Reports

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;
using System.Net;
using System.Diagnostics;
namespace epicmail
{
    public partial class Form1 : Form
    {
        List<string> data;
        List<string> cren;
        List<string> subj;
        List<string> body;
        List<string> frm;
        List<string> to;
        List<string> cc;
        List<string> files;
        string sSource;
        string sLog;
        string sEvent;
        int flag = 0;
        private readonly string path = @"c:\config\mailconfig.txt";
        private readonly string mailfile = @"c:\Report\";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }
        protected void loadData()
        {
            string line = string.Empty;
            string header = string.Empty;
            cren = new List<string>();
            subj = new List<string>();
            body = new List<string>();
            frm = new List<string>();
            to = new List<string>();
            cc = new List<string>();
            files = new List<string>();
            if (Directory.Exists(mailfile))
            {
               
                string[] p = Directory.GetFiles(mailfile);
                foreach (string pt in p)
                {
                    flag = 1;
                    files.Add(pt);
                }
            }
            else
            {
                sSource = "Robot Mail Application";
                sLog = "Application";
                sEvent = "Please check the mail or configuration file.\r\n!!!";
                if (!EventLog.SourceExists(sSource))
                    EventLog.CreateEventSource(sSource, sLog);
                EventLog.WriteEntry(sSource, sEvent);
                EventLog.WriteEntry(sSource, sEvent,
                    EventLogEntryType.Error, 234);
                flag = 0;
            }
            if (File.Exists(path) && flag == 1)
            {
                data = new List<string>();
                using (StreamReader sr = new StreamReader(path))
                {
                    if (sr != null)
                    {
                        while ((line = sr.ReadLine()) != null)
                        {
                            data.Add(line);
                            flag = 1;
                        }
                    }
                    else
                    {
                        sSource = "Robot Mail";
                        sLog = "Application";
                        sEvent = "Please check the mail or configuration file.\r\n!!!";
                        if (!EventLog.SourceExists(sSource))
                            EventLog.CreateEventSource(sSource, sLog);
                        EventLog.WriteEntry(sSource, sEvent);
                        EventLog.WriteEntry(sSource, sEvent,
                            EventLogEntryType.Error, 234);
                        flag = 0;
                    }
                }
                if (flag == 1)
                {
                    foreach (string s in data)
                    {
                        if (!s.Contains("="))
                        {
                            if (s.ToUpper() == "SMTP" || s.ToUpper() == "SUBJECT" || s.ToUpper() == "BODY" || s.ToUpper() == "FROM" || s.ToUpper() == "TO" || s.ToUpper() == "CC")
                            {
                                header = s;
                            }
                            if (header.ToUpper() == "SMTP" && s.ToUpper() != "SMTP")
                            {
                                cren.Add(s);
                            }
                            if (header.ToUpper() == "SUBJECT" && s.ToUpper() != "SUBJECT")
                            {
                                subj.Add(s);
                            }
                            if (header.ToUpper() == "BODY" && s.ToUpper() != "BODY")
                            {
                                body.Add(s);
                            }
                            if (header.ToUpper() == "FROM" && s.ToUpper() != "FROM")
                            {
                                frm.Add(s);
                            }
                            if (header.ToUpper() == "TO" && s.ToUpper() != "TO")
                            {
                                to.Add(s);
                            }
                            if (header.ToUpper() == "CC" && s.ToUpper() != "CC")
                            {
                                cc.Add(s);
                            }
                        }

                    }
                }
            }
            else
            {
                sSource = "Robot Mail";
                sLog = "Application";
                sEvent = "Please check the mail or configuration file.\r\n!!!";
                if (!EventLog.SourceExists(sSource))
                    EventLog.CreateEventSource(sSource, sLog);
                EventLog.WriteEntry(sSource, sEvent);
                EventLog.WriteEntry(sSource, sEvent,
                    EventLogEntryType.Error, 234);
            }
        }
        protected void sendMail()
        {
            MailMessage mailMessage = new MailMessage();
            foreach (string t in to)
            {
                mailMessage.To.Add(t);
            }
            foreach (string m in subj)
                mailMessage.Subject = m;
            foreach (string c in cc)
                mailMessage.CC.Add(c);
            string bd = string.Empty;
            foreach (string b in body)
            {
                bd = bd + b;
            }
            mailMessage.Body = bd;
           
            foreach (string f in files)
                mailMessage.Attachments.Add(new Attachment(f));
            mailMessage.From = new MailAddress(frm[0]);
            mailMessage.IsBodyHtml = true;//false;
            SmtpClient sc = new SmtpClient(cren[0]);
            sc.Port = 587;
            sc.Credentials = new NetworkCredential(cren[1], cren[2]);
            sc.EnableSsl = true;//false
            sc.Send(mailMessage);
            mailMessage.Dispose();
            mailMessage = null;
            sc.Dispose();
            sc = null;
        }
        protected void delFile()
        {
            try
            {
               
               
                Directory.CreateDirectory(@"C:\Report");
                String[] f = Directory.GetFiles(@"C:\Report");
                foreach (string f1 in f)
                    File.Delete(f1);
               
            }
            catch (IOException ex)
            {
                sSource = "Robot Mail";
                sLog = "Application";
                sEvent = ex.Message;
                if (!EventLog.SourceExists(sSource))
                    EventLog.CreateEventSource(sSource, sLog);
                EventLog.WriteEntry(sSource, sEvent);
                EventLog.WriteEntry(sSource, sEvent,
                    EventLogEntryType.Error, 234);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                loadData();
                if (flag == 1)
                    sendMail();
                sSource = "Robot Mail Application";
                sLog = "Application";
                sEvent = "Mail send successfully.";
                EventLog.WriteEntry(sSource, sEvent);
                EventLog.WriteEntry(sSource, sEvent,
                    EventLogEntryType.Information, 234);
                delFile();
                this.Close();
                this.Dispose();
            }
            catch (Exception ex)
            {
                sSource = "Robot Mail Application";
                sLog = "Application";
                sEvent = ex.Message;
                if (!EventLog.SourceExists(sSource))
                    EventLog.CreateEventSource(sSource, sLog);
                EventLog.WriteEntry(sSource, sEvent);
                EventLog.WriteEntry(sSource, sEvent,
                    EventLogEntryType.Error, 234);
            }
        }
    }
}

No comments:

Post a Comment

Copy Markup charges while posting purchase invoice using X++

 Copy Markup charges while posting purchase invoice using X++ Class: Important: Code logic is just for Reference.  New class => Duplicate...