BEST /BaliEnterpriseSystems/BaliEnterpriseSystems/UtilEMail.cs

Language C# Lines 150
MD5 Hash ff20246b7675cdc7aebd6b0a10bac412
Repository https://github.com/sirivedula/BEST.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;
using BaliEnterpriseSystems.BestObjects;
using System.Data.OleDb;
using System.Text;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace BaliEnterpriseSystems
{
    public class UtilEMail
    {
        private string smtpHost;
        private string fromemail;
        private string frompwd;
        private string port;
        public string lastError;
        private string fromName;
        private bool enableSSL;

        public UtilEMail()
        {
            smtpHost = ConfigurationManager.AppSettings["SMTPHost"];
            fromemail = ConfigurationManager.AppSettings["FromEMail"];
            frompwd = ConfigurationManager.AppSettings["FromPassowrd"];
            port = ConfigurationManager.AppSettings["Port"];
            fromName = ConfigurationManager.AppSettings["FromName"];
            enableSSL = ConfigurationManager.AppSettings["enableSSL"].Equals("1");
        }

        public string ToAddress
        {
            get;
            set;
        }


        public bool Send(string toaddress, string subject, string body, string ToName)
        {
            var fromAddress = new MailAddress(this.fromemail, fromName);
            var toAddress = new MailAddress(toaddress, ToName);
            string fromPassword = this.frompwd;
            int iPort;
            int.TryParse(this.port, out iPort);
            var smtp = new SmtpClient
            {
                Host = this.smtpHost,
                Port = iPort,
                EnableSsl = enableSSL,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
                Timeout = 20000
            };

            bool result = true;
            BestTable tbl = new BestTable();
            OleDbCommand myCmd = tbl.dbCmd;
            myCmd.CommandText = "insert into BestLogs (message) values (?)";
            OleDbParameter p1 = new OleDbParameter("msg", OleDbType.VarChar, 9999999);

            try
            {
                using (var message = new MailMessage(fromAddress, toAddress)
                {
                    IsBodyHtml = true,
                    Subject = subject,
                    Body = body
                })
                {
                    p1.Value = "From:" + fromAddress.DisplayName + " To:" + toaddress + " Subject:" + subject + " Body:" + body;
                    myCmd.Parameters.Add(p1);
                    myCmd.ExecuteNonQuery();
                    smtp.Send(message);
                }
            }
            catch (Exception ex)
            {
                result = false;
                lastError = ex.Message;
                myCmd.Parameters[0].Value = ex.ToString();
                myCmd.ExecuteNonQuery();
            }

            return result;
        }

        public bool SendMultiple(NameValueCollection ToAddrs, string subject, string body)
        {
            bool result = true;
            MailMessage mailMsg = new MailMessage();
            mailMsg.From = new MailAddress(this.fromemail, this.fromName);
            foreach(string key in ToAddrs.AllKeys)
            {
                mailMsg.To.Add( new MailAddress(key, ToAddrs[key]));
            }
            mailMsg.Subject = subject;
            mailMsg.Body = body;

            int iPort;
            int.TryParse(this.port, out iPort);
            var fromAddress = new MailAddress(this.fromemail, fromName);
            string fromPassword = this.frompwd;

            BestTable tbl = new BestTable();

            var smtp = new SmtpClient
            {
                Host = this.smtpHost,
                Port = iPort,
                EnableSsl = enableSSL,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
                Timeout = 20000
            };

            OleDbCommand myCmd = tbl.dbCmd;
            myCmd.CommandText = "insert into BestLogs (message) values (?)";
            OleDbParameter p1 = new OleDbParameter("msg", OleDbType.VarChar, 9999999);
            try
            {
                p1.Value = "From:" + fromAddress.DisplayName + " To:" + mailMsg.To.ToString() + " Subject:" + subject + " Body:" + body;
                myCmd.Parameters.Add(p1);
                myCmd.ExecuteNonQuery();
                smtp.Send(mailMsg);
            }
            catch (Exception ex)
            {
                result = false;
                lastError = ex.Message;
                myCmd.Parameters[0].Value = ex.ToString();
                myCmd.ExecuteNonQuery();
            }

            return result;

        }
    }
}
Back to Top