JavaMail通过Exchange发送邮件

转自:http://jarvi.iteye.com/blog/2209212


使用Exchange Server邮件服务器发送邮件的特殊之处在于用户名username必须添加所在域的前缀,比如要使用域domain中的用户demo@sample.com来发送邮件,那么登录名要改成domain\demo,而不是普通stmp服务器中的demo@sample.com。 

下面贴出演示代码: 

Java代码   收藏代码
  1. import java.util.Date;  
  2. import java.util.Properties;  
  3.   
  4. import javax.mail.BodyPart;  
  5. import javax.mail.Message;  
  6. import javax.mail.MessagingException;  
  7. import javax.mail.Multipart;  
  8. import javax.mail.Session;  
  9. import javax.mail.Transport;  
  10. import javax.mail.internet.InternetAddress;  
  11. import javax.mail.internet.MimeBodyPart;  
  12. import javax.mail.internet.MimeMessage;  
  13. import javax.mail.internet.MimeMultipart;  
  14.   
  15. public class Mailer {  
  16.     private String host;  
  17.     private String auth;  
  18.     private String username;  
  19.     private String domainUser;  
  20.     private String password;  
  21.   
  22.     public boolean send(String[] to, String[] cc, String[] bcc, String subject, String content) throws MessagingException {  
  23.         Properties props = new Properties();  
  24.         props.put("mail.smtp.host", host);  
  25.         props.put("mail.smtp.auth", auth);  
  26.         Session s = Session.getInstance(props);  
  27.         //      s.setDebug(true);  
  28.   
  29.         MimeMessage message = new MimeMessage(s);  
  30.   
  31.         InternetAddress from = new InternetAddress(username);  
  32.         message.setFrom(from);  
  33.         InternetAddress[] Toaddress = new InternetAddress[to.length];  
  34.         for (int i = 0; i < to.length; i++)  
  35.             Toaddress[i] = new InternetAddress(to[i]);  
  36.         message.setRecipients(Message.RecipientType.TO, Toaddress);  
  37.   
  38.         if (cc != null) {  
  39.             InternetAddress[] Ccaddress = new InternetAddress[cc.length];  
  40.             for (int i = 0; i < cc.length; i++)  
  41.                 Ccaddress[i] = new InternetAddress(cc[i]);  
  42.             message.setRecipients(Message.RecipientType.CC, Ccaddress);  
  43.         }  
  44.   
  45.         if (bcc != null) {  
  46.             InternetAddress[] Bccaddress = new InternetAddress[bcc.length];  
  47.             for (int i = 0; i < bcc.length; i++)  
  48.                 Bccaddress[i] = new InternetAddress(bcc[i]);  
  49.             message.setRecipients(Message.RecipientType.BCC, Bccaddress);  
  50.         }  
  51.         message.setSubject(subject);  
  52.         message.setSentDate(new Date());  
  53.   
  54.         BodyPart mdp = new MimeBodyPart();  
  55.         mdp.setContent(content, "text/html;charset=utf-8");  
  56.         Multipart mm = new MimeMultipart();  
  57.         mm.addBodyPart(mdp);  
  58.         message.setContent(mm);  
  59.   
  60.         message.saveChanges();  
  61.         Transport transport = s.getTransport("smtp");  
  62.         transport.connect(host, (null == domainUser) ? username : domainUser, password);  
  63.         transport.sendMessage(message, message.getAllRecipients());  
  64.         transport.close();  
  65.         return true;  
  66.     }  
  67.   
  68.     public Mailer(String host, String auth, String domainUser, String username, String password) {  
  69.         super();  
  70.         this.host = host;  
  71.         this.auth = auth;  
  72.         this.domainUser = domainUser;  
  73.         this.username = username;  
  74.         this.password = password;  
  75.     }  
  76. }  

 调用: 

Java代码   收藏代码
  1. new Mailer("smtp.sample.com""true""domain\\demo""demo@sample.com""password").send(new String[] { "to@mail.com" }, nullnull"demo_title""<h3>test</h3>");  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值