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();
|
}
|
|
}
|
|
}
|
}
|