import java.io.File; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.Multipart; 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; public class EmailSender { private static final String charset = "GBK"; private static final String defaultMimetype = "text/plain"; public static void main(String[] args) throws Exception { EmailSender.send(new String[]{"2345678@qq.com"}, "郵件測試xx", "<b>這是個測試</b>", null , "text/html"); } /** * 發送郵件 * @param receivers 收件人 * @param subject 標題 * @param mailContent 郵件內容 * @param attachements 附件 * @param mimetype 內容類型 默認爲text/plain,若是要發送HTML內容,應設置爲text/html */ public static void send(String[] receivers, String subject, String mailContent, File[] attachements, String mimetype) { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.qq.com");//smtp服務器地址 sohu props.put("mail.smtp.auth", "true");//須要校驗 Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("854334592","12345678");//登陸用戶名/密碼 } }); session.setDebug(true); try { MimeMessage mimeMessage = new MimeMessage(session); mimeMessage.setFrom(new InternetAddress("854334592@qq.com"));//發件人郵箱 InternetAddress[] toAddress = new InternetAddress[receivers.length]; for (int i=0; i<receivers.length; i++) { toAddress[i] = new InternetAddress(receivers[i]); } mimeMessage.setRecipients(Message.RecipientType.TO, toAddress);//收件人郵件 mimeMessage.setSubject(subject, charset); Multipart multipart = new MimeMultipart(); //正文 MimeBodyPart body = new MimeBodyPart(); // body.setText(message, charset);不支持html body.setContent(mailContent, (mimetype!=null && !"".equals(mimetype) ? mimetype : defaultMimetype)+ ";charset="+ charset); multipart.addBodyPart(body);//發件內容 //附件 if(attachements!=null){ for (File attachement : attachements) { MimeBodyPart attache = new MimeBodyPart(); //ByteArrayDataSource bads = new ByteArrayDataSource(byte[],"application/x-any"); attache.setDataHandler(new DataHandler(new FileDataSource(attachement))); String fileName = getLastName(attachement.getName()); attache.setFileName(MimeUtility.encodeText(fileName, charset, null)); multipart.addBodyPart(attache); } } mimeMessage.setContent(multipart); // SimpleDateFormat formcat = new SimpleDateFormat("yyyy-MM-dd"); mimeMessage.setSentDate(new Date());//formcat.parse("2010-5-23") Transport.send(mimeMessage); } catch (Exception e) { e.printStackTrace(); } } private static String getLastName(String fileName) { int pos = fileName.lastIndexOf("\\"); if (pos > -1) { fileName = fileName.substring(pos + 1); } pos = fileName.lastIndexOf("/"); if (pos > -1) { fileName = fileName.substring(pos + 1); } return fileName; } }