天天看点

java邮件发送(简单邮件、带附件邮件、带抄送人邮件)demo

package com.sdskjt.common.util;

import java.util.Properties;

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.mail.Address;

import javax.mail.BodyPart;

import javax.mail.Message;

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;

public class SendEmailUtil {

private MimeMessage mimeMsg; //MIME閭欢瀵硅薄

private Session session; //閭欢浼氳瘽瀵硅薄

private Properties props; //绯荤粺灞炴€? private boolean needAuth = false; //smtp鏄惁闇€瑕佽璇? //smtp璁よ瘉鐢ㄦ埛鍚嶅拰瀵嗙爜

private String username;

private String password;

private Multipart mp; //Multipart瀵硅薄,閭欢鍐呭,鏍囬,闄勪欢绛夊唴瀹瑰潎娣诲姞鍒板叾涓悗鍐嶇敓鎴怣imeMessage瀵硅薄

public SendEmailUtil(String smtp){

setSmtpHost(smtp);

createMimeMessage();

}

public void setSmtpHost(String hostName) {

System.out.println("璁剧疆绯荤粺灞炴€э細mail.smtp.host = "+hostName);

if(props == null)

props = System.getProperties(); //鑾峰緱绯荤粺灞炴€у璞? props.put(鈥渕ail.smtp.host鈥?hostName); //璁剧疆SMTP涓绘満

}

public boolean createMimeMessage()

{

try {

System.out.println(鈥滃噯澶囪幏鍙栭偖浠朵細璇濆璞★紒鈥?;

session = Session.getDefaultInstance(props,null); //鑾峰緱閭欢浼氳瘽瀵硅薄

}

catch(Exception e){

System.err.println(鈥滆幏鍙栭偖浠朵細璇濆璞℃椂鍙戠敓閿欒锛佲€?e);

return false;

}

System.out.println(鈥滃噯澶囧垱寤篗IME閭欢瀵硅薄锛佲€?;

try {

mimeMsg = new MimeMessage(session); //鍒涘缓MIME閭欢瀵硅薄

mp = new MimeMultipart();

return true;   
           

} catch(Exception e){

System.err.println(鈥滃垱寤篗IME閭欢瀵硅薄澶辫触锛佲€?e);

return false;

}

}

public void setNeedAuth(boolean need) {

System.out.println("璁剧疆smtp韬唤璁よ瘉锛歮ail.smtp.auth = "+need);

if(props == null) props = System.getProperties();

if(need){

props.put(鈥渕ail.smtp.auth鈥?鈥渢rue鈥?;

}else{

props.put(鈥渕ail.smtp.auth鈥?鈥渇alse鈥?;

}

}

public void setNamePass(String name,String pass) {

username = name;

password = pass;

}

public boolean setSubject(String mailSubject) {

System.out.println(鈥滆缃偖浠朵富棰橈紒鈥?;

try{

mimeMsg.setSubject(mailSubject);

return true;

}

catch(Exception e) {

System.err.println(鈥滆缃偖浠朵富棰樺彂鐢熼敊璇紒鈥?;

return false;

}

}

public boolean setBody(String mailBody) {

try{

BodyPart bp = new MimeBodyPart();

bp.setContent(""+mailBody,鈥渢ext/html;charset=GBK鈥?;

mp.addBodyPart(bp);

return true;   
           

} catch(Exception e){

System.err.println(鈥滆缃偖浠舵鏂囨椂鍙戠敓閿欒锛佲€?e);

return false;

}

}

public boolean addFileAffix(String filename) {

System.out.println(鈥滃鍔犻偖浠堕檮浠讹細鈥?filename);

try{

BodyPart bp = new MimeBodyPart();

FileDataSource fileds = new FileDataSource(filename);

bp.setDataHandler(new DataHandler(fileds));

bp.setFileName(fileds.getName());

mp.addBodyPart(bp);   
   
 return true;   
           

} catch(Exception e){

System.err.println(鈥滃鍔犻偖浠堕檮浠讹細鈥?filename+鈥滃彂鐢熼敊璇紒鈥?e);

return false;

}

}

public boolean setTo(String to){

if(to == null)return false;

try{

mimeMsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));

return true;

} catch(Exception e) {

return false;

}

}

public boolean setCopyTo(String copyto)

{

if(copyto == null)return false;

try{

mimeMsg.setRecipients(Message.RecipientType.CC,(Address[])InternetAddress.parse(copyto));

return true;

}

catch(Exception e)

{ return false; }

}

public boolean sendOut()

{

try{

mimeMsg.setContent(mp);

mimeMsg.saveChanges();

System.out.println(鈥滄鍦ㄥ彂閫侀偖浠垛€︹€?;

Session mailSession = Session.getInstance(props,null);   
 Transport transport = mailSession.getTransport("smtp");   
 transport.connect((String)props.get("mail.smtp.host"),username,password);   
 transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));   
 //transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.CC));   
 //transport.send(mimeMsg);   
   
 System.out.println("鍙戦€侀偖浠舵垚鍔燂紒");   
 transport.close();   
   
 return true;   
           

} catch(Exception e) {

e.printStackTrace();

// System.err.println(鈥滈偖浠跺彂閫佸け璐ワ紒鈥?e.printStackTrace());

return false;

}

}

public static boolean send(String smtp,String from,String to,String subject,String content,String username,String password) {

SendEmailUtil theMail = new SendEmailUtil(smtp);

theMail.setNeedAuth(true); //闇€瑕侀獙璇?

if(!theMail.setSubject(subject)) return false;

if(!theMail.setBody(content)) return false;

if(!theMail.setTo(to)) return false;

if(!theMail.setFrom(from)) return false;

theMail.setNamePass(username,password);

if(!theMail.sendOut()) return false;

return true;

}

public static boolean sendAndCc(String smtp,String from,String to,String copyto,String subject,String content,String username,String password) {

SendEmailUtil theMail = new SendEmailUtil(smtp);

theMail.setNeedAuth(true); //闇€瑕侀獙璇?

if(!theMail.setSubject(subject)) return false;

if(!theMail.setBody(content)) return false;

if(!theMail.setTo(to)) return false;

if(!theMail.setCopyTo(copyto)) return false;

if(!theMail.setFrom(from)) return false;

theMail.setNamePass(username,password);

if(!theMail.sendOut()) return false;

return true;

}

public static boolean send(String smtp,String from,String to,String subject,String content,String username,String password,String filename) {

SendEmailUtil theMail = new SendEmailUtil(smtp);

theMail.setNeedAuth(true); //闇€瑕侀獙璇?

if(!theMail.setSubject(subject)) return false;

if(!theMail.setBody(content)) return false;

if(!theMail.addFileAffix(filename)) return false;

if(!theMail.setTo(to)) return false;

if(!theMail.setFrom(from)) return false;

theMail.setNamePass(username,password);

if(!theMail.sendOut()) return false;

return true;

}

public static boolean sendAndCc(String smtp,String from,String to,String copyto,String subject,String content,String username,String password,String filename) {

SendEmailUtil theMail = new SendEmailUtil(smtp);

theMail.setNeedAuth(true); //闇€瑕侀獙璇?

if(!theMail.setSubject(subject)) return false;

if(!theMail.setBody(content)) return false;

if(!theMail.addFileAffix(filename)) return false;

if(!theMail.setTo(to)) return false;

if(!theMail.setCopyTo(copyto)) return false;

if(!theMail.setFrom(from)) return false;

theMail.setNamePass(username,password);

if(!theMail.sendOut()) return false;

return true;

}

public static void main(String[] args) {

/*String smtp = "smtp.163.com";  
 String from = "鍙戜欢绠?;  
 String to = "鏀朵欢绠?;  
 String copyto = "鎶勯€佷汉";  
 String subject = "涓婚";  
 String content = "鍐呭銆?;  
 String username="閭";  
 String password="xxx";//鎺堟潈鐮?*/ 
 
 /*String smtp = "smtphz.qiye.163.com";
 String to = "[email聽protected]";
 String subject = "涓婚";  
 String content = "鍐呭銆?; 
 
 String from="[email聽protected]";
 String username="[email聽protected]";
 String password="chenwh*2016";//鎺堟潈鐮?/


 String smtp = "mail.shandong.cn";//鐪侀偖鏈嶅姟鍣ㄥ湴鍧€
 String to = "[email聽protected]";//鏀朵欢绠? String subject = "涓婚";
 String content = "鍐呭銆?;

 String from="[email聽protected]";//鐪侀偖鍙戜欢绠? String username="[email聽protected]";//鐪侀偖鍙戜欢绠? String password="123456";//鐪侀偖瀵嗙爜
 String filename = "C:\\闄勪欢\\xxx.doc";
 SendEmailUtil.send(smtp, from, to,  subject, content, username, password);
           

// String from = 鈥淸email聽protected]鈥?

// String username=鈥淸email聽protected]鈥?

// String password=鈥渃henva*213鈥?

SendEmailUtil.send(smtp, from, to, subject, content, username, password,filename);

}
           

}

继续阅读