weblogic javamail jar weblogic jar , Mail import 。 jar jar ,
spring jar mail.jar , http://zht1933.iteye.com/blog/807856,
package com.ats.task.mail;
import java.util.Date;
import java.util.Properties;
/**
*
* @author jeff
*
*/
public class Mail{
private String host;
private String auth;
private String username;
private String domainUser;
private String password;
private Constant c;
public boolean send(String[] to, String[] cc, String[] bcc, String subject, String content) throws javax.mail.MessagingException {
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", auth);
javax.mail.Session s = javax.mail.Session.getInstance(props);
// s.setDebug(true);
javax.mail.internet.MimeMessage message = new javax.mail.internet.MimeMessage(s);
javax.mail.internet.InternetAddress from = new javax.mail.internet.InternetAddress(username);
message.setFrom(from);
javax.mail.internet.InternetAddress[] Toaddress = new javax.mail.internet.InternetAddress[to.length];
for (int i = 0; i < to.length; i++)
Toaddress[i] = new javax.mail.internet.InternetAddress(to[i]);
message.setRecipients(javax.mail.Message.RecipientType.TO, Toaddress);
if (cc != null) {
javax.mail.internet.InternetAddress[] Ccaddress = new javax.mail.internet.InternetAddress[cc.length];
for (int i = 0; i < cc.length; i++)
Ccaddress[i] = new javax.mail.internet.InternetAddress(cc[i]);
message.setRecipients(javax.mail.Message.RecipientType.CC, Ccaddress);
}
if (bcc != null) {
javax.mail.internet.InternetAddress[] Bccaddress = new javax.mail.internet.InternetAddress[bcc.length];
for (int i = 0; i < bcc.length; i++)
Bccaddress[i] = new javax.mail.internet.InternetAddress(bcc[i]);
message.setRecipients(javax.mail.Message.RecipientType.BCC, Bccaddress);
}
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
message.setSentDate(new Date());
javax.mail.BodyPart mdp = new javax.mail.internet.MimeBodyPart();
mdp.setHeader("Content-Transfer-Encoding","base64");
mdp.setContent(content, "text/plain;charset=GBK");
javax.mail.Multipart mm = new javax.mail.internet.MimeMultipart();
mm.addBodyPart(mdp);
message.setContent(mm);
message.setSubject(subject);
message.saveChanges();
javax.mail.Transport transport = s.getTransport("smtp");
transport.connect(host, (null == domainUser) ? username : domainUser, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
return true;
}
/* */
public Mail() {
super();
c = c.getInstance();
this.host = c.getMailServer();
this.auth = "true";
this.domainUser =c.getMailCount();
this.username = c.getMailAddress();
this.password = c.getMailPassword();
}
}
メール関連の設定クラス:
package com.ats.task.mail;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
/**
*
* @author jeff
*
*/
public class Constant {
private static Constant instance = null;
public static synchronized Constant getInstance() {
if(instance==null){
instance = new Constant();
}
return instance;
}
private String mailAddress;//
private String mailCount; //
private String mailPassword;//
private String mailServer; // smtp
/**
* ,
*/
private Constant(){
init();
}
private void init(){
Properties p = new Properties();
try {
p.load(Constant.class.getResourceAsStream("/email.properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mailAddress = p.getProperty("mailAddress");
mailCount = p.getProperty("mailCount");
mailPassword = p.getProperty("mailPassword");
mailServer = p.getProperty("mailServer");
}
public String getMailAddress() {
return mailAddress;
}
public void setMailAddress(String mailAddress) {
this.mailAddress = mailAddress;
}
public String getMailCount() {
return mailCount;
}
public void setMailCount(String mailCount) {
this.mailCount = mailCount;
}
public String getMailPassword() {
return mailPassword;
}
public void setMailPassword(String mailPassword) {
this.mailPassword = mailPassword;
}
public String getMailServer() {
return mailServer;
}
public void setMailServer(String mailServer) {
this.mailServer = mailServer;
}
/*
*
*/
public static void main(String[] args) {
Constant c = Constant.getInstance();
System.out.println(c.getMailAddress());
}
}
設定ファイルはemail.propertiesです.具体的な構成は以下の通りです.
メールアドレス
mail Count=sino-life\ATS(ユーザー 、ここで してください。Exchangeメールアドレスはドメイン/ユーザー が です。)
mail Password=パスワード
メールセーバー=SMTP , smtp IP , , linx smtp