Skip to content

Commit 4ff02d4

Browse files
SeunMattmaibin
authored andcommitted
added example code for Java mail (eugenp#4101)
1 parent 13c7bae commit 4ff02d4

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

core-java/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@
235235
<artifactId>h2</artifactId>
236236
<version>1.4.197</version>
237237
</dependency>
238+
<dependency>
239+
<groupId>javax.mail</groupId>
240+
<artifactId>mail</artifactId>
241+
<version>1.5.0-b01</version>
242+
</dependency>
238243
</dependencies>
239244

240245
<build>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.mail;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.io.File;
7+
import java.util.Properties;
8+
import javax.mail.*;
9+
import javax.mail.internet.InternetAddress;
10+
import javax.mail.internet.MimeBodyPart;
11+
import javax.mail.internet.MimeMessage;
12+
import javax.mail.internet.MimeMultipart;
13+
14+
public class EmailService {
15+
16+
private String host = "";
17+
private int port = 0;
18+
private String username = "";
19+
private String password = "";
20+
21+
22+
public EmailService(String host, int port, String username, String password) {
23+
24+
this.host = host;
25+
this.port = port;
26+
this.username = username;
27+
this.password = password;
28+
29+
sendMail();
30+
}
31+
32+
private void sendMail() {
33+
34+
Properties prop = new Properties();
35+
prop.put("mail.smtp.auth", true);
36+
prop.put("mail.smtp.starttls.enable", "true");
37+
prop.put("mail.smtp.host", host);
38+
prop.put("mail.smtp.port", port);
39+
prop.put("mail.smtp.ssl.trust", host);
40+
41+
Session session = Session.getInstance(prop, new Authenticator() {
42+
@Override
43+
protected PasswordAuthentication getPasswordAuthentication() {
44+
return new PasswordAuthentication(username, password);
45+
}
46+
});
47+
48+
try {
49+
50+
Message message = new MimeMessage(session);
51+
message.setFrom(new InternetAddress("[email protected]"));
52+
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
53+
message.setSubject("Mail Subject");
54+
55+
String msg = "This is my first email using JavaMailer";
56+
57+
MimeBodyPart mimeBodyPart = new MimeBodyPart();
58+
mimeBodyPart.setContent(msg, "text/html");
59+
60+
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
61+
attachmentBodyPart.attachFile(new File("pom.xml"));
62+
63+
Multipart multipart = new MimeMultipart();
64+
multipart.addBodyPart(mimeBodyPart);
65+
multipart.addBodyPart(attachmentBodyPart);
66+
67+
message.setContent(multipart);
68+
69+
Transport.send(message);
70+
71+
} catch (Exception e) {
72+
e.printStackTrace();
73+
}
74+
}
75+
76+
public static void main(String ... args) {
77+
new EmailService("smtp.mailtrap.io", 25, "87ba3d9555fae8", "91cb4379af43ed");
78+
}
79+
80+
}

0 commit comments

Comments
 (0)