package com.jd.mail; import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; /** * javamail Client * User: zhangb * Date: 12-4-20 * Time: 上午12:53 */ public class EmailClient { public static void sendMail(String subject, String content, String sender, String password, String ... receivers) throws MessagingException { BodyPart mdp = new MimeBodyPart(); mdp.setContent(content, "text/html;charset=UTF-8"); Multipart mm = new MimeMultipart(); mm.addBodyPart(mdp); Properties props = new Properties(); props.put("mail.smtp.host", "smtp.163.com"); props.put("mail.smtp.auth", "true"); Session sendSession = Session.getDefaultInstance(props, new EmailAuthenticator(sender, password)); sendSession.setDebug(true); Message message = new MimeMessage(sendSession); message.setFrom(new InternetAddress(sender)); Address[] addresses = new InternetAddress[receivers.length]; for (int i = 0; i < receivers.length; i ++) { addresses[i] = new InternetAddress(receivers[i]); } message.setRecipients(Message.RecipientType.TO, addresses); message.setSubject(subject); message.setContent(mm); message.saveChanges(); Transport.send(message); } private static class EmailAuthenticator extends Authenticator { private String strUser; private String strPwd; public EmailAuthenticator(String user, String password) { this.strUser = user; this.strPwd = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(strUser, strPwd); } } }