真实项目应用的java发送邮件,应该还待进一步完善。
依赖 mail-1.4.jar
jar包下载地址: http://mvnrepository.com/artifact/javax.mail/mail/1.4.7
import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; 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;
Mail.java
package com.testdemo.tool.zsmail; import java.io.Serializable; /** * Mail属性实体 * * @author shadow * */ @SuppressWarnings("serial") public class Mail implements Serializable { public static final String ENCODEING = "UTF-8"; private String sender;// ProperUtil.get("mail_account"); // 发件人的邮箱 private String name; // 发件人昵称 private String subject; // 主题 private String message; // 信息(支持HTML) private String[] receivers; // 收件人的邮箱 public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSubject() { return subject; } /** * 设置主题 * * @param subject */ public void setSubject(String subject) { this.subject = subject; } public String getMessage() { return message; } /** * 设置发送消息 * * @param message */ public void setMessage(String message) { this.message = message; } public String[] getReceivers() { return receivers; } /** * 设置接收者 * * @param receivers */ public void setReceivers(String[] receivers) { this.receivers = receivers; } }
MailTool.java
package com.testdemo.tool.zsmail; import java.util.Date; import java.util.Properties; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; 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 java.util.HashMap; public class MailTool { private static Properties prop = new Properties(); private static Session session = null; static { prop.put("mail.smtp.auth", "true"); prop.put("mail.smtp.host", "smtp.qq.com");//邮件服务器 prop.put("mail.smtp.port", "25"); prop.put("mail.transport.protocol", "smtp"); prop.put("mail_account", "");//qq帐号 prop.put("mail_password", "");//qq密码 if (null == session) session = Session.getInstance(prop); } public static synchronized HashMap<String, Object> sendMail(Mail mail) { String error = ""; boolean b = false; Message msg = null; BodyPart messageBodyPart = null; Multipart multipart = null; Transport tran = null; try { msg = new MimeMessage(session); // 建立信息 messageBodyPart = new MimeBodyPart(); multipart = new MimeMultipart(); msg.setFrom(new InternetAddress(prop.getProperty("mail_account"))); // 发件人 InternetAddress[] receivers = InternetAddress.parse(getMailList(mail.getReceivers())); msg.setRecipients(Message.RecipientType.TO, receivers); // 收件人 msg.setSentDate(new Date()); // 发送日期 msg.setSubject(mail.getSubject()); // 主题 msg.setText(mail.getMessage()); // 内容 // 显示以html格式的文本内容 messageBodyPart.setContent(mail.getMessage(), "text/html;charset=UTF-8"); multipart.addBodyPart(messageBodyPart); msg.setContent(multipart); tran = session.getTransport("smtp"); tran.connect(prop.getProperty("mail.smtp.host"), prop.getProperty("mail_account"), prop.getProperty("mail_password")); tran.sendMessage(msg, msg.getAllRecipients()); // 发送 b = true; } catch (Exception e) { e.printStackTrace(); b = false; error = e.getMessage(); } finally { System.out.println("邮件发送" + (b == true ? "成功" : "失败")); try { if (null != tran) tran.close(); } catch (MessagingException e) { e.printStackTrace(); } } HashMap<String, Object> map = new HashMap<String, Object>(); map.put("status", b); map.put("error", error); return map; } public static String getMailList(String[] mailArray) { StringBuffer toList = new StringBuffer(); int length = mailArray.length; if (mailArray != null && length < 2) { toList.append(mailArray[0]); } else { for (int i = 0; i < length; i++) { toList.append(mailArray[i]); if (i != (length - 1)) { toList.append(","); } } } return toList.toString(); } public static void help(){ System.out.println( "public static void main(String[] args) { "+ " Mail mail = new Mail();//基本对象 "+ " mail.setReceivers(new String[] { "1713930654@qq.com", "xiao@163.com" });//接收者数组 "+ " mail.setSubject("subject");//主题 "+ " mail.setMessage("message");//内容 "+ " sendMail(mail);//发送"); "+ "}"); } public static void main(String[] args) { // Mail mail = new Mail();//基本对象 // mail.setReceivers(new String[] { "1713930654@qq.com", "389522975@qq.com" });//接收者数组 // mail.setSubject("subject");//主题 // mail.setMessage("message");//内容 // sendMail(mail);//发送 help(); } }