標籤:exists html min res sage rda void etc message
package com.sun.mail;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
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 String username = null;
private String password = null;
private Authenticator auth = null;
private MimeMessage mimeMessage =null;
private Properties pros = null;
private Multipart multipart = null;
private BodyPart bodypart= null;
/**
* 初始化帳號密碼並驗證
* 建立MimeMessage對象
* 發送郵件必須的步驟:1
* @param username
* @param password
*/
public SendMail(String username,String password){
this.username = username;
this.password = password;
}
/**
* 初始化MimeMessage對象
* 發送郵件必須的步驟:3
*/
public void initMessage(){
this.auth = new Email_Autherticator();
Session session = Session.getDefaultInstance(pros,auth);
mimeMessage = new MimeMessage(session);
}
/**
* 設定email系統參數
* 接收一個map集合key為string類型,值為String
* 發送郵件必須的步驟:2
* @param map
*/
public void setPros(Map<String,String> map){
pros = new Properties();
for(Map.Entry<String,String> entry:map.entrySet()){
pros.setProperty(entry.getKey(), entry.getValue());
}
}
/**
* 驗證帳號密碼
* 發送郵件必須的步驟
* @author Administrator
*
*/
public class Email_Autherticator extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
}
/**
* 設定發送郵件的基本參數(去除繁瑣的郵件設定)
* @param sub 設定郵件主題
* @param text 設定郵件常值內容
* @param rec 設定郵件接收人
* @throws MessagingException
* @throws UnsupportedEncodingException
*/
public void setDefaultMessagePros(String sub,String text,String rec) throws MessagingException, UnsupportedEncodingException{
mimeMessage.setSubject(sub);
mimeMessage.setText(text);
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(rec));
mimeMessage.setSentDate(new Date());
mimeMessage.setFrom(new InternetAddress(username,username));
}
/**
* 設定主題
* @param subject
* @throws MessagingException
*/
public void setSubject(String subject) throws MessagingException{
mimeMessage.setSubject(subject);
}
/**
* 設定日期
* @param date
* @throws MessagingException
*/
public void setDate(Date date) throws MessagingException{
mimeMessage.setSentDate(new Date());
}
/**
* 設定郵件常值內容
* @param text
* @throws MessagingException
*/
public void setText(String text) throws MessagingException{
mimeMessage.setText(text);
}
/**
* 設定郵件標頭部
* @param arg0
* @param arg1
* @throws MessagingException
*/
public void setHeader(String arg0,String arg1) throws MessagingException{
mimeMessage.setHeader(arg0, arg1);
}
/**
* 設定郵件接收人地址 <單人發送>
* @param recipient
* @throws MessagingException
*/
public void setRecipient(String recipient) throws MessagingException{
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
}
/**
* 設定郵件接收人地址 <多人發送>
* @param list
* @throws MessagingException
* @throws AddressException
*/
public String setRecipients(List<String> recs) throws AddressException, MessagingException{
if(recs.isEmpty()){
return "接收人地址為空白!";
}
for(String str:recs){
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(str));
}
return "加入接收人地址成功!";
}
/**
* 設定郵件接收人地址 <多人發送>
* @param StringBuffer<parms,parms2,parms.....>
* @throws MessagingException
* @throws AddressException
*/
@SuppressWarnings("static-access")
public String setRecipients(StringBuffer sb) throws AddressException, MessagingException{
if(sb==null||"".equals(sb)){
return "字串資料為空白!";
}
Address []address = new InternetAddress().parse(sb.toString());
mimeMessage.addRecipients(Message.RecipientType.TO, address);
return "收件者加入成功";
}
/**
* 設定郵件發送人的名字
* @param from
* @throws UnsupportedEncodingException
* @throws MessagingException
*/
public void setFrom(String from) throws UnsupportedEncodingException, MessagingException{
mimeMessage.setFrom(new InternetAddress(username,from));
}
/**
* 發送郵件<單人發送>
* return 是否發送成功
* @throws MessagingException
*/
public String sendMessage() throws MessagingException{
Transport.send(mimeMessage);
return "success";
}
/**
* 設定附件
* @param file 傳送檔案的路徑
*/
public void setMultipart(String file) throws MessagingException, IOException{
if(multipart==null){
multipart = new MimeMultipart();
}
multipart.addBodyPart(writeFiles(file));
mimeMessage.setContent(multipart);
}
/**
* 設定附件<添加多附件>
* @param fileList<接收List集合>
* @throws MessagingException
* @throws IOException
*/
public void setMultiparts(List<String> fileList) throws MessagingException, IOException{
if(multipart==null){
multipart = new MimeMultipart();
}
for(String s:fileList){
multipart.addBodyPart(writeFiles(s));
}
mimeMessage.setContent(multipart);
}
/**
* 發送常值內容,設定編碼方式
* <方法與發送附件配套使用>
* <發送普通的常值內容請使用setText()方法>
* @param s
* @param type
* @throws MessagingException
*/
public void setContent(String s,String type) throws MessagingException{
if(multipart==null){
multipart = new MimeMultipart();
}
bodypart = new MimeBodyPart();
bodypart.setContent(s, type);
multipart.addBodyPart(bodypart);
mimeMessage.setContent(multipart);
mimeMessage.saveChanges();
}
/**
* 讀取附件
* @param filePath
* @return
* @throws IOException
* @throws MessagingException
*/
public BodyPart writeFiles(String filePath)throws IOException, MessagingException{
File file = new File(filePath);
if(!file.exists()){
throw new IOException("檔案不存在!請確定檔案路徑是否正確");
}
bodypart = new MimeBodyPart();
DataSource dataSource = new FileDataSource(file);
bodypart.setDataHandler(new DataHandler(dataSource));
//檔案名稱要加入編碼,不然出現亂碼
bodypart.setFileName(MimeUtility.encodeText(file.getName()));
return bodypart;
}
}
package com.sun.mail;
import java.util.Date;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.mail.MessagingException;
public class testSend {
public static void main(String[] args) throws MessagingException, IOException
{
Map<String,String> map= new HashMap<String,String>();
SendMail mail = new SendMail("[email protected]","xxxxx");
map.put("mail.smtp.host", "smtp.163.com");
//暫時未成功,需要調試
/*SendMail mail = new SendMail("14789****@sina.cn","***miya");
map.put("mail.smtp.host", "smtp.sina.com");*/
map.put("mail.smtp.auth", "true");
mail.setPros(map);
mail.initMessage();
/*
* 添加收件者有三種方法:
* 1,單人添加(單人發送)調用setRecipient(str);發送String類型
* 2,多人添加(群發)調用setRecipients(list);發送list集合類型
* 3,多人添加(群發)調用setRecipients(sb);發送StringBuffer類型
*/
List<String> list = new ArrayList<String>();
list.add("[email protected]");
//list.add("***[email protected]");
//list.add("[email protected]");
mail.setRecipients(list);
/*String defaultStr = "[email protected],[email protected],[email protected],[email protected];
StringBuffer sb = new StringBuffer();
sb.append(defaultStr);
sb.append(",[email protected]");
mail.setRecipients(sb);*/
mail.setSubject("測試郵箱");
//mail.setText("謝謝合作");
mail.setDate(new Date());
mail.setFrom("MY");
mail.setMultipart("D:HELLO.txt");
mail.setContent("謝謝合作", "text/html; charset=UTF-8");
/*List<String> fileList = new ArrayList<String>();
fileList.add("D:1.jpg");
fileList.add("D:activation.zip");
fileList.add("D:dstz.sql");
fileList.add("D:軟體配置要求.doc");
mail.setMultiparts(fileList);*/
System.out.println(mail.sendMessage());
}
}
郵件發送 java