注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

流星永恒的博客

JSF,Facelets,Rich(Prime)Faces,和java的笔记

 
 
 

日志

 
 

JavaMail for Gmail,163 and GlassFish mail resource  

2010-08-22 18:30:39|  分类: java |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

java code:

1. For gmail

public class Test {
       public static void main(String[] args) {
        try {
            sendMail("test", "test", "yyq2008@gmail.com", "yyq2007aa@163.com");
        } catch (Exception ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

 

 public static synchronized void sendMail(String subject, String body, String sender, String recipients)
                       throws Exception
 {
  Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  Properties props = new Properties();
  props.setProperty("mail.transport.protocol", "smtp");
  props.setProperty("mail.host", "smtp.gmail.com");
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.port", "465");
  props.put("mail.smtp.socketFactory.port", "465");
  props.put("mail.smtp.socketFactory.class",
  "javax.net.ssl.SSLSocketFactory");
  props.put("mail.smtp.socketFactory.fallback", "false");
  props.setProperty("mail.smtp.quitwait", "false");

  Session session = Session.getDefaultInstance(props,
    new javax.mail.Authenticator()
  {
   protected PasswordAuthentication getPasswordAuthentication()
   { return new PasswordAuthentication("yyq2008","password"); }
  });

  MimeMessage message = new MimeMessage(session);
  message.setSender(new InternetAddress(sender));
  message.setSubject(subject);
  message.setContent(body, "text/plain");
  if (recipients.indexOf(',') > 0)
     message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
  else
     message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));


  Transport.send(message);

 }

v2. For 163

public class Test {
       public static void main(String[] args) {
        try {
            sendMail("test", "test", "yyq2007aa@163.com", "yyq2007aa@163.com");
        } catch (Exception ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

 

 public static synchronized void sendMail(String subject, String body, String sender, String recipients)
                       throws Exception
 {
  Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  Properties props = new Properties();
  props.setProperty("mail.transport.protocol", "smtp");
  props.setProperty("mail.host", smtp.163.com);
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.port", "25");
  props.put("mail.smtp.socketFactory.port", "25");
  props.put("mail.smtp.socketFactory.fallback", "false");
  props.setProperty("mail.smtp.quitwait", "false");

  Session session = Session.getDefaultInstance(props,
    new javax.mail.Authenticator()
  {
   protected PasswordAuthentication getPasswordAuthentication()
   { return new PasswordAuthentication("yyq2007aa","passoword"); }
  });

  MimeMessage message = new MimeMessage(session);
  message.setSender(new InternetAddress(sender));
  message.setSubject(subject);
  message.setContent(body, "text/plain");
  if (recipients.indexOf(',') > 0)
     message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
  else
     message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
  Transport.send(message);

 }
}

 

GlassFish config:

start your galssfish server, enter url http://localhost:4848  to config your mail resource, default user "admin" and password "adminadmin" or config in domain.xml file directlty.

config in domain.xml, path: glassfish\domains\domain1\config\domain.xml

add following fragment:

for gmail

<mail-resource host="smtp.gmail.com" store-protocol-class="com.sun.mail.pop3.POP3Store" store-protocol="POP3" jndi-name="mail/Session" from="yyq2008@gmail.com" user="yyq2008">
      <property name="mail.smtp.auth" value="true" />
      <property name="mail.smtp.port" value="465" />
      <property name="mail.smtp.socketFactory.port" value="465" />
      <property name="mail.smtp.socketFactory.class" value="javax.net.ssl.SSLSocketFactory" />
      <property name="mail.smtp.socketFactory.fallback" value="false" />
      <property name="mail.smtp.password" value="password" />
      <property name="mail.smtp.quitwait" value="false" />
      <property name="mail.smtp.user" value="yyq2008" />
    </mail-resource>

 

for 163

<mail-resource host="smtp.163.com" store-protocol-class="com.sun.mail.pop3.POP3Store" store-protocol="POP3" jndi-name="mail/Session" from="yyq2007aa@163.com" user="yyq2008">
      <property name="mail.smtp.auth" value="true" />
      <property name="mail.smtp.port" value="25" />
      <property name="mail.smtp.socketFactory.port" value="25" />
      <property name="mail.smtp.socketFactory.fallback" value="false" />
      <property name="mail.smtp.password" value="password" />
      <property name="mail.smtp.quitwait" value="false" />
      <property name="mail.smtp.user" value="yyq2007aa" />
    </mail-resource>

 example configuration:

 

JavaMail for Gmail,163 and GlassFish mail resource - yyq2007aa - 流星永恒的博客
JavaMail for Gmail,163 and GlassFish mail resource - yyq2007aa - 流星永恒的博客
 
Configed the properties  in  java code above, that is the point.

 

Use mail resource in servlet:

...

    @Resource(name = "mail/Session")
    private Session mail;

   ...

    private void sendMail(String email, String subject, String body) throws NamingException, MessagingException {
        MimeMessage message = new MimeMessage(mail);
        message.setSubject(subject);
        message.setRecipients(RecipientType.TO, InternetAddress.parse(email, false));
        message.setText(body);
        Transport.send(message);
    }

...

That's all.

  评论这张
 
阅读(1997)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018