2013-07-03 4 views
-4

JavaSE 1.6을 사용하여 간단한 전자 메일 보낸 사람을 만들어야합니다. 해결책을 찾으려고했지만 JavaEE에 대한 토론 만있었습니다.javaSE1.6을 사용하여 전자 메일을 보내는 방법

+0

https://developers.google.com/appengine/docs/java/mail/usingjavamail – scrappedcola

+0

정말? 구글 "이메일 보내기". 가장 결과 : http://www.tutorialspot.com/java/java_sending_email.htm. 또한 관련 :이 그래서 게시물 : http://stackoverflow.com/questions/3649014/send-email-using-java –

+0

Javax.mail는 SE 라이브러리가 아니라 EE 환경을위한 것입니다! – user2512014

답변

1

확실히 Java SE 내에서 메일을 보낼 수 있습니다. 프로젝트에 mail.jar 및 activation.jar라는 두 개의 jar 파일 만 포함하면됩니다. Java EE에는 Java EE 컨테이너 외부에서 사용할 수있는 많은 기술이 포함되어 있습니다. 관련 라이브러리 파일을 프로젝트에 추가하면되므로 클래스 경로가 필요합니다. 아래 코드는 TLS를 사용하고 메일을 보내기 위해 인증을 요구하는 전자 메일 서버에서 작동해야합니다. 포트 25 (보안 없음)에서 실행되는 전자 메일 서버는 코드가 적습니다.

public static void main(String[] args) { 
    Properties props = new Properties(); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.socketFactory.port", "465"); 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.port", "465"); 
    Session session = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication("gmailUser", "*****"); 
       } 
      }); 

    System.out.println(props); 
    try { 
     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse("[email protected]")); 
     message.setSubject("Java EE7 jars deployed used on Java SE"); 
     message.setText("Dear Mr. Gupta,\nThank you for your work on Java EE7, but I need to send email from my desktop app."); 
     Transport.send(message); 
     System.out.println("Done"); 
    } catch (MessagingException utoh) { 
     System.out.println("Error sending message:" + utoh.toString()); 
    } 
} 
관련 문제