- package com.forenms.ycczj.email;
- import javax.mail.Authenticator;
- import javax.mail.PasswordAuthentication;
- /**
- * 進行郵箱認證
- *
- *
- */
- public class EmailAutherticator extends Authenticator{
- private String userName;
- private String passWord;
- public EmailAutherticator(String userName, String passWord) {
- super();
- this.setUserName(userName);
- this.setPassWord(passWord);
- }
- public PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(userName, passWord);
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getPassWord() {
- return passWord;
- }
- public void setPassWord(String passWord) {
- this.passWord = passWord;
- }
- }
- package com.forenms.ycczj.email;
- import java.util.Date;
- import java.util.Properties;
- import java.util.ResourceBundle;
- import javax.activation.DataHandler;
- import javax.activation.FileDataSource;
- import javax.mail.Authenticator;
- 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 javax.mail.internet.MimeUtility;
- public class SendMail {
- private static ResourceBundle bunder=ResourceBundle.getBundle("email");
- public static void main(String args[])throws Exception{
- String smsMob[]=new String[]{"[email protected]"};
- System.out.println(SendMail.sendMail("title 郵件的標題", smsMob, "content 郵件的內容"));
- }
- /**
- * 發送郵件
- * @param subject 標題
- * @param to 接收方
- * @param text 內容
- * @param mimeType 類型("text/html;charset=gb2312")
- * @throws Exception
- */
- public static boolean sendMail(String subject, String[] to,String text) {
- boolean result=false;
- String smtp = bunder.getString("mail.smtp.host");
- String serverName = bunder.getString("mail.qq");
- String serverPaswd = bunder.getString("mail.from.password");
- String userMail=bunder.getString("mail.from");
- //設置連接郵件服務器屬性
- Properties properties = System.getProperties();
- properties.put("mail.smtp.host", smtp);
- properties.put("mail.smtp.auth", "true");
- //進行郵件服務器認證
- EmailAutherticator emailAuther = new EmailAutherticator(serverName, serverPaswd);
- //獲取連接郵件服務器會話
- Session session = Session.getInstance(properties,(Authenticator)emailAuther);
- //設置郵件正文
- MimeMessage mimeMessage = new MimeMessage(session);
- Multipart multipart = new MimeMultipart();
- try {
- //設置發送郵件人地址
- InternetAddress sentFrom = new InternetAddress(userMail);
- mimeMessage.setFrom(sentFrom);
- //設置接收郵件人地址
- InternetAddress[] sendTo = new InternetAddress[to.length];
- for (int i = 0; i < to.length; i++) {
- sendTo[i] = new InternetAddress(to[i]);
- }
- //mimeMessage.setRecipients(MimeMessage.RecipientType.TO, sendTo);
- mimeMessage.setRecipients(MimeMessage.RecipientType.BCC,sendTo);
- //設置郵件標題
- mimeMessage.setSubject(subject, "UTF-8");
- //加入郵件正文信息
- MimeBodyPart mimeBodyPartHtml = new MimeBodyPart();
- //加入html
- mimeBodyPartHtml.setContent(text, "text/html;charset=gb2312");
- multipart.addBodyPart(mimeBodyPartHtml);
- //加入郵件正文
- mimeMessage.setContent(multipart);
- //加入發送時間
- mimeMessage.setSentDate(new Date());
- //保存信息
- mimeMessage.saveChanges();
- //發送郵件
- Transport.send(mimeMessage);
- result=true;
- } catch (Exception e) {
- //e.printStackTrace();
- return false;
- }
- return result;
- }
- /**
- * 發送郵件(加入附件)
- * @param subject 標題
- * @param to 接收方
- * @param text 內容
- * @param fileName 發送文件
- * @param mimeType 類型("text/html;charset=gb2312")
- * @throws Exception
- */
- public static void sendMailToFile(String subject, String[] to,String text, String fileName) throws Exception {
- String smtp = "smtp.163.com";
- String serverName = "xxxxxx";
- String serverPaswd = "xxxxxxxxx";
- String userMail="[email protected]";
- //設置連接郵件服務器屬性
- Properties properties = System.getProperties();
- properties.put("mail.smtp.host", smtp);
- properties.put("mail.smtp.auth", "true");
- //進行郵件服務器認證
- EmailAutherticator emailAuther = new EmailAutherticator(serverName, serverPaswd);
- //獲取連接郵件服務器會話
- Session session = Session.getInstance(properties,(Authenticator)emailAuther);
- //設置郵件正文
- MimeMessage mimeMessage = new MimeMessage(session);
- Multipart multipart = new MimeMultipart();
- //設置發送郵件人地址
- InternetAddress sentFrom = new InternetAddress(userMail);
- mimeMessage.setFrom(sentFrom);
- //設置接收郵件人地址
- InternetAddress[] sendTo = new InternetAddress[to.length];
- for (int i = 0; i < to.length; i++) {
- sendTo[i] = new InternetAddress(to[i]);
- }
- //mimeMessage.setRecipients(MimeMessage.RecipientType.TO, sendTo);
- mimeMessage.setRecipients(MimeMessage.RecipientType.BCC,sendTo);
- //設置郵件標題
- mimeMessage.setSubject(subject, "UTF-8");
- //加入郵件正文信息
- MimeBodyPart mimeBodyPartHtml = new MimeBodyPart();
- //加入html
- mimeBodyPartHtml.setContent(text, "text/html;charset=gb2312");
- multipart.addBodyPart(mimeBodyPartHtml);
- //加入附件
- MimeBodyPart mimeBodyPartFile = new MimeBodyPart();
- //附件文件路徑
- String filename = fileName.split(",")[0];
- //附件文件名稱
- String displayname = fileName.split(",")[1];
- //獲取文件流
- FileDataSource fds = new FileDataSource(filename);
- mimeBodyPartFile.setDataHandler(new DataHandler(fds));
- mimeBodyPartFile.setFileName(MimeUtility.encodeText(displayname));
- multipart.addBodyPart(mimeBodyPartFile);
- //加入郵件正文
- mimeMessage.setContent(multipart);
- //加入發送時間
- mimeMessage.setSentDate(new Date());
- //保存信息
- mimeMessage.saveChanges();
- //發送郵件
- Transport.send(mimeMessage);
- }
- }
email.properties
- mail.smtp.host=smtp.qq.com
- mail.smtp.auth=true
- [email protected]
- mail.qq=xxxxxx
- mail.from.password=******
- mail.smtp.timeout=25000
- mail.smtp.starttls.enable=true