socket实现邮件发送
2012-10-16 15:45
405 查看
通过java的网络编程特性,完成邮件发送的功能,效果图如下:
首先新建一个maiTest的java项目,
然后新建GBC.java和MailTest.java文件
GBC.java代码如下:
package ch02;
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;
}
}
MailTest.java的代码如下:
package ch02;
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;
}
运行即出现效果图
首先新建一个maiTest的java项目,
然后新建GBC.java和MailTest.java文件
GBC.java代码如下:
package ch02;
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;
}
}
MailTest.java的代码如下:
package ch02;
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;
}
运行即出现效果图
相关文章推荐
- java使用Socket实现SMTP协议发送邮件
- socket实现邮件发送
- 使用Pear和php socket来实现通过smtp验证发送邮件
- php基于socket实现SMTP发送邮件的方法
- Socket实现的SMTP邮件发送客户端。
- 再谈用java实现Smtp发送邮件之Socket编程
- 再谈用java实现Smtp发送邮件之Socket编程
- Java通过socket实现smtp协议发送邮件
- 用socket发送匿名邮件之python实现
- 如何在代理环境下发送邮件?——C#实现Socket代理方式的邮件发送
- php基于socket实现SMTP发送邮件的方法
- C#基于SMTP协议和SOCKET通信,实现邮件内容和附件的发送,并可隐藏收件人
- C#基于SMTP协议和SOCKET通信,实现邮件内容和附件的发送,并可隐藏收件人
- 如何在代理环境下发送邮件?——C#实现Socket代理方式的邮件发送
- android通过socket实现发送邮件
- 在采集程序中增加定时发送邮件以及关机处理的实现
- python实现邮件发送
- 羊皮书APP(Android版)开发系列(十三)Android 邮件发送的实现
- 基于Node.js实现nodemailer邮件发送
- 调用系统的短信和邮件功能,实现发送指定内容等