import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
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;
import javax.mail.internet.MimeUtility;
public class SendMail {
public boolean Send(MailInfo mailInfo)throws Exception{
Properties props=new Properties();
//設置發送郵件的郵件服務器的屬性(這里使用網易的smtp服務器)
props.put("mail.smtp.host", mailInfo.getHost());
//需要經過授權,也就是有戶名和密碼的校驗,這樣才能通過驗證(一定要有這一條)
props.put("mail.smtp.auth", mailInfo.getValidate());
Session session=Session.getDefaultInstance(props);// 用剛剛設置好的props對象構建一個session
// session.setDebug(true);
MimeMessage message=new MimeMessage(session);
try{
message.setFrom(new InternetAddress(mailInfo.getFromAddress()));
InternetAddress[] sendTo = new InternetAddress[mailInfo.getSendAddress().length];
for(int i=0;i<mailInfo.getSendAddress().length;i++){
sendTo[i]=new InternetAddress(mailInfo.getSendAddress()[i]);
}
// message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailInfo.getToAddress()));
message.setRecipients(Message.RecipientType.TO, sendTo);
message.setSubject(mailInfo.getSubject());
message.setSentDate(mailInfo.getSentdate());
//向multipart對象中添加郵件的各個部分內容,包括文本內容和附件
Multipart multipart=new MimeMultipart();
BodyPart contentPart=new MimeBodyPart();
contentPart.setText(mailInfo.getContent());
multipart.addBodyPart(contentPart);
//添加附件
for(int i=0;i<mailInfo.getFilepath().length;i++){
BodyPart messageBodyPart=new MimeBodyPart();
String fpath=mailInfo.getFilepath()[i].split(",")[0];
DataSource source=new FileDataSource(fpath);
messageBodyPart.setDataHandler(new DataHandler(source));
sun.misc.BASE64Encoder enc=new sun.misc.BASE64Encoder();
String lname=fpath.substring(fpath.lastIndexOf("."), fpath.length());
String fname=String.valueOf(System.currentTimeMillis())+lname;
messageBodyPart.setFileName("=?GBK?B?"+enc.encode(fname.getBytes())+"?=");
multipart.addBodyPart(messageBodyPart);
}
message.setContent(multipart);//text/plain表示純文本內容
message.saveChanges();
Transport transport=session.getTransport("smtp");
transport.connect(mailInfo.getHost(),mailInfo.getUsername(),mailInfo.getPassword());
transport.sendMessage(message,message.getAllRecipients());
transport.close();
return true;
}catch(Exception e){
e.printStackTrace();
}
return false;
}
}
//測試代碼:
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class SendTest {
@Test
public void mailTest(){
SendMail mail=new SendMail();
MailInfo mailInfo=new MailInfo();
mailInfo.setContent("郵件內容44444444444444444444");
String[] filepath=new String[]{"D:\\1317742305879.csv","D:\\zpssoa.log","E:\\Emotion\\Photo\\好寂寞好空虛.gif"};
mailInfo.setFilepath(filepath);
// mailInfo.setFilename("希望"+fname);
mailInfo.setFromAddress("leeposter@126.com");
mailInfo.setHost("smtp.126.com");
mailInfo.setSubject("郵件主題444444444444444444444");
// mailInfo.setToAddress("liposter@163.com");
String[] destination=new String[]{"liposter@163.com","lposter@163.com","liposter@126.com"};
mailInfo.setSendAddress(destination);
mailInfo.setUsername("leeposter");
mailInfo.setValidate("true");
mailInfo.setPassword("****");
try{
boolean flag=mail.Send(mailInfo);
if(flag==true){
System.out.println("發送成功");
}else{
System.out.println("發送失敗");
}
}catch(Exception e){
e.printStackTrace();
}
}
}