hank
2015-08-18 2d4b747b3ac277babbd8eddfd88f378953f497bd
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
using System;
using System.Data.OracleClient;
using System.Net.Mail;
 
namespace CCSTrace.CCS.Function
{
    public class MailService
    {
        private SmtpClient smtpClient = null;
        private string SMTP_IP;
        private int SMTP_Port;
        private string EMailAddr;
        private string EMailPass;
        private string ToEMail;
 
        public MailService(OracleConnection _Conn)
        {
            InitialSmtpClient(_Conn);
        }
 
        public void InitialSmtpClient(OracleConnection _Conn)
        {
            getMailSetting(_Conn);
            smtpClient = new SmtpClient(SMTP_IP, SMTP_Port);
            smtpClient.Credentials = new System.Net.NetworkCredential(EMailAddr, EMailPass);
            smtpClient.EnableSsl = false;
        }
 
        public bool SendMail(String CCSID)
        {
            Attachment objAttFle = new Attachment(CCS.LocalVariable.CCS_ListPath + 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;
 
                if ( objAttFle != null )
                    objMail.Attachments.Add(objAttFle);
 
                smtpClient.Send(objMail);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
 
        private void getMailSetting(OracleConnection _Conn)
        {
            String SqlStmt;
 
            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())
                {
                    SMTP_IP = reader["MAIL_HOST"].ToString();
                    SMTP_Port = Convert.ToInt32(reader["MAIL_PORT"].ToString());
                    EMailAddr = reader["MAIL_ACCOUNT"].ToString();
                    EMailPass = reader["MAIL_PASSWORD"].ToString();
                    ToEMail = reader["CCS_MAIL"].ToString();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                reader.Close();
                Command.Dispose();
            }
            
        }
 
    }
}