메일전송 코드

java에서 메일 전송시 여러가지 오류가 생긴다…

그중 가장 완벽히 전송이 가능한 형태를 올려본다.

public static boolean sendMail(String from, String to, String cc, String msg, String title, String path) throws UnsupportedEncodingException{
    String authId = "계정@도메인";
    String authPwd = "비번";
    
    Properties properties = System.getProperties();
    String host = "메일서버호스트";
    properties.put("mail.smtp.port","587"); //tls 를 사용하는지 안하는지에따라 포트번호가 달라질 수 있다.
  properties.put("mail.smtp.host",host);
  properties.put("mail.transport.protocol","smtp");  
   properties.put("mail.smtp.starttls.enable", "true"); //tls를 사용하지 않는다면 이부분은 필요 없다.
   properties.put("mail.smtp.starttls.required", "true");//tls를 사용하지 않는다면 이부분은 필요 없다.
   //properties.put("mail.smtp.auth", "true"); //550 [sniper] isn't allowed to relay 오류 발생시 넣어준다. 보내는 메일서버 인증하기 옵션이다.
   
   
   MailAuth auth = new MailAuth (authId, authPwd);
   
   Session mailSession = Session.getDefaultInstance(properties, auth);
   MimeMessage message = new MimeMessage(mailSession);
   try {
       

     message.setFrom(new InternetAddress(from));  
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
     
     message.setSubject(title);  
     String content = " 내용내용내용";

     //첨부파일 처리      
     Multipart multipart = new MimeMultipart();
     MimeBodyPart messageBodyPart = new MimeBodyPart();
         messageBodyPart = new MimeBodyPart();
         
         //String file = "path of file to be attached";
         String fileName = "파일명.확장자";          
         DataSource source = new FileDataSource(path); //전체 경로
         messageBodyPart.setDataHandler(new DataHandler(source));
         messageBodyPart.setFileName(MimeUtility.encodeText(fileName));         
         multipart.addBodyPart(messageBodyPart);
         
         
         MimeBodyPart messageBodyPart2 = new MimeBodyPart();
         messageBodyPart2 = new MimeBodyPart();      
         messageBodyPart2.setContent(content, "text/html; charset=utf-8");
         multipart.addBodyPart(messageBodyPart2);
         
         message.setContent(multipart);
   
     Transport transport=mailSession.getTransport("smtp");
     transport.connect("메일서버주소",authId,authPwd);
     
     
     Address[] add = message.getAllRecipients();      
     
     transport.sendMessage(message,add);
     transport.close();
   
     return true;
   } catch (AddressException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     return false;
   } catch (MessagingException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     return false;
   }
   }

MailAuth 내용

import javax.mail.Authenticator;
 import javax.mail.PasswordAuthentication;
 
 public class MailAuth extends Authenticator {
 String smtpUsername = null;
 String smtpPassword = null;
 
 public MailAuth(String username, String password) {
   smtpUsername = username;
   smtpPassword = password;
 }
 
 public PasswordAuthentication getPasswordAuthentication() {
   return new PasswordAuthentication(smtpUsername, smtpPassword);
 }
 
 }