使用spring框架
public static void main(String[] args) throws Exception {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("mail.fuiou.com"); //设置发送的邮件服务器地址
mailSender.setUsername("xuxl@fuiou.com"); //自己的账号
mailSender.setPassword("xlxu"); //密码
//使用JavaMail的MimeMessage,支付更加复杂的邮件格式和内容
MimeMessage msg = mailSender.createMimeMessage();
//创建MimeMessageHelper对象,处理MimeMessage的辅助类
MimeMessageHelper helper = new MimeMessageHelper(msg, true);
//使用辅助类MimeMessage设定参数
helper.setFrom(mailSender.getUsername());
helper.setTo("8758043@qq.com"); //对方的邮件地址
helper.setSubject("Hello Attachment");
helper.setText("This is a mail with attachment");
try {
for (int i = 0; i < 5; i++) {
FileSystemResource file = new FileSystemResource("C:\\Users\\admin\\Desktop\\work\\skin.png");//邮件的附件
helper.addAttachment(file.getFilename(), file);
}
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 发送邮件
mailSender.send(msg);
System.out.println("发送成功");
}