public string SendMail(MAILINFO ob, bool bThread = true) { string msg = string.Empty; try { obMail = ob; _bThread = bThread; if (bThread) { ThreadStart1 = new System.Threading.ThreadStart(this.SendMailExec); thread1 = new Thread(ThreadStart1); thread1.Start(); } else { this.SendMailExec(); } } catch (Exception ex) { throw ex; } return(msg); }
private void SendMailExec() { try { MAILINFO ob = obMail; MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient(ob.SMTP_ADDRESS); mail.From = new MailAddress(ob.SENDER_ID, ob.SENDER_NAME, System.Text.Encoding.UTF8); //보내는 사람 설정 if (!string.IsNullOrEmpty(ob.CC_ID)) { string[] arrCC = ob.CC_ID.Split(';'); foreach (string ccData in arrCC) { if (!string.IsNullOrEmpty(ccData)) { mail.CC.Add(ccData); //참조메일 설정 } } } if (!string.IsNullOrEmpty(ob.SUBJECT)) { mail.Subject = ob.SUBJECT;//제목 설정 } if (!string.IsNullOrEmpty(ob.CONTENT)) { mail.Body = ob.CONTENT; } /*+ ob.CUSTOMER_CODE;*/ //내용 설정 ////추가 //if (!string.IsNullOrEmpty(ob.CUSTOMER_CODE)) // mail.Body = ob.CUSTOMER_CODE;//내용에 customer_code 갖고오는지 확인 테스트 mail.IsBodyHtml = ob.IsBodyHtml; mail.BodyEncoding = System.Text.Encoding.UTF8; //한글 입력하려면 써야한다는군요 mail.SubjectEncoding = System.Text.Encoding.UTF8; //마찬가지 if (!string.IsNullOrEmpty(ob.FILE_PATH)) { System.Net.Mail.Attachment attachment; //첨부파일 만들기 attachment = new System.Net.Mail.Attachment(ob.FILE_PATH); //첨부파일 붙이기 mail.Attachments.Add(attachment); //첨부파일 붙이기 } SmtpServer.Port = ob.SMTP_PORT; //쥐메일 포트 설정 SmtpServer.UseDefaultCredentials = false; // 시스템에 설정된 인증 정보를 사용하지 않는다. SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network; // 이걸 하지 않으면 Gmail에 인증을 받지 못한다. SmtpServer.Credentials = new System.Net.NetworkCredential(ob.SMTP_ID, ob.SMTP_PW); //쥐메일 인증을 위한 아이디 비밀번호 if (ob.SMTP_ADDRESS.Contains("gmail")) { SmtpServer.EnableSsl = true; } else { SmtpServer.EnableSsl = ob.EnableSsl; //SSL 설정 (쥐메일은 트루해야해요) } if (ob.ACCEPT_TYPE == enAcceptType.PERSONAL) //개별적으로 발송 { if (!string.IsNullOrEmpty(ob.ACCEPT_ID)) { string[] arrAc = ob.ACCEPT_ID.Split(';'); foreach (string Data1 in arrAc) { if (!string.IsNullOrEmpty(Data1)) { mail.To.Add(Data1); //받는 사람 설정 SmtpServer.Send(mail); // 메일 발송 mail.To.RemoveAt(0); } } } } else { if (!string.IsNullOrEmpty(ob.ACCEPT_ID)) { string[] arrAc = ob.ACCEPT_ID.Split(';'); foreach (string Data1 in arrAc) { if (!string.IsNullOrEmpty(Data1)) { mail.To.Add(Data1); //받는 사람 설정 } } } SmtpServer.Send(mail);// 메일 발송 } if (_bThread) { thread1.Abort(); } } catch (Exception ex) { if (_bThread) { thread1.Abort(); } throw ex; } }