/**
* SendMailService 構造子注解
*/
public SendMail() {
super();
}
private void fillMail(Session session
String fileName = null;
Multipart mPart = new MimeMultipart();
if (mailFrom != null) {
msg
System
} else {
System
return;
}
if (mailTo != null) {
InternetAddress[] address = InternetAddress
msg
System
} else {
System
return;
}
if (mailccTo != null) {
InternetAddress[] ccaddress = InternetAddress
System
msg
}
if (mailbccTo != null) {
InternetAddress[] bccaddress = InternetAddress
System
msg
}
msg
InternetAddress[] replyAddress = { new InternetAddress(mailFrom)};
msg
// create and fill the first message part
MimeBodyPart mBodyContent = new MimeBodyPart();
if (msgContent != null)
mBodyContent
else
mBodyContent
mPart
// attach the file to the message
if (attachedFileList != null) {
for (Enumeration fileList = attachedFileList
fileName = (String) fileList
MimeBodyPart mBodyPart = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource(messageBasePath + fileName);
System
mBodyPart
mBodyPart
mPart
}
}
msg
msg
}
/**
* 此處插入方法說明
*/
public void init()
{
}
/**
* 發送e_mail
* 當返回值為
* 當返回值為
*/
public int sendMail() throws IOException
int loopCount;
Properties props = System
props
props
MailAuthenticator auth = new MailAuthenticator();
Session session = Session
session
MimeMessage msg = new MimeMessage(session);
Transport trans = null;
try {
fillMail(session
// send the message
trans = session
try {
nnect(smtpHost
} catch (AuthenticationFailedException e) {
e
System
return
} catch (MessagingException e) {
System
return
}
trans
trans
} catch (MessagingException mex) {
System
mex
Exception ex = null;
if ((ex = mex
System
ex
}
return
} finally {
try {
if (trans != null && trans
trans
} catch (Exception e) {
System
}
}
System
return
}
public void setAttachedFileList(java
{
attachedFileList = filelist;
}
public void setDebug(boolean debugFlag)
{
debug=debugFlag;
}
public void setMailAccount(String strAccount) {
mailAccount = strAccount;
}
public void setMailbccTo(String bccto) {
mailbccTo = bccto;
}
public void setMailccTo(String ccto) {
mailccTo = ccto;
}
public void setMailFrom(String from)
{
mailFrom=from;
}
public void setMailPass(String strMailPass) {
mailPass = strMailPass;
}
public void setMailTo(String to)
{
mailTo=to;
}
public void setMessageBasePath(String basePath)
{
messageBasePath=basePath;
}
public void setMessageContentMimeType(String mimeType)
{
messageContentMimeType = mimeType;
}
public void setMsgContent(String content)
{
msgContent=content;
}
public void setSMTPHost(String host)
{
smtpHost=host;
}
public void setSubject(String sub)
{
subject=sub;
}
public static void main(String[] argv) throws Exception
{
for(int i =
SendMail sm = new SendMail();
sm
sm
sm
sm
sm
sm
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26252.html