?? mailbox.java
字號:
package com.yuanchung.sales.util.emailutil;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.AuthenticationFailedException;
import javax.mail.Message;
import javax.mail.MessagingException;
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 org.apache.log4j.Logger;
/**
*
* @author 福建圓創軟件;
* @function 構造郵箱工具類;
*/
public class MailBox {
private static Logger logger = Logger.getLogger(MailBox.class);// 記載日志
private String mailTo = null;// 郵件接受者;
private String mailFrom = null;// 郵件發送者
private String smtpHost = null;// 郵件服務器地址;
private boolean debug = false;// 是否采用調試方式
private String messageBasePath = null;
private String subject = null;// 郵件主題;
/** 郵件內容* */
private String msgContent;// 郵件主題內容;
private String messageContentMimeType = "text/html; charset=utf-8";// 設置郵件類型和編碼;
private Vector attachedFileList;// 附件列表;
private String mailAccount = null;// 郵件數量;
private String mailPass = null;
private String mailbccTo = null;// 接受者b--發送副本給附加人b;
private String mailccTo = null;// 接受者c--發送副本給附加人c;s
// 構造函數;
public MailBox() {
super();
}
// 實例化方法;
public void init() {
}
/**
* @author 福建圓創軟件;
* @function 封裝郵件,把必要的信息裝載進郵件;
* @param session
* 在郵件發送中的會話
* @param msg
* 郵件對象;
* @throws IOException
* 輸入輸出流異常;
* @throws MessagingException
* 郵件異常;
*/
private void fillMail(Session session, MimeMessage msg) throws IOException,
MessagingException {
String fileName = null;
Multipart mPart = new MimeMultipart();
/**判斷發件人是否為空**/
if (mailFrom != null) {
msg.setFrom(new InternetAddress(mailFrom));
logger.debug("發送人Mail地址:" + mailFrom);
} else {
logger.debug("沒有指定發送人郵件地址!");
return;
}
/**判斷收件人是否為空**/
if (mailTo != null) {
InternetAddress[] address = InternetAddress.parse(mailTo);
msg.setRecipients(Message.RecipientType.TO, address);
logger.debug("收件人Mail地址:" + mailTo);
} else {
logger.debug("沒有指定收件人郵件地址!");
return;
}
/**郵件附加收件人c是否為空**/
if (mailccTo != null) {
InternetAddress[] ccaddress = InternetAddress.parse(mailccTo);
logger.debug("CCMail地址:" + mailccTo);
msg.setRecipients(Message.RecipientType.CC, ccaddress);
}
/**郵件附加收件人b是否為空**/
if (mailbccTo != null) {
InternetAddress[] bccaddress = InternetAddress.parse(mailbccTo);
logger.debug("BCCMail地址:" + mailbccTo);
msg.setRecipients(Message.RecipientType.BCC, bccaddress);
}
msg.setSubject(subject);//設置主題
InternetAddress[] replyAddress = { new InternetAddress(mailFrom) };//設置回信地址;
msg.setReplyTo(replyAddress);//傳入回信地址位置;
MimeBodyPart mBodyContent = new MimeBodyPart();//設置郵件主體部分;
/**判斷郵件主體是否為空**/
if (msgContent != null)//如果內容不為空;
mBodyContent.setContent(msgContent, messageContentMimeType);//設置主體的編碼;
else{
mBodyContent.setContent("", messageContentMimeType);
}
mPart.addBodyPart(mBodyContent);//總體部分加入主體部分;
/**附件位置s**/
if (attachedFileList != null) {
for (Enumeration fileList = attachedFileList.elements(); fileList
.hasMoreElements();) {
fileName = (String) fileList.nextElement();
MimeBodyPart mBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(messageBasePath
+ fileName);
logger.debug("Mail發送的附件為:" + messageBasePath + fileName);
mBodyPart.setDataHandler(new DataHandler(fds));
mBodyPart.setFileName(fileName);
mPart.addBodyPart(mBodyPart);
}
}
msg.setContent(mPart);//郵件加入總體部分;
msg.setSentDate(new Date());//發送時間
}
/**
* @author 福建圓創軟件;
* @function 發送郵件;
* @return 返回類型為int,1為成功,3為失敗;
* @throws IOException
* @throws MessagingException
*/
public int sendMail() throws IOException, MessagingException {
int loopCount;
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", "true");
MailAuthenticator auth = new MailAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
Transport trans = null;
try {
fillMail(session, msg);
trans = session.getTransport("smtp");
try {
trans.connect(smtpHost, MailAuthenticator.LU_MAIL_USER,
MailAuthenticator.LU_MAIL_PASSWORD);
} catch (AuthenticationFailedException e) {
e.printStackTrace();
logger.debug("連接郵件服務器錯誤:");
return 3;
} catch (MessagingException e) {
logger.debug("連接郵件服務器錯誤:");
return 3;
}
trans.send(msg);
trans.close();
} catch (MessagingException mex) {
logger.debug("發送郵件失敗:");
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
System.out.println(ex.toString());
ex.printStackTrace();
}
return 3;
} finally {
try {
if (trans != null && trans.isConnected())
trans.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
logger.debug("發送郵件成功!");
return 0;
}
public String getMailTo() {
return mailTo;
}
public void setMailTo(String mailTo) {
this.mailTo = mailTo;
}
public String getMailFrom() {
return mailFrom;
}
public void setMailFrom(String mailFrom) {
this.mailFrom = mailFrom;
}
public String getSmtpHost() {
return smtpHost;
}
public void setSmtpHost(String smtpHost) {
this.smtpHost = smtpHost;
}
public boolean isDebug() {
return debug;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
public String getMessageBasePath() {
return messageBasePath;
}
public void setMessageBasePath(String messageBasePath) {
this.messageBasePath = messageBasePath;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getMsgContent() {
return msgContent;
}
public void setMsgContent(String msgContent) {
this.msgContent = msgContent;
}
public String getMessageContentMimeType() {
return messageContentMimeType;
}
public void setMessageContentMimeType(String messageContentMimeType) {
this.messageContentMimeType = messageContentMimeType;
}
public Vector getAttachedFileList() {
return attachedFileList;
}
public void setAttachedFileList(Vector attachedFileList) {
this.attachedFileList = attachedFileList;
}
public String getMailAccount() {
return mailAccount;
}
public void setMailAccount(String mailAccount) {
this.mailAccount = mailAccount;
}
public String getMailPass() {
return mailPass;
}
public void setMailPass(String mailPass) {
this.mailPass = mailPass;
}
public String getMailbccTo() {
return mailbccTo;
}
public void setMailbccTo(String mailbccTo) {
this.mailbccTo = mailbccTo;
}
public String getMailccTo() {
return mailccTo;
}
public void setMailccTo(String mailccTo) {
this.mailccTo = mailccTo;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -