注册 | 登录 | 设为首页 | 加入收藏
您当前的位置:飞翔学院-IT中国 → 编程开发JSP → 文章内容

一段带smtp认证的JavaMail代码

作者:佚名 来源:不详 发布时间:2007-11-30 0:00:47
       Properties props = System.getProperties();
        props.put("mail.smtp.host", host);

        String mailNeedAuth = CatseyeConfig.getConfig("MailNeedAuth");
        String mailUser = CatseyeConfig.getConfig("MailUser");
        String mailPassword = CatseyeConfig.getConfig("MailPassword");

        if (mailNeedAuth.equals("1")) {
            props.put("mail.smtp.auth", "true");
        } else {
            props.put("mail.smtp.auth", "false");
        }

        Session session = Session.getDefaultInstance(props, null);

        javax.mail.Message msg = new MimeMessage(session);
        InternetAddress[] toAddrs = null;

        try {
            if (recipients != null) {
                toAddrs = InternetAddress.parse(recipients, false);
                msg.setRecipients(javax.mail.Message.RecipientType.TO, toAddrs);
            } else {
                throw new Exception("No recipient address specified");
            }

            if (sender != null) {
                msg.setFrom(new InternetAddress(sender));
            } else {
                throw new Exception("No sender address specified");
            }

            if (subject != null) {
                msg.setSubject(subject);
            }

            /*
            BodyPart bp=new MimeBodyPart();
            bp.setContent(body, "text/html");
            Multipart mp=new MimeMultipart();
            mp.addBodyPart(bp);
            */

            //msg.setContent(mp);
            msg.setContent(body, "text/html");
            msg.setHeader("X-Mailer", "Catseye SMTP Robot");
            msg.setHeader("Content-Type", "text/html");

            /*
            msg.setDataHandler(new DataHandler(
            new ByteArrayDataSource(body, "text/html")));
            //msg.setText(body);
            */
            Transport trans = session.getTransport("smtp");
            trans.connect(host, mailUser, mailPassword);
            trans.sendMessage(msg, 
                              msg.getRecipients(
                                      javax.mail.Message.RecipientType.TO));
更多内容请看PCdog.com--SMTP协议专题

  • 打印文档
  • 推荐好友
  • 返回顶部
  • 增大字体
  • 减少字体
关于本站 | 工作机会 | 合作网站 | 广告服务 | 市场合作| 联系我们 | 抽奖活动
版权所有: 武汉威俊科技有限公司 Copyright 2005-2007 www.ITCNW.COM All rights reserved