用java实现简单发送邮件功能的客户端:
- 发送邮件之前,先下载javax.mail的jar包,jar包地址:链接,然后导入到项目里面。
- 发送邮件包括①收件人(一个或者多个)②自己的账号③授权码(因为程序属于第三方登录,所有登录密码必须使用相关邮箱的授权码)④邮件标题⑤邮件文本内容⑥附件标题⑦附件内容。
- 下面是简单的用QQ邮箱发送邮件的demo代码,没有添加实现附件的功能,滑稽。
- 请多指教。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Send extends JFrame {
public Send() {
setTitle("Luyucheng邮箱");
setLocationRelativeTo(null);// 居中显示
setSize(800, 600); // 窗口大小
setResizable(false);// 是否可以改变窗口大小
this.getContentPane().add(new Panel());// 添加JPanel面板
setDefaultCloseOperation(EXIT_ON_CLOSE);// 设置关闭方式
setVisible(true); // 可视化
}
public static void main(String[] args) throws AddressException, MessagingException {
Send s=new Send();
}
}
class Panel extends JPanel {
public Panel() {
// TODO 自动生成的构造函数存根
this.setLayout(null);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
JLabel jl1 = new JLabel("收件人:");
JTextField jt = new JTextField();
jl1.setFont(new Font("华文仿宋", Font.PLAIN, 18));
jt.setColumns(35);
contentPane.add(BorderLayout.WEST, jl1);
contentPane.add(BorderLayout.CENTER, jt);
contentPane.setVisible(true);
contentPane.setBounds(0, 0, 800, 50);
this.add(contentPane);
JPanel contentPane2 = new JPanel();
contentPane2.setLayout(new BorderLayout());
JLabel jl2 = new JLabel("账号:");
JTextField jt2 = new JTextField();
jl2.setFont(new Font("华文仿宋", Font.PLAIN, 18));
jt2.setColumns(35);
contentPane2.add(BorderLayout.WEST, jl2);
contentPane2.add(BorderLayout.CENTER, jt2);
contentPane2.setVisible(true);
contentPane2.setBounds(0, 50, 800, 50);
this.add(contentPane2);
JPanel contentPane3 = new JPanel();
contentPane3.setLayout(new BorderLayout());
JLabel jl3 = new JLabel("授权码:");
JTextField jt3 = new JTextField();
jl3.setFont(new Font("华文仿宋", Font.PLAIN, 18));
jt3.setColumns(35);
contentPane3.add(BorderLayout.WEST, jl3);
contentPane3.add(BorderLayout.CENTER, jt3);
contentPane3.setVisible(true);
contentPane3.setBounds(0, 100, 800, 50);
this.add(contentPane3);
JPanel contentPane4 = new JPanel();
contentPane4.setLayout(new BorderLayout());
JLabel jl4 = new JLabel("标题:");
JTextField jt4 = new JTextField();
jl4.setFont(new Font("华文仿宋", Font.PLAIN, 18));
jt4.setColumns(35);
contentPane4.add(BorderLayout.WEST, jl4);
contentPane4.add(BorderLayout.CENTER, jt4);
contentPane4.setVisible(true);
contentPane4.setBounds(0, 150, 800, 50);
this.add(contentPane4);
JLabel jl5 = new JLabel("内容:");
jl5.setHorizontalAlignment(JTextField.CENTER);
jl5.setFont(new Font("华文仿宋", Font.PLAIN, 18));
jl5.setBounds(0, 200, 800, 50);
JTextArea jt5 = new JTextArea();
jt5.setLineWrap(true);
JScrollPane scroll = new JScrollPane(jt5);
//分别设置水平和垂直滚动条总是出现
scroll.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setBounds(0, 250, 800, 300);
JButton jb=new JButton("发送");
jb.setFont(new Font("华文仿宋", Font.PLAIN, 18));
jb.setBounds(0, 550, 800, 20);
this.add(jl5);
this.add(scroll);
this.add(jb);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
Util u=new Util();
u.setAddressee(jt.getText());
u.setAccount(jt2.getText());
u.setKey(jt3.getText());
u.setTitle(jt4.getText());
u.setText(jt5.getText());
try {
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 连接协议
properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
properties.put("mail.smtp.port", 465);// 端口号
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
// 得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
// 设置发件人邮箱地址
message.setFrom(new InternetAddress(u.getAccount()));
// 设置收件人邮箱地址
message.setRecipient(Message.RecipientType.TO, new InternetAddress(u.getAddressee()));// 一个收件人
// 多个收件人
// message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new
// InternetAddress("xxx@qq.com"),new InternetAddress("xxx@qq.com"),new
// InternetAddress("xxx@qq.com")});
// 设置邮件标题
message.setSubject(u.getTitle());
// 设置邮件内容
message.setText(u.getText());
// 得到邮差对象
Transport transport = session.getTransport();
// 连接自己的邮箱账户
transport.connect(u.getAccount(), u.getKey());// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
// 发送邮件
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
class Util {
public String getAddressee() {
return Addressee;
}
public void setAddressee(String addressee) {
Addressee = addressee;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
private String title;// 标题
private String text;// 内容文本
private String account;// 账号
private String key;// 授权码
private String Addressee;// 收件人
}