-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsendEmail.java
92 lines (83 loc) · 2.77 KB
/
sendEmail.java
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
package test;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.activation.*;
public class sendEmail {
public static void main(String[] args) throws InterruptedException {
/* String hostname="localhost";
String from="[email protected]";
String to="[email protected]";
String shouquanma="bxginmdxqplmdaig";
String text="你好";
new sendEmailByQQSmpt().send(hostname, from, to, shouquanma, text);
*/
new sendEmailGUI();
}
}
class sendEmailByQQSmpt{
public void send(String hostname,String from,String to,String shouquanma,String text) throws InterruptedException {
Properties properties=System.getProperties();
properties.put("mail.transport.protocol", "smtp");// 连接协议
properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
Session session=Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
try {
message.setSubject("subject line");
message.setText(text);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
Transport transport = session.getTransport();
transport.connect(from, shouquanma);
transport.sendMessage(message, message.getAllRecipients());
} catch (MessagingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
class sendEmailGUI extends JFrame{
private Container container;
private JFrame jframe=new JFrame();
private JTextField from=new JTextField("from");
private JTextField to=new JTextField("to");
private JTextField id=new JTextField("id");
private JTextField text=new JTextField("text");
private JButton send=new JButton("发送邮件");
sendEmailGUI(){
jframe=new JFrame();
container=jframe.getContentPane();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(200,300,800,100);
setLayout(new GridLayout());
from.setSize(100,20);
to.setSize(100,20);
id.setSize(30,10);
text.setSize(50,50);
send.setSize(50,50);
send.setBackground(Color.green);
add(from);
add(to);
add(id);
add(text);
add(send);
setVisible(true);
send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
try {
new sendEmailByQQSmpt().send("localhost", from.getText(), to.getText(), id.getText(), text.getText());
} catch (InterruptedException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
});
}
}