차근차근/이것저것

centOS sendmail에서 gmail로 발송이 안됨. 11 - gmail을 통해서 전송

예쁜꽃이피었으면 2017. 1. 19. 16:57


sendmail을 이용해서 메일을 전송하려고 했지만.. 잘 되지 않았다.






final String username="사용할 ID@gmail.com";

//final String password="실제 비밀번호";

final String password="기기 비밀번호?";

// sets SMTP server properties

   Properties properties = new Properties();

   properties.put("mail.smtp.host", "smtp.gmail.com");

   properties.put("mail.smtp.port", "25");

   properties.put("mail.smtp.auth", "true");

   properties.put("mail.smtp.starttls.enable", "true");

   properties.put("mail.user", username);

   properties.put("mail.password", password);


   // creates a new session with an authenticator

   Authenticator auth = new Authenticator() {

       public PasswordAuthentication getPasswordAuthentication() {

           return new PasswordAuthentication(JProperties.getString("MAIL.ACCESS.ID"), JProperties.getString("MAIL.ACCESS.PW"));

       

* 로컬에서 테스트 할 때는 잘되는데 서버에 적용한 후에는 되지 않는다 (인증이 안된다고 그런다.. 그럴 이유가 없는데.. 혹시해서 바로 적지 않고 프로퍼티에서 가져왔다.. )


       }

   };

   Session session = Session.getInstance(properties, auth);

try{

   // creates a new e-mail message

   Message msg = new MimeMessage(session);


   msg.setFrom(new InternetAddress(username));

   InternetAddress[] toAddresses = { new InternetAddress(toMail) };

   msg.setRecipients(Message.RecipientType.TO, toAddresses);

   msg.setSubject(subject);

   msg.setSentDate(new Date());


   // creates message part

   MimeBodyPart messageBodyPart = new MimeBodyPart();

   messageBodyPart.setContent(content, "text/html; charset="+charset);


   // creates multi-part

   Multipart multipart = new MimeMultipart();

   multipart.addBodyPart(messageBodyPart);



//    // adds attachments

//    if (attachFiles != null && attachFiles.length > 0) {

//        for (String filePath : attachFiles) {

//            MimeBodyPart attachPart = new MimeBodyPart();

//

//            try {

//                attachPart.attachFile(filePath);

//            } catch (IOException ex) {

//                ex.printStackTrace();

//            }

//

//            multipart.addBodyPart(attachPart);

//        }

//    }


   // sets the multi-part as e-mail's content

   msg.setContent(multipart);


   // sends the e-mail

   Transport.send(msg);

   

return true;

} catch(Exception e){

e.printStackTrace();

return false;

}




[Java] Gmail SMTP를 이용한 메일 보내기

http://fruitdev.tistory.com/15



[JAVA] gmail smtp 설정을 통해 메일 보내기

http://darkhorizon.tistory.com/324



javax 를 이용한 gmail 서버로 메일 보내기

http://dus815.tistory.com/entry/javax-%EB%A5%BC-%EC%9D%B4%EC%9A%A9%ED%95%9C-gmail-%EC%84%9C%EB%B2%84%EB%A1%9C-%EB%A9%94%EC%9D%BC-%EB%B3%B4%EB%82%B4%EA%B8%B0


[JAVA] gmail smtp 를 활용한 메일발송 예제

http://devgwangpal.tistory.com/34



Login failed with Javax.mail via google

http://stackoverflow.com/questions/21180966/login-failed-with-javax-mail-via-google



반응형