-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailTransfer.java
More file actions
55 lines (42 loc) · 1.9 KB
/
MailTransfer.java
File metadata and controls
55 lines (42 loc) · 1.9 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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.library.util;
import jakarta.mail.Authenticator;
import jakarta.mail.Message;
import jakarta.mail.PasswordAuthentication;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import java.util.Properties;
/**
*
* @author hieuchu
*/
public class MailTransfer {
public static void send(String account, String title, String messageText) {
Properties config = new Properties();
config.put("mail.smtp.auth", "true"); // true : notice to server need to authentication password and gmail
config.put("mail.smtp.starttls.enable", "true"); // true : use encytion TLS for sending email
config.put("mail.smtp.host", "smtp.gmail.com"); // server addderess
config.put("mail.smtp.port", "587"); // 587 entry to send email to starttls
Session session = Session.getInstance(config, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// return new PasswordAuthentication(USERNAME, PASSWORD); // return password and account to server gmail
}
});
try {
MimeMessage message = new MimeMessage(session);
// message.setFrom(new InternetAddress(USERNAME, "Library System", "UTF-8"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(account));
message.setSubject(title, "UTF-8");
message.setContent(messageText, "text/html; charset=UTF-8");
Transport.send(message); // send message to server
} catch (Exception e) {
e.printStackTrace();
}
}
}