论坛首页 Java企业应用论坛

Exchange服务器邮件发送(javamail,无乱码,支持内网)

浏览 3322 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-03-15   最后修改:2011-03-15
因为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,具体配置如下:

mailAddress=发送邮箱名
mailCount=sino-life\\ATS (用户名,这里要注意,Exchage邮箱用户名需要 域//用户名)
mailPassword=密码
mailServer=SMTP地址,建议直接使用smtp IP地址,不要用域名,否则linx下会找不到smtp地址

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics