发送E-mail

为了发送E-mail,必须建立一个到端口25(即SMTP端口)的套接字连接。

一旦连接到服务器,就可以发送一个邮件报头(采用SMTP格式,该格式很容易生成)。紧随其后的是邮件消息。详细操作步骤如下

  1. 打开一个到达主机的套接字:
1
2
Socket s=new Socket("mail.yourserver.com",25);
PrintWriter out = new PrintWriter(s.getOutputStream());
  1. 发送一下消息到打印流:

    1
    2
    3
    4
    5
    6
    7
    8
    HELO sending host
    MAIL FROM:<sender e-mail address>
    RCPT TO: <recipient e-mail address>
    DATA
    mail message
    {any number of lines}
    ...
    QUIT

    SMTP规范规定,每一行都要以\r再紧跟一个\n来结尾。

    有些SMTP服务器并不检查信息的真实性,你可以随意填写任何你喜欢的发件人名字。

相比几下,有个更为简单的E-mail发送方式——使用JavaMail API,在JavaMail API中,你只需要简单地调用下面这个方法,就可以发送一条邮件了,该类库负责实现邮件协议、认证和附件处理等。

示例程序如下:

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
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
public class MailTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new MailTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class MailTestFrame extends JFrame
{
public MailTestFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setTitle("MailTest");

setLayout(new GridBagLayout());

add(new JLabel("From:"), new GBC(0, 0).setFill(GBC.HORIZONTAL));

from = new JTextField(20);
add(from, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0));

add(new JLabel("To:"), new GBC(0, 1).setFill(GBC.HORIZONTAL));

to = new JTextField(20);
add(to, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(100, 0));

add(new JLabel("SMTP server:"), new GBC(0, 2).setFill(GBC.HORIZONTAL));

smtpServer = new JTextField(20);
add(smtpServer, new GBC(1, 2).setFill(GBC.HORIZONTAL).setWeight(100, 0));

message = new JTextArea();
add(new JScrollPane(message), new GBC(0, 3, 2, 1).setFill(GBC.BOTH).setWeight(100, 100));

comm = new JTextArea();
add(new JScrollPane(comm), new GBC(0, 4, 2, 1).setFill(GBC.BOTH).setWeight(100, 100));

JPanel buttonPanel = new JPanel();
add(buttonPanel, new GBC(0, 5, 2, 1));

JButton sendButton = new JButton("Send");
buttonPanel.add(sendButton);
sendButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
new SwingWorker<Void, Void>()
{
protected Void doInBackground() throws Exception
{
comm.setText("");
sendMail();
return null;
}
}.execute();
}
});
}
public void sendMail()
{
try
{
Socket s = new Socket(smtpServer.getText(), 25);

InputStream inStream = s.getInputStream();
OutputStream outStream = s.getOutputStream();

in = new Scanner(inStream);
out = new PrintWriter(outStream, true /* autoFlush */);

String hostName = InetAddress.getLocalHost().getHostName();

receive();
send("HELO " + hostName);
receive();
send("MAIL FROM: <" + from.getText() + ">");
receive();
send("RCPT TO: <" + to.getText() + ">");
receive();
send("DATA");
receive();
send(message.getText());
send(".");
receive();
s.close();
}
catch (IOException e)
{
comm.append("Error: " + e);
}
}
public void send(String s) throws IOException
{
comm.append(s);
comm.append("\n");
out.print(s.replaceAll("\n", "\r\n"));
out.print("\r\n");
out.flush();
}
public void receive() throws IOException
{
String line = in.nextLine();
comm.append(line);
comm.append("\n");
}

private Scanner in;
private PrintWriter out;
private JTextField from;
private JTextField to;
private JTextField smtpServer;
private JTextArea message;
private JTextArea comm;

public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 300;
}
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
import java.awt.*;

public class GBC extends GridBagConstraints
{
public GBC(int gridx, int gridy)
{
this.gridx = gridx;
this.gridy = gridy;
}
public GBC(int gridx, int gridy, int gridwidth, int gridheight)
{
this.gridx = gridx;
this.gridy = gridy;
this.gridwidth = gridwidth;
this.gridheight = gridheight;
}
public GBC setAnchor(int anchor)
{
this.anchor = anchor;
return this;
}
public GBC setFill(int fill)
{
this.fill = fill;
return this;
}
public GBC setWeight(double weightx, double weighty)
{
this.weightx = weightx;
this.weighty = weighty;
return this;
}
public GBC setInsets(int distance)
{
this.insets = new Insets(distance, distance, distance, distance);
return this;
}
public GBC setInsets(int top, int left, int bottom, int right)
{
this.insets = new Insets(top, left, bottom, right);
return this;
}

public GBC setIpad(int ipadx, int ipady)
{
this.ipadx = ipadx;
this.ipady = ipady;
return this;
}
}
Donate comment here