-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGmailSMTPTest.java
More file actions
37 lines (31 loc) · 1.24 KB
/
GmailSMTPTest.java
File metadata and controls
37 lines (31 loc) · 1.24 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
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class GmailSMTPTest {
public static void main(String[] args) {
String from = "your.email@gmail.com";
String password = "your-app-password";
String to = "receiver@example.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
});
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
msg.setSubject("Test Email");
msg.setText("Hello from Java!");
Transport.send(msg);
System.out.println("✅ Email sent!");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}