앱)AWS SES Email Util java spring 예제 샘플

Posted by HULIA(휴리아)
2018. 6. 27. 14:59 백엔드개발/자바스프링
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-ses</artifactId>
<version>1.11.0</version>
</dependency>



@Service
public class AWSEmail{
    @Value("${ses.accessKey}")
    private String accessKey;
   
    @Value("${ses.secretKey}")
    private String secretKey;

    @Value("${ses.region}")
    private String region;

    public String sendMail(String fromEmail, List<String> toAddress, String subject, String emailBody) throws AmazonClientException, Exception {
           String resultMsg = "";
           SendEmailRequest request = new SendEmailRequest().withSource(fromEmail);
         
          Destination dest = new Destination().withToAddress(toAddress);
          request.setDestination(dest);

          Content subjContent = new Content().withData(subject);
          Message msg = new Message().withSubject(subjContent);

         //Include a body in HTML formats.
          Content htmlContent = new Content().withData(emailBody);
          Body body = new Body.withHtml(htmlContent);
          msg.setBody(body);

          request.setMessage(msg);

          //Set AWS access credentials
          String tmpAccessKey = "";
          String tmpSecretKey ="";
          
          if("Local".equals(System.getProperty("server.type"))){
              tmpAccessKey = accessKey;
              tmpSecretKey = secretKey;

                           } else {
                              try{
                                  tmpAccessKey = AESCipherUtil.decrypt(accessKey);
                                  tmpSecretKey = AESCipherUtil.decrypt(secretKey);
                              }catch(Exception e){
                                sysout("Credential decryption failed !");
                              }
                           }

                   AWSCredentilas credentilas = new BasicAWSCredentials(tmpAcessKey, tmpSecretKey);
                   AmazonSimpleEmailServiceClient client = null;
   
                  if("Local".equals(System.getProperty("server.type"))){
                      ClientConfiguration clientCfg = new ClientConfiguration();
                       clientCfg.setProtocol(Protocol.HTTP);
                      clientCfg.setProxyHost("XXX.XXX.XXX.XXX");
               clientCfg.setProxyPort(8080);
                     client = new AmazonSimpleEmailServiceClient(credentials, clientCfg);
             } else{
                    client = new AmazonSimpleEmailServiceClient(credentials);
              }


               Region regionCode = null;

               switch(region){
                         case "EU_WEST_1":
                                 regionCode = Region.getRegion(Region.EU_WEST_1);
                                 break;
                         case "AP_NORTHEAST_1":
                             regionCode = Region.getRegion(Region.AP_NORTHEAST_1);
                             break;

               }

                client.setRegion(regionCode);

                  //Call Amazon SES to send the message
                  SendEmailResult rs = client.sendEmail(request);
                   resultMsg = rs.toString();

                   return resultMsg;
    }
}