2011-10-26 3 views
2

Amazon SES를 사용하여 전자 메일 메시지를 보내도록 GlassFish의 JavaMail을 구성하는 방법에 대한 명확한 문서를 찾지 못했습니다. 누군가 예제를 제공해 줄 수 있습니까? AWS JDK 내부Amazon SES를 사용하도록 GlassFish JavaMail을 구성하는 방법은 무엇입니까?

+0

Amazon SES는 SMTP [2011 년 12 월 이후]를 지원합니다 (http://aws.typepad.com/aws/2011/12/new-smtp-support -for- the-simple-email-service-ses.html) 그게 그렇게 쉬운 것 같아? – Arjan

답변

3

당신이 샘플을 찾을 수 있습니다 샘플을 \ AmazonSimpleEmailService \ AWSJavaMailSample.java

기본적으로, 당신은 당신의 AWS 자격 증명 프로토콜에 "AWS", 사용자 및 암호를 설정해야합니다

// Create a email session 
Session session = Session.getInstance(props); 

// Create a new Message 
Message msg = new MimeMessage(session); 
msg.setFrom(new InternetAddress(FROM)); 
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(TO)); 
msg.setSubject(SUBJECT); 
msg.setText(BODY); 
msg.saveChanges(); 

// Reuse one Transport object for sending all your messages 
// for better performance 
Transport t = new AWSJavaMailTransport(session, null); 
t.connect(); 
t.sendMessage(msg, null); 

당신을 위해 작업을 수행해야합니다 메시지를 전송하기 위해

/* 
* Setup JavaMail to use the Amazon Simple Email Service by specifying 
* the "aws" protocol. 
*/ 
Properties props = new Properties(); 
props.setProperty("mail.transport.protocol", "aws"); 

/* 
* Setting mail.aws.user and mail.aws.password are optional. Setting 
* these will allow you to send mail using the static transport send() 
* convince method. It will also allow you to call connect() with no 
* parameters. Otherwise, a user name and password must be specified 
* in connect. 
*/ 
props.setProperty("mail.aws.user", credentials.getAWSAccessKeyId()); 
props.setProperty("mail.aws.password", credentials.getAWSSecretKey()); 
.

+0

[Amazon SES의 SMTP 인터페이스] (http : //docs.amazonwebservices)를 사용하지 않는 이유를 알고 계십니까? .com/ses/latest/DeveloperGuide/SMTP.html)? ([공지] (http://aws.typepad.com/aws/2011/12/new-smtp-support-for-the-simple-email -service-ses.html) 2011 년 12 월 – Arjan

2

Glassfish가 JavaMail 세션을 제공 할 수 있으므로 응용 프로그램 코드가 공급자에 영향을받지 않습니다.

은 글래스 피시 관리 인터페이스를 사용하여 JavaMail에 세션을 만듭니다

자원 -> JavaMail에 세션.

주요 특성은 다음과 같습니다

  • JNDI : 메일/someValue와
  • 메일 호스트 : email.us-east-1.amazonaws.com
  • 기본 보낸 사람 주소 : 소스 전자 메일 주소
  • 전송 프로토콜 : aws
  • 전송 프로토콜 클래스 : com.amazonaws.services.simpleemail.AWSJavaMailTransport

양식은 "기본 사용자"에 대한 값을 요구하지만 내가 말할 수있는, 그것은 사용되지 않습니다에서.

  • mail.aws.password : 당신의 AWS 비밀 키
  • mail.aws.user : 당신의 AWS 액세스

    는 또한 세션에 다음과 같은 속성을 추가 할 필요가 키

주사를 통해 세션을 얻을 수있는 응용 프로그램 코드 :

를 호출 msg.setFrom은() 세션 속성 "메일에서 개최 된 값으로 메시지의 보낸 사람 필드를 채 웁니다 주입 된 세션

Message msg = new MimeMessage(mailSession); 
    try { 
     msg.setSubject(subject); 
     msg.setText(body); 
     msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); 
     msg.setFrom(); 

     Transport t = session.getTransport(); 

     if (!t.isConnected()) { 
      t.connect(); 
     } 

     t.sendMessage(msg, null); 

    } catch (MessagingException ex) { 
     // Handle exception 
    } catch (UnsupportedEncodingException ex) { 
     // Handle exception   
    } 

를 사용하여 이메일을 보내십시오.사용자 "(JavaMail 세션 필드"기본 발신자 주소 "에서 가져옴)

관련 문제