162 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			162 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| 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 void Send_mail()
 | |
|         {
 | |
|             MailMessage mail = new MailMessage();
 | |
| 
 | |
|             // 보내는 사람 이메일
 | |
|             mail.From = new MailAddress("admin@gloriabook.co.kr");
 | |
| 
 | |
|             // 받는 사람 이메일
 | |
|             mail.To.Add("jhk132765@gmail.com");
 | |
| 
 | |
|             // 메일 제목
 | |
|             mail.Subject = "메일 제목";
 | |
| 
 | |
|             // 메일 내용
 | |
|             mail.Body = "메일 내용";
 | |
| 
 | |
|             // SMTP 및 포트 설정
 | |
|             SmtpClient smtp = new SmtpClient("localhost", 25);
 | |
|             smtp.EnableSsl = true;
 | |
| 
 | |
|             // 계정 설정
 | |
|             smtp.Credentials = new NetworkCredential("admin@gloriabook.co.kr", "gloria815");
 | |
| 
 | |
|             try
 | |
|             {
 | |
|                 smtp.Send(mail);
 | |
|                 MessageBox.Show("메일 전송 완료");
 | |
|             }
 | |
|             catch(SmtpException e)
 | |
|             {
 | |
|                 MessageBox.Show(e.ToString());
 | |
|             }
 | |
|         }
 | |
|         /// <summary>
 | |
|         /// 테스트 메일 보낼때 사용. (추후 함수가 기반이 되어야함.)
 | |
|         /// ex. email.Send_Test_mail("관리", "jhk132765@gmail.com");
 | |
|         /// </summary>
 | |
|         /// <param name="comp_name">DB에 저장된 회사명.</param>
 | |
|         /// <param name="taker">받는 업체의 메일주소.</param>
 | |
|         public void Send_Test_mail(string comp_name, string taker, string title, string content)
 | |
|         {
 | |
|             db.DBcon();
 | |
|             string tmpdata = db.DB_Select_Search("`email_ID`, `email_PW`, `smtp`, `port`", 
 | |
|                 "Comp", "comp_name", comp_name);
 | |
|             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();
 | |
|         /// <summary>
 | |
|         /// 메일을 보내기 위한 함수
 | |
|         /// </summary>
 | |
|         /// <param name="sender">보내는 사람 메일</param>
 | |
|         /// <param name="taker">받는 사람 메일</param>
 | |
|         /// <param name="title">메일의 제목</param>
 | |
|         /// <param name="content">메일의 내용</param>
 | |
|         /// <param name="link">첨부파일의 경로</param>
 | |
|         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); }
 | |
| 
 | |
|             // 포트 설정 ( TODO: 보내는이 메일에 따라 수정이 필요함. DB내 저장된 smtp 정보 가져오면 될 것 같음. )
 | |
|             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);
 | |
|         }
 | |
|     }
 | |
| }
 | 
