using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
    class Email
    {
        Helper_DB db = new Helper_DB();
        public bool Send_mail(string compidx, string pur, string filePath, string sender)
        {
            db.DBcon();
            #region Setup
            // 보내는 이 : 메일ID, 메일PW, smtp주소, 포트번호
            string sender_Area = "`comp_name`, `email_ID`, `email_PW`, `smtp`, `port`";
            string tmp_db = db.DB_Select_Search(sender_Area, "Comp", "idx", compidx);
            string[] arr_db = tmp_db.Split('|');
            #endregion
            MailMessage mail = new MailMessage();
            // 보내는 사람 이메일
            mail.From = new MailAddress("jhk132765@naver.com");
            // 받는 사람 이메일
            mail.To.Add(sender);
            // 메일 제목
            mail.Subject = arr_db[0] + "주문분입니다.";
            // 메일 내용
            mail.Body = arr_db[0] + "주문분입니다.";
            // 첨부파일
            System.Net.Mail.Attachment attachment;
            // 첨부파일 붙이기
            attachment = new System.Net.Mail.Attachment(filePath);
            mail.Attachments.Add(attachment);
            // SMTP 및 포트 설정
            SmtpClient smtp = new SmtpClient(arr_db[3], Convert.ToInt32(arr_db[4]));
            smtp.EnableSsl = true;
            // 계정 설정
            smtp.Credentials = new NetworkCredential(arr_db[1], arr_db[2]);
            try
            {
                smtp.Send(mail);
                MessageBox.Show("메일 전송 완료");
                return true;
            }
            catch(SmtpException e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }
        }
        /// 
        /// 테스트 메일 보낼때 사용. (추후 함수가 기반이 되어야함.)
        /// ex. email.Send_Test_mail("관리", "jhk132765@gmail.com");
        /// 
        /// DB에 저장된 회사명.
        /// 받는 업체의 메일주소.
        public void Send_Test_mail(string compidx, string taker, string title, string content)
        {
            db.DBcon();
            string tmpdata = db.DB_Select_Search("`email_ID`, `email_PW`, `smtp`, `port`", 
                "Comp", "idx", compidx);
            string[] data = tmpdata.Split('|');
            int port = Convert.ToInt32(data[3]);
            if(port == 465) { port = 587; } // 465포트는 기술적으로 사용되지않음. 587로 대체.
            MailMessage mail = new MailMessage();
            // 보내는 사람 이메일
            mail.From = new MailAddress(data[0]);
            // 받는 사람 이메일
            mail.To.Add(taker);
            // 메일 제목
            mail.Subject = title;
            // 메일 내용
            mail.Body = content;
            // SMTP 및 포트 설정
            SmtpClient smtp = new SmtpClient(data[2], port);
            smtp.EnableSsl = true;
            // 계정 설정
            smtp.Credentials = new NetworkCredential(data[0], data[1]);
            try
            {
                smtp.Send(mail);
                MessageBox.Show("메일 전송 완료");
            }
            catch (SmtpException e)
            {
                MessageBox.Show(e.ToString());
            }
        }
    }
    class TestMail
    {
        Helper_DB db = new Helper_DB();
        /// 
        /// 메일을 보내기 위한 함수
        /// 
        /// 보내는 사람 메일
        /// 받는 사람 메일
        /// 메일의 제목
        /// 메일의 내용
        /// 첨부파일의 경로
        public void Send_Mail(string sender, string taker, string title, string body, string link = "")
        {
            db.DBcon();
            string Area = "`smtp`, `port`, `email_ID`, `email_PW`";
            string db_tmp = db.DB_Select_Search(Area, "Comp", "email_ID", sender);
            string[] db_data = db_tmp.Split('|'); // smtp, port, email_ID, email_PW
            string smtp = db_data[0];
            int port = Convert.ToInt32(db_data[1]);
            string id = db_data[2];
            string pw = db_data[3];
            MailMessage mail = new MailMessage();
            // 보내는 사람 이메일 주소
            mail.From = new MailAddress(sender);
            // 받는 사람 이메일 주소
            mail.To.Add(taker);
            // 메일 제목
            mail.Subject = title;
            // 메일 내용
            mail.Body = body;
            if(link != "") { Attach_File(mail, link); }
            // 포트 설정
            SmtpClient tp = new SmtpClient(smtp, port);
            // SSL 설정
            tp.EnableSsl = true;
            // 메일 인증을 위한 id/pw (Sender 정보)
            tp.Credentials = new NetworkCredential(id, pw);
            try
            {
                tp.Send(mail);
                MessageBox.Show("메일이 전송되었습니다.");
            }
            catch(SmtpException e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        public void Attach_File(MailMessage mail, string link)
        {
            System.Net.Mail.Attachment attachment;
            attachment = new System.Net.Mail.Attachment(link);
            mail.Attachments.Add(attachment);
        }
    }
}