Skip to content

Commit 39317b3

Browse files
committed
JavaMail AWS SES
1 parent 36d8584 commit 39317b3

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
target
3+
*.iml
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.hellokoding.mail</groupId>
8+
<artifactId>sendingmail-aws-sessmtpserver-javamailapi</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<exec.mainClass>com.hellokoding.mail.Main</exec.mainClass>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.sun.mail</groupId>
18+
<artifactId>javax.mail</artifactId>
19+
<version>1.6.0</version>
20+
</dependency>
21+
</dependencies>
22+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.hellokoding.mail;
2+
3+
public class Main {
4+
private static final String SMTP_SERVER_HOST = "email-smtp.us-west-2.amazonaws.com";
5+
private static final String SMTP_SERVER_PORT = "587";
6+
private static final String SUBJECT = "Sending mail with SES SMTP and Java Mail";
7+
private static final String BODY = "Hi,<br><br>This is a programmatic email.";
8+
9+
public static void main(String[] args) {
10+
final String SMTP_USER_NAME = args[0];
11+
final String SMTP_USER_PASSWORD = args[1];
12+
final String FROM_USER_EMAIL = args[2];
13+
final String FROM_USER_FULLNAME = args[3];
14+
final String TO_USER_EMAIL = args[4];
15+
16+
SendingMailThroughAWSSESSMTPServer sendingMailThroughAWSSESSMTPServer = new SendingMailThroughAWSSESSMTPServer();
17+
sendingMailThroughAWSSESSMTPServer.sendMail(SMTP_SERVER_HOST, SMTP_SERVER_PORT, SMTP_USER_NAME, SMTP_USER_PASSWORD, FROM_USER_EMAIL, FROM_USER_FULLNAME, TO_USER_EMAIL, SUBJECT, BODY);
18+
}
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.hellokoding.mail;
2+
3+
4+
import javax.mail.*;
5+
import javax.mail.internet.InternetAddress;
6+
import javax.mail.internet.MimeMessage;
7+
import java.util.Properties;
8+
import java.util.logging.Level;
9+
import java.util.logging.Logger;
10+
11+
public class SendingMailThroughAWSSESSMTPServer {
12+
void sendMail(String smtpServerHost, String smtpServerPort, String smtpUserName, String smtpUserPassword, String fromUserEmail, String fromUserFullName, String toEmail, String subject, String body) {
13+
try {
14+
Properties props = System.getProperties();
15+
props.put("mail.transport.protocol", "smtp");
16+
props.put("mail.smtp.port", smtpServerPort);
17+
props.put("mail.smtp.starttls.enable", "true");
18+
props.put("mail.smtp.auth", "true");
19+
20+
Session session = Session.getDefaultInstance(props);
21+
22+
MimeMessage msg = new MimeMessage(session);
23+
msg.setFrom(new InternetAddress(fromUserEmail, fromUserFullName));
24+
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
25+
msg.setSubject(subject);
26+
msg.setContent(body, "text/html");
27+
28+
Transport transport = session.getTransport();
29+
transport.connect(smtpServerHost, smtpUserName, smtpUserPassword);
30+
transport.sendMessage(msg, msg.getAllRecipients());
31+
} catch (Exception ex) {
32+
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, ex.getMessage(), ex);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)