Search in sources :

Example 1 with AWSJavaMailTransport

use of com.amazonaws.services.simpleemail.AWSJavaMailTransport in project onebusaway-application-modules by camsys.

the class EmailServiceImpl method setup.

@PostConstruct
@Override
public void setup() {
    try {
        String mailSMTPServer = "";
        String mailSMTPServerPort = "";
        // Try getting smtp host and port values from configurationService.
        try {
            mailSMTPServer = configurationService.getConfigurationValueAsString("admin.smtpHost", SMTP_HOST_NOT_FOUND);
            mailSMTPServerPort = configurationService.getConfigurationValueAsString("admin.smtpPort", "25");
        } catch (RemoteConnectFailureException e) {
            _log.error("Setting smtp host to value : '" + SMTP_HOST_NOT_FOUND + "' due to failure to connect to TDM");
            mailSMTPServer = SMTP_HOST_NOT_FOUND;
            e.printStackTrace();
        }
        // If smtp host name was not found, assume this should use AWS
        _properties = new Properties();
        boolean useSMTP = mailSMTPServer.equals(SMTP_HOST_NOT_FOUND) ? false : true;
        if (useSMTP) {
            // Configure for SMTP
            _properties.setProperty("mail.transport.protocol", "smtp");
            _properties.setProperty("mail.smtp.starttls.enable", "false");
            _properties.setProperty("mail.smtp.host", mailSMTPServer);
            _properties.setProperty("mail.smtp.auth", "false");
            _properties.setProperty("mail.debug", "false");
            _properties.setProperty("mail.smtp.port", mailSMTPServerPort);
        } else {
            // Configure for AWS
            // AWS specifics
            _credentials = new BasicAWSCredentials(_username, _password);
            _eClient = new AmazonSimpleEmailServiceAsyncClient(_credentials);
            // Java specifics
            _properties.setProperty("mail.transport.protocol", "aws");
            _properties.setProperty("mail.aws.user", _credentials.getAWSAccessKeyId());
            _properties.setProperty("mail.aws.password", _credentials.getAWSSecretKey());
        }
        _session = Session.getInstance(_properties);
        Session session = Session.getDefaultInstance(_properties);
        session.setDebug(false);
        _transport = useSMTP ? _session.getTransport("smtp") : new AWSJavaMailTransport(_session, null);
    } catch (Exception ioe) {
        // log this heavily, but don't let it prevent context startup
        _log.error("EmailServiceImpl setup failed, likely due to missing or invalid credentials.");
        _log.error(ioe.toString());
    }
}
Also used : RemoteConnectFailureException(org.springframework.remoting.RemoteConnectFailureException) Properties(java.util.Properties) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) AWSJavaMailTransport(com.amazonaws.services.simpleemail.AWSJavaMailTransport) RemoteConnectFailureException(org.springframework.remoting.RemoteConnectFailureException) AmazonSimpleEmailServiceAsyncClient(com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceAsyncClient) Session(javax.mail.Session) PostConstruct(javax.annotation.PostConstruct)

Aggregations

BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 AWSJavaMailTransport (com.amazonaws.services.simpleemail.AWSJavaMailTransport)1 AmazonSimpleEmailServiceAsyncClient (com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceAsyncClient)1 Properties (java.util.Properties)1 PostConstruct (javax.annotation.PostConstruct)1 Session (javax.mail.Session)1 RemoteConnectFailureException (org.springframework.remoting.RemoteConnectFailureException)1