java 邮件收发

代码块:

package com.real.tool;


import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.Font;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import java.util.Properties;


import javax.activation.DataHandler;

import javax.activation.DataSource;

import javax.activation.FileDataSource;

import javax.mail.Address;

import javax.mail.Authenticator;

import javax.mail.Message;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

import javax.mail.internet.MimeUtility;

import javax.mail.internet.MimeMessage.RecipientType;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.border.BevelBorder;

import javax.swing.border.Border;

import javax.swing.border.EtchedBorder;


/*

使用JAVA API接口编程的邮件收发*/


public class Email_Frame extends JFrame{



private JLabel maitLabel;

private JLabel mailPasswordLable;

private JTextField mailText;

private JPasswordField passwordText;

private JPanel mailLoginPanel;

private JLabel subject;

private JTextField subjectText;

private JComboBox mailbox;

private String MailServer;

private JLabel mainContent;

private JTextArea contentText;

private JLabel address;

private JPanel contentPanel;

private JTextArea addText;

private JPanel addressPanel;

private JButton sendSuccess;

private JButton sendAgain;

private JButton sendButton;

private JPanel buttonPanel;

private String name;

private JTextField boyField;

private JTextField boyField1;

private JTextField boyField2;



public Email_Frame(){

super("邮件");

this.setSize(550, 600);

//获取面板

Container container=getContentPane();

//发件人邮箱

maitLabel=new JLabel("邮 箱 :",JLabel.CENTER);

mailPasswordLable=new JLabel("密 码 :",JLabel.CENTER);

mailText=new JTextField(10);

passwordText=new JPasswordField(10);

mailLoginPanel=new JPanel();

mailLoginPanel.setBackground(Color.WHITE);

mailLoginPanel.setLayout(new GridLayout(3,4));

//邮件主题

subject=new JLabel("主 题  :",JLabel.CENTER);

subjectText=new JTextField("", 10);

mailbox = new JComboBox();

mailbox.addItem("请选择");

mailbox.addItem("@qq.com");

mailbox.addItem("@sina.com");

mailbox.addItem("@126.com");

mailbox.addItem("@163.com");

mailbox.addItem("@139.com");

mailbox.addItem("@sina.cn");

mailbox.addItemListener(new ItemListener() {


public void itemStateChanged(ItemEvent e) {

String server = e.getItem().toString().trim();

if(server.equals("请选择您的邮箱")){

JOptionPane.showMessageDialog(null, "请选择你的服务器邮箱", "友情提示", JOptionPane.INFORMATION_MESSAGE);

}

if(server.equals("@qq.com")){

name =mailText.getText().trim()+ "@qq.com";

MailServer = "smtp.qq.com";

return;

}

else if(server.equals("@sina.com")){

name =  mailText.getText().trim()+"@sina.com";

MailServer ="smtp.sina.com";

return;

}

else if(server.equals("@126.com")){

name= mailText.getText().trim()+"[email protected]";

MailServer= "smtp.126.com";

return;

}

else if(server.equals("@163.com")){

name= mailText.getText().trim()+"[email protected]";

MailServer = "smtp.163.com";

return;

}

else{

name= mailText.getText().trim()+".139.com";

MailServer = "smtp.139.com";

return;

}

}

});

mailLoginPanel.add(maitLabel);

mailLoginPanel.add(mailText);

mailLoginPanel.add(mailbox);

mailLoginPanel.add(new JLabel(""));

mailLoginPanel.add(mailPasswordLable);

mailLoginPanel.add(passwordText);

mailLoginPanel.add(new JLabel(""));

mailLoginPanel.add(new JLabel(""));

mailLoginPanel.add(subject);

mailLoginPanel.add(subjectText);

mailLoginPanel.add(new JLabel(""));

mailLoginPanel.add(new JLabel(""));

//邮件正文


contentPanel=new JPanel();

Border border = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);

 border = BorderFactory.createTitledBorder(border, "");

contentPanel.setBorder(border);

contentPanel.setLayout(new BorderLayout());

buttonPanel = new JPanel();

buttonPanel.setBackground(Color.WHITE);

buttonPanel.setLayout(new GridLayout(3,2));

boyField  = new JTextField();

boyField1  = new JTextField();

boyField2  = new JTextField();

JLabel boyLabel = new JLabel(":附件1 注:附件的格式:xx:\\xx\\xx.xx",JLabel.LEFT);

JLabel boyLabel2 = new JLabel(":附件2",JLabel.LEFT);

JLabel boyLabel3 = new JLabel(":附件3",JLabel.LEFT);

buttonPanel.add(boyField);

buttonPanel.add(boyLabel);

buttonPanel.add(boyField1);

buttonPanel.add(boyLabel2);

buttonPanel.add(boyField2);

buttonPanel.add(boyLabel3);

contentPanel.add(buttonPanel,BorderLayout.NORTH);

contentText=new JTextArea(8,45);

Border borde2 = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);

borde2 = BorderFactory.createTitledBorder(borde2, "邮箱正文");

contentText.setBorder(borde2);

contentText.setFont(new Font("Serif",Font.PLAIN,14));

contentText.setLineWrap(true);

contentText.setEditable(true);

contentPanel.add(contentText,BorderLayout.CENTER);

//目的邮箱

address=new JLabel("目的邮箱:");

addText=new JTextArea(1,25);

Border borde1 = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);

borde1 = BorderFactory.createTitledBorder(borde1, "");

