ulysseskao
2016-05-05 540014a7702a9bae7a3b9c00098671a132e869e8
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
using System;
using System.Data.OracleClient;
using System.Net;
using System.Net.Mail;
using NLog;
 
namespace CCSTrace.CCS.Function
{
    public class MailService
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
 
        private SmtpClient _smtpClient;
        private string _smtpIp;
        private int _smtpPort;
        private string _eMailAddr;
        private string _eMailPass;
        private string _toEMail;
 
        public MailService()
        {
        }
 
        public void InitialSmtpClient(OracleConnection conn)
        {
            GetMailSetting(conn);
            _smtpClient = new SmtpClient(_smtpIp, _smtpPort)
            {
                Credentials = new NetworkCredential(_eMailAddr, _eMailPass),
                EnableSsl = false
            };
        }
 
        public bool SendMail(string ccsid)
        {
            Attachment objAttFle = new Attachment(GlobalVariable.CcsListPath + ccsid + ".txt");
            MailMessage objMail = new MailMessage();
 
            try
            {
                objMail.From = new MailAddress(_eMailAddr);
                objMail.To.Add(_toEMail);
 
                objMail.BodyEncoding = System.Text.Encoding.UTF8;
                objMail.Subject = "CCS事故案件追蹤失敗";
                //objMail.Body = "測試收件者";
                objMail.IsBodyHtml = true;
 
                objMail.Attachments.Add(objAttFle);
 
                _smtpClient.Send(objMail);
                return true;
            }
            catch
            {
                return false;
            }
        }
 
        private void GetMailSetting(OracleConnection conn)
        {
            const string sqlStmt = "SELECT MAIL_ACCOUNT,MAIL_PASSWORD,MAIL_HOST,MAIL_PORT,WOS_MAIL FROM USRADMIN.SYS_MAIL";
 
            OracleCommand command = new OracleCommand(sqlStmt, conn);
            OracleDataReader reader = command.ExecuteReader();
 
            try
            {
                if (reader.Read())
                {
                    _smtpIp = reader["MAIL_HOST"].ToString();
                    _smtpPort = Convert.ToInt32(reader["MAIL_PORT"].ToString());
                    _eMailAddr = reader["MAIL_ACCOUNT"].ToString();
                    _eMailPass = reader["MAIL_PASSWORD"].ToString();
                    _toEMail = reader["CCS_MAIL"].ToString();
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message);
            }
            finally
            {
                reader.Close();
                command.Dispose();
            }
        }
    }
}