?? sendemail.java
字號:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.PropertyResourceBundle;
import java.util.Date;
import javax.activation.DataHandler;
public class SendEmail {
public static String getPropertiesDescByKey(String code,String propertiesFile){
try{
PropertyResourceBundle configBundle = (PropertyResourceBundle)PropertyResourceBundle.getBundle(propertiesFile);
String desc = configBundle.getString(code);
return desc;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
try {
String title="對帳結果";
String content="我要成功";
java.util.Properties props = System.getProperties();
props.put("mail.smtp.host","mail.hmit.com.cn");
props.put("mail.smtp.auth","true");
Session s=Session.getInstance(props);
s.setDebug(true);
MimeMessage message=new MimeMessage(s);
//從屬性文件中讀取發送方的郵件箱和密碼------------------------------------------------------
String fromEmail=SendEmail.getPropertiesDescByKey("email","Email");
String fromEailPWD=SendEmail.getPropertiesDescByKey("pwd","Email");
System.out.println (fromEmail);
//登錄到要發送的郵件服務器-----------------------------------------------------------------------
Transport transport=s.getTransport("smtp");
transport.connect("mail.hmit.com.cn",fromEmail,fromEailPWD);//以smtp方式登錄郵箱
//設置發件人地址---------------------------------------------------------------------------------------
InternetAddress from=new InternetAddress(fromEmail);
message.setFrom(from);
//收件人地址,并解析油箱地址----------------------------------------------------------------------
String toURL = "mingyang.sun@hmit.com.cn;lixiong.chen@hmit.com.cn";
while(toURL.indexOf(";") != -1){
toURL=toURL.replace(';',',');
}
String temptoURL=toURL;
int len=0;
while(temptoURL.indexOf(",")!=-1){
len++;
temptoURL=temptoURL.substring(temptoURL.indexOf(",")+1,temptoURL.length());
}
InternetAddress[] tolist= new InternetAddress[len+1];
int j=0;
while(toURL.indexOf(",")!=-1){
String toReceiver=toURL.substring(0,toURL.indexOf(","));
toURL=toURL.substring(toURL.indexOf(",")+1,toURL.length());
tolist[j]=new InternetAddress(toReceiver);
j++;
//message.setRecipient(Message.RecipientType.TO,to);
}
if(toURL!=""){
tolist[j]=new InternetAddress(toURL);
}
//InternetAddress to=new InternetAddress(toURL);
//message.setRecipient(Message.RecipientType.TO,to);
message.setRecipients(Message.RecipientType.TO,tolist);
//設置主題------------------------------------------------------------------------------
message.setSubject(title);
//設置發送日期-----------------------------------------------------------------------
message.setSentDate(new Date());
//設置正文內容------------------------------------------------------------------------
//message.setText(content);//設置信件內容
// 如果文件上傳成功,則發送附件; 如果文件上傳不成功,則發送錯誤信息
Multipart mm=new MimeMultipart();//新建一個MimeMultipart對象用來存放多個BodyPart對象
//第一部分
BodyPart mdp=new MimeBodyPart();//新建一個存放信件內容的BodyPart對象
mdp.setContent(content,"text/html;charset=gb2312");//給BodyPart對象設置內容和格式/編碼方式
mm.addBodyPart(mdp);//將含有信件內容的BodyPart加入到MimeMultipart對象中
//第二部分(附件)
mdp=new MimeBodyPart();
//String targetFile = this.getServletContext().getRealPath("") + "tmsTemp\\target\\uploadaccoutcheck.xls";
String targetFile = "D:/a.xls";
javax.activation.FileDataSource fds=new javax.activation.FileDataSource(targetFile);
DataHandler dh=new DataHandler(fds);
mdp.setFileName("checkAccountResult.xls");//可以和原文件名不一致
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
//把mm作為消息對象的內容------------------------------------------------------------
message.setContent(mm);
//存儲郵件信息------------------------------------------------------------------------------
message.saveChanges();
//開始發送
transport.sendMessage(message,message.getAllRecipients());//發送郵件,其中第二個參數是所有已設好的收件人地址
transport.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -