-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEmail_JavaTutorial.java
More file actions
114 lines (109 loc) · 4.42 KB
/
Copy pathEmail_JavaTutorial.java
File metadata and controls
114 lines (109 loc) · 4.42 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
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/**
*
*@author Vincent Liink to javax.mail api
* http://www.oracle.com/technetwork/java/index-138643.html
*
*/
public class Email_JavaTutorial {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
/*
*Component setup
*/
JFrame window = new JFrame("Emailer: Java Tutorial");
JPanel messageInfo = new JPanel(new BorderLayout());
JPanel userInfo = new JPanel(new BorderLayout());
final JButton send = new JButton("Send");
final JTextField user = new JTextField(10);
final JPasswordField pw = new JPasswordField(10);
final JTextField to = new JTextField(10);
final JTextField subject = new JTextField(10);
final JTextArea body = new JTextArea(5, 20);
/*
*Component properties
*/
// pw.setEchoChar('*');
body.setLineWrap(true);
body.setWrapStyleWord(false);
user.setText("username@gmail.com");
pw.setText("Password");
to.setText("Someone@gmail.com");
subject.setText("Subject");
/*
*Attach Components
*/
userInfo.add(user, BorderLayout.NORTH);
userInfo.add(pw, BorderLayout.CENTER);
messageInfo.add(to, BorderLayout.NORTH);
messageInfo.add(subject, BorderLayout.CENTER);
messageInfo.add(send, BorderLayout.SOUTH);
final JScrollPane scroll = new JScrollPane(body, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
/*
*Component Actions
*/
send.addActionListener((ActionEvent e) -> {
/*
Just in case the Lambda expression is just for newer versions of Java
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
*/
final String username = user.getText();
final String password = pw.getText();
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(user.getText()));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to.getText()));
message.setSubject(subject.getText());
message.setText(body.getText());
Transport.send(message);
JOptionPane.showMessageDialog(null, "Email sent successfully!");
} catch (MessagingException ex) {
JOptionPane.showMessageDialog(null, "Oops something went wrong" + "\n" + ex.getMessage());
}
});
/*
*Window properties
*/
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(BorderLayout.EAST, userInfo);
window.add(BorderLayout.NORTH, messageInfo);
window.add(scroll);
window.setSize(355, 220);
window.setMinimumSize(new Dimension(355, 220));
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}