-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendEmail.java
More file actions
101 lines (82 loc) · 3.32 KB
/
SendEmail.java
File metadata and controls
101 lines (82 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.api.utils;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendEmail {
public static Session getSession(final String username, final String password){
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
return session;
}
public static boolean sendEmail(final Session session, final String messageContent, final String toId, final String subject){
try {
Message message = new MimeMessage(session);
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toId));
message.setSubject(subject);
message.setText(messageContent);
message.setReplyTo(null);
Transport.send(message);
System.out.println("Done");
return true;
} catch (MessagingException e) {
System.out.println(new RuntimeException(e));
return false;
}
}
public static boolean sendEmailWithAttachment(final Session session, final String messageContent, final String toId, final String subject, final String file, final String attachmentName){
try {
Message message = new MimeMessage(session);
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toId));
message.setSubject(subject);
message.setText(messageContent);
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
messageBodyPart = new MimeBodyPart();
// String file = "path of file to be attached";
// String fileName = "subject";
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachmentName);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
System.out.println("Sending");
Transport.send(message);
System.out.println("Done");
return true;
} catch (MessagingException e) {
System.out.println(new RuntimeException(e));
return false;
}
}
public static void main(String[] args) {
Session s = getSession("app4api.noreply","digitalCentersIntern");
// System.out.println(sendEmail(s,"Hi","[email protected]","Test"));
System.out.println(sendEmailWithAttachment(s, "Hi", "[email protected]", "Test","pom.xml","pom.xml"));
}
}