spring mail 发送邮件
生活随笔
收集整理的這篇文章主要介紹了
spring mail 发送邮件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近在項目中用到了發送郵件的功能(用戶注冊郵箱激活、用戶密碼重置郵箱獲取驗證碼等等),所以寫了一下java的郵件發送。
java mail
java mail是最早出現的java郵件發送,以下是它的使用方式(引入jar包:activation.jar、mail.jar) package com.lingjuli.servlet;import java.util.Date; import java.util.Properties;import javax.mail.Authenticator; import javax.mail.BodyPart; 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;public class SendMail {private static String username = "luo.xxx@163.com";private static String password = "xxx";private static String smtpServer = "smtp.163.com";//郵件協議private static String fromMailAddress = "luo.xxx@163.com";private static String toMailAddress = "xxx@sina.com";public static void main(String[] args) throws Exception {Properties props = new Properties();props.put("mail.smtp.auth", "true");props.put("mail.smtp.host", smtpServer);// 獲得郵件會話對象Session session = Session.getDefaultInstance(props,new SmtpAuthenticator(username, password));/** *************************************************** */// 創建MIME郵件對象MimeMessage mimeMessage = new MimeMessage(session);mimeMessage.setFrom(new InternetAddress(fromMailAddress));// 發件人mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toMailAddress));// 收件人mimeMessage.setSubject("主題");mimeMessage.setSentDate(new Date());// 發送日期BodyPart mdp = new MimeBodyPart();// 新建一個存放信件內容的BodyPart對象mdp.setContent("測試java郵件發送", "text/html;charset= GB2312");// 設置發送郵件內容為HTML類型,并為中文編碼Multipart mm = new MimeMultipart();mm.addBodyPart(mdp);mimeMessage.setContent(mm);mimeMessage.saveChanges();Transport.send(mimeMessage);// 發送郵件}} /*** Smtp認證*/ class SmtpAuthenticator extends Authenticator {String username = null;String password = null;// SMTP身份驗證public SmtpAuthenticator(String username, String password) {this.username = username;this.password = password;}public PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(this.username, this.password);}}spring mail?
通過spring對java mail的上層封裝,以下為使用方式
package com.zwx.utils;import java.io.File;import javax.mail.MessagingException; import javax.mail.internet.MimeMessage;import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper;public class MailSender {/*private static String username = "luo.xxx@163.com";private static String password = "xxx";private static String smtpServer = "smtp.163.com";private static String fromMailAddress = "luo.xxx@163.com";private static String toMailAddress = "xxx@sina.com";*/private static PropertiesLoader propertiesLoader = new PropertiesLoader("bdsc-web.properties");//讀取配置文件public static void mailSimple(String toMailSAddress,String subject,String content) {// 發送器 JavaMailSenderImpl mailSender= new JavaMailSenderImpl();// 建立郵件消息,發送簡單郵件和html郵件的區別MimeMessage mailMessage = mailSender.createMimeMessage();// 為防止亂碼,添加編碼集設置MimeMessageHelper messageHelper = null;try {//發送附件 則 參數為 multipart 為 turemessageHelper = new MimeMessageHelper(mailMessage,true,"UTF-8");} catch (MessagingException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}//設定mail servermailSender.setHost(propertiesLoader.getProperty("mail.smtp.host"));mailSender.setPort(propertiesLoader.getInteger("mail.smtp.port"));mailSender.setUsername(propertiesLoader.getProperty("mail.smtp.username"));mailSender.setPassword(propertiesLoader.getProperty("mail.smtp.password"));//建立郵件消息//設置收件人、寄件人try {messageHelper.setTo(toMailSAddress);messageHelper.setFrom(propertiesLoader.getProperty("mail.smtp.username"));messageHelper.setSubject(subject);messageHelper.setText(content,true);//附件內容messageHelper.addAttachment("附件1", new File("h:/test/abc.pdf"));messageHelper.addAttachment("附件2", new File("h:/test/qwe.gif"));} catch (MessagingException e) {// TODO Auto-generated catch blocke.printStackTrace();}//發送郵件mailSender.send(mailMessage);System.out.println("郵件發送成功!");}public static void main(String[] args) {try {MailSender.mailSimple("xxx@sina.com","qwe","qwe");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}} }bdsc-web.properties
#spring mail mail.smtp.username = luo.xxx@163.com mail.smtp.password = xxx mail.smtp.host = smtp.163.com mail.smtp.auth = true mail.smtp.timeout = 1000總結
以上是生活随笔為你收集整理的spring mail 发送邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springmvc + ajaxfile
- 下一篇: 测血压的智能手表