addText.setBorder(borde1);

addText.setText("[email protected]");

addText.setEditable(false);

addressPanel=new JPanel();

//发送按钮

sendButton=new JButton("发送");

sendButton.addActionListener(new ActionListener(){


public void actionPerformed(ActionEvent arg0) {

if(name.equals("")){

JOptionPane.showMessageDialog(null, "发信人不能为空!", "温馨提示", JOptionPane.INFORMATION_MESSAGE);

sendButton.setVisible(false);

     return;

}

if(mailbox.getSelectedItem().equals("请选择")){

JOptionPane.showMessageDialog(null, "请选择邮箱服务器!", "温馨提示", JOptionPane.INFORMATION_MESSAGE);

sendButton.setVisible(false);

     return;

}

if(passwordText.getText().equals("")){

JOptionPane.showMessageDialog(null, "请输出密码...", "温馨提示", JOptionPane.INFORMATION_MESSAGE);

sendButton.setVisible(false);

     return;

}

if(subjectText.getText().equals("")){

JOptionPane.showConfirmDialog(null,"你不需要设置主题吗?","系统提示",0);

}

if(sendButton.getText().equals("重新发送")){

boyField.setText("");

boyField1.setText("");

boyField2.setText("");

contentText.setText("");

addText.setText("xxx");

sendButton.setText("发送");

}

else{

try{

//邮件的配置文件

Properties props = new Properties();

props.setProperty("mail.transport.protocol", "smtp");

props.setProperty("mail.smtp.auth", "true");

props.setProperty("mail.host", MailServer);

//系统的配置环境

Session session =Session.getInstance(props,new Authenticator(){


@Override

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(name,passwordText.getText());//认证用户名和密码

}

});

session.setDebug(true);

Message msg = new MimeMessage(session);

//邮件发送人来源

msg.setFrom(new InternetAddress("\"" + MimeUtility.encodeText("使用者")+ "\" <"+name+">"));

//回复给谁

msg.setReplyTo( new Address[] {new InternetAddress(name)});

//可以实现群发

msg.setRecipients(RecipientType.TO, InternetAddress.parse( MimeUtility.encodeText("新浪") + "<xxxx>," + MimeUtility.encodeText("腾讯") +  " <xxx>"));

//邮件主题

msg.setSubject(subjectText.getText());

//定义一个混合体

MimeMultipart multipart =new MimeMultipart("mixed");

msg.setContent(multipart);

//混合体的body

MimeBodyPart bodyPart1 = new MimeBodyPart();

MimeBodyPart bodyPart2 = new MimeBodyPart();

MimeBodyPart   content = new MimeBodyPart();

//将body加入到混合体

multipart.addBodyPart(bodyPart1);

multipart.addBodyPart(bodyPart2);

multipart.addBodyPart(content);

//文件附件,但是注意文件路径

DataSource ds = new FileDataSource(boyField.getText());  

DataHandler dh =new DataHandler(ds );

bodyPart1.setDataHandler(dh );

bodyPart1.setFileName(MimeUtility.encodeText("文本附件:"));//附件的名字,注意需要对中文编码。

DataSource ds1 = new FileDataSource(boyField1.getText());

DataHandler dh1 =new DataHandler(ds1 );

bodyPart2.setDataHandler(dh1 );

bodyPart2.setFileName(MimeUtility.encodeText("HTML附件:"));

MimeMultipart bodyMultipart=new MimeMultipart("rerated");

content.setContent(bodyMultipart);

MimeBodyPart html = new MimeBodyPart();

MimeBodyPart text = new MimeBodyPart();

bodyMultipart.addBodyPart(html );

bodyMultipart.addBodyPart(text);

DataHandler tdh = new DataHandler("",contentText.getText());

text.setDataHandler(tdh);

FileDataSource ds2 = new FileDataSource(boyField2.getText());

DataHandler dh2 = new DataHandler(ds2 );

html.setDataHandler(dh2 );

html.setFileName(MimeUtility.encodeText("照片附件:"));

//msg.setText(contentText.getText());

msg.saveChanges();//保存文件

msg.setRecipients(RecipientType.TO, InternetAddress.parse("xxx"));

Transport.send(msg);//发送邮件

address.setText("温馨提示:");

addText.setText("您已经成功给我们发邮件...");

sendButton.setText("重新发送");

}catch(Exception e){

e.printStackTrace();

JOptionPane.showMessageDialog(null,

"发送失败...", "友情提示", JOptionPane.INFORMATION_MESSAGE);

address.setText("发送失败了,请重新发送.....");

addText.setVisible(false);

sendButton.setText("重新发送");

}

}

}

});

addressPanel.add(address,BorderLayout.SOUTH);

addressPanel.add(addText,BorderLayout.SOUTH);

addressPanel.add(sendButton,BorderLayout.SOUTH);

//设置底板颜色

contentPanel.setBackground(Color.WHITE);

addressPanel.setBackground(Color.WHITE);

//将组件添加到面板上

container.add(mailLoginPanel,BorderLayout.NORTH);

container.add(contentPanel,BorderLayout.CENTER);

container.add(addressPanel,BorderLayout.SOUTH);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String[] args){

try{

new Email_Frame().setVisible(true);

}catch(Exception e){

System.out.println("New类出现错误... 错误如下:");

e.printStackTrace();

}

}

}


你可能感兴趣的:(java,邮件)