Skip to content

Commit 66fccab

Browse files
committed
Java Mail Gmail
1 parent 39317b3 commit 66fccab

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-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-gmailsmtpserver-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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.hellokoding.mail;
2+
3+
public class Main {
4+
private static final String SMTP_SERVER_HOST = "smtp.gmail.com";
5+
private static final String SMTP_SERVER_PORT = "587";
6+
private static final String SUBJECT = "Sending mail with Gmail 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 FROM_USER_EMAIL = args[0];
11+
final String FROM_USER_FULLNAME = args[1];
12+
final String FROM_USER_ACCESSTOKEN = args[2];
13+
final String TO_USER_EMAIL = args[3];
14+
15+
new SendingMailThroughGmailSMTPServer().sendMail(SMTP_SERVER_HOST, SMTP_SERVER_PORT, FROM_USER_EMAIL, FROM_USER_ACCESSTOKEN, FROM_USER_EMAIL, FROM_USER_FULLNAME, TO_USER_EMAIL, SUBJECT, BODY);
16+
}
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.hellokoding.mail;
2+
3+
4+
import com.sun.mail.smtp.SMTPTransport;
5+
import com.sun.mail.util.BASE64EncoderStream;
6+
7+
import javax.mail.Message;
8+
import javax.mail.Session;
9+
import javax.mail.internet.InternetAddress;
10+
import javax.mail.internet.MimeMessage;
11+
import java.util.Properties;
12+
import java.util.logging.Level;
13+
import java.util.logging.Logger;
14+
15+
public class SendingMailThroughGmailSMTPServer {
16+
void sendMail(String smtpServerHost, String smtpServerPort, String smtpUserName, String smtpUserAccessToken, String fromUserEmail, String fromUserFullName, String toEmail, String subject, String body) {
17+
try {
18+
Properties props = System.getProperties();
19+
props.put("mail.transport.protocol", "smtp");
20+
props.put("mail.smtp.port", smtpServerPort);
21+
props.put("mail.smtp.starttls.enable", "true");
22+
23+
Session session = Session.getDefaultInstance(props);
24+
session.setDebug(true);
25+
26+
MimeMessage msg = new MimeMessage(session);
27+
msg.setFrom(new InternetAddress(fromUserEmail, fromUserFullName));
28+
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
29+
msg.setSubject(subject);
30+
msg.setContent(body, "text/html");
31+
32+
SMTPTransport transport = new SMTPTransport(session, null);
33+
transport.connect(smtpServerHost, smtpUserName, null);
34+
transport.issueCommand("AUTH XOAUTH2 " + new String(BASE64EncoderStream.encode(String.format("user=%s\1auth=Bearer %s\1\1", smtpUserName, smtpUserAccessToken).getBytes())), 235);
35+
transport.sendMessage(msg, msg.getAllRecipients());
36+
} catch (Exception ex) {
37+
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, ex.getMessage(), ex);
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)