import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Multipart;
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.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class SendMail extends JFrame implements ActionListener{
public static String myEmailAccount;
public static String myEmailPassword;
// 发件人邮箱的 SMTP 服务器地址, 必须准确, 不同邮件服务器地址不同, 一般(只是一般, 绝非绝对)格式为: smtp.xxx.com
// 网易163邮箱的 SMTP 服务器地址为: smtp.163.com
public static String myEmailSMTPHost ;
public static String cc;
public static String subject;
public static String content;
// 收件人邮箱(替换为自己知道的有效邮箱)
public static String receiveMailAccount;
String labelName[]={"发送地址","发送密码","接收地址(多个地址用逗号隔开)","抄送地址","smtp服务器","邮件主题","邮件内容"};
JTextField text[]=new JTextField[labelName.length];
JButton button=new JButton("发送");
JLabel label[]=new JLabel[labelName.length];
JButton sel=new JButton("添加文件");
JTextField f=new JTextField();
public SendMail()
{
this.setLayout(new GridLayout(9,2));
this.setSize(400, 400);
for(int i=0;i<labelName.length;i++)
{
if(i!=1)
text[i]=new JTextField();
else
text[i]=new JPasswordField();
label[i]=new JLabel(labelName[i]);
this.add(label[i]);
this.add(text[i]);
}
this.add(sel);
this.add(f);
sel.addActionListener(this);
this.add(button);
button.addActionListener(this);
this.setVisible(true);
}
public static void main(String[] args) throws Exception {
// 1. 创建参数配置, 用于连接邮件服务器的参数配置
new SendMail();
}
public void send() throws Exception
{
Properties props = new Properties(); // 参数配置
props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
props.setProperty("mail.smtp.host", myEmailSMTPHost); // 发件人的邮箱的 SMTP 服务器地址
props.setProperty("mail.smtp.auth", "true"); // 需要请求认证
Session session = Session.getInstance(props);
session.setDebug(true); // 设置为debug模式, 可以查看详细的发送 log
// 3. 创建一封邮件
MimeMessage message = createMimeMessage(session, receiveMailAccount);
// 4. 根据 Session 获取邮件传输对象
Transport transport = session.getTransport();
transport.connect(myEmailAccount, myEmailPassword);
// 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
transport.sendMessage(message, message.getAllRecipients());
// 7. 关闭连接
transport.close();
}
public MimeMessage createMimeMessage(Session session, String receiveMail) throws Exception {
// 1. 创建一封邮件
MimeMessage message = new MimeMessage(session);
// 2. From: 发件人(昵称有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改昵称)
message.setFrom(new InternetAddress(myEmailAccount));
// 3. To: 收件人(可以增加多个收件人、抄送、密送)
InternetAddress[] internetAddressTo = new InternetAddress().parse(receiveMail);
message.setRecipients(MimeMessage.RecipientType.TO, internetAddressTo);
message.setRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(cc));
// 4. Subject: 邮件主题(标题有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改标题)
message.setSubject(subject);
Multipart multipart = new MimeMultipart();
// 添加邮件正文
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(subject, "text/html;charset=UTF-8");
multipart.addBodyPart(contentPart);
String fi[]=f.getText().trim().split(";");
for(String s:fi)
{
System.out.println(s);
File file=new File(s);
BodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(MimeUtility.encodeWord(file.getName()));
multipart.addBodyPart(attachmentBodyPart);
}
message.setContent(multipart);
// 5. Content: 邮件正文(可以使用html标签)(内容有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改发送内容)
// 6. 设置发件时间
message.setSentDate(new Date());
// 7. 保存设置
message.saveChanges();
return message;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==sel)
{
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.showDialog(new JLabel(), "选择");
File file = chooser.getSelectedFile();
if(!f.getText().trim().equals(""))
f.setText(f.getText().trim()+";"+file.getAbsoluteFile().toString());
else
f.setText(file.getAbsoluteFile().toString());
}else
{
myEmailAccount=text[0].getText().trim();
myEmailPassword=text[1].getText().trim();
receiveMailAccount=text[2].getText().trim();
cc=text[3].getText().trim();
myEmailSMTPHost=text[4].getText().trim();
subject=text[5].getText().trim();
content=text[6].getText().trim();
try {
send();
} catch (Exception ev) {
// TODO Auto-generated catch block
ev.printStackTrace();
}
}
}
}
此程序还需添加java mail包