forked from AsishSamantaray/send-email-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailUtil.java
More file actions
171 lines (146 loc) · 5.81 KB
/
EmailUtil.java
File metadata and controls
171 lines (146 loc) · 5.81 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
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 EmailUtil {
// public static void sendEmail(String fromEmail, String password, String toEmail, String subject, String body){
//
//
// Properties props = new Properties();
// props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
// props.put("mail.smtp.port", "587"); //TLS Port
// props.put("mail.smtp.auth", "true"); //enable authentication
// props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
//
// //create Authenticator object to pass in Session.getInstance argument
// Authenticator auth = new Authenticator() {
// //override the getPasswordAuthentication method
// protected PasswordAuthentication getPasswordAuthentication() {
// return new PasswordAuthentication(fromEmail, password);
// }
// };
// Session session = Session.getInstance(props, auth);
//
// try
// {
// MimeMessage msg = new MimeMessage(session);
// //set message headers
// msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
// msg.addHeader("format", "flowed");
// msg.addHeader("Content-Transfer-Encoding", "8bit");
//
// msg.setFrom(new InternetAddress("no_reply@example.com", "NoReply-JD"));
//
// msg.setReplyTo(InternetAddress.parse("no_reply@example.com", false));
//
// msg.setSubject(subject, "UTF-8");
//
// msg.setText(body, "UTF-8");
//
// msg.setSentDate(new Date());
//
// msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
// System.out.println("Message is ready");
// Transport.send(msg);
//
// System.out.println("EMail Sent Successfully!!");
// }
// catch (Exception e) {
// e.printStackTrace();
// }
// }
public static void sendEmail(String from, String password, String to, String sub, String msg) {
// Get properties object
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");
// get Session
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
});
// compose message
try {
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
// send message
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
/*
props.put("mail.smtp.host", "smtp-mail.outlook.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
*/
public static void sendAttachmentEmail(String from, String password, String toEmail, String subject, String body){
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");
// Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toEmail));
// Set Subject: header field
message.setSubject(subject);
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Now set the actual message
messageBodyPart.setText(body);
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "D:\\J2EE\\Projects\\jdbcdemo\\employee_details\\data.csv";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("data.csv");
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}