javamail 郵件群發

          package gmailsender;

          import java.security.Security;
          import java.util.Date;
          import java.util.Properties;

          import javax.mail.Authenticator;
          import javax.mail.Message;
          import javax.mail.MessagingException;
          import javax.mail.PasswordAuthentication;
          import javax.mail.Session;
          import javax.mail.Transport;
          import javax.mail.internet.AddressException;
          import javax.mail.internet.InternetAddress;
          import javax.mail.internet.MimeMessage;

          /**
           * 使用Gmail發送郵件
           * @author Winter Lau
           */
          public class Main {

           public static void main(String[] args) throws AddressException, MessagingException {
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
            // Get a Properties object
            Properties props = System.getProperties();
            props.setProperty("mail.smtp.host", "smtp.gmail.com");
            props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.setProperty("mail.smtp.socketFactory.fallback", "false");
            props.setProperty("mail.smtp.port", "465");
            props.put("mail.smtp.auth", "true");
            final String username = "username";
            final String password = "password";
            Session session = Session.getDefaultInstance(props, new Authenticator(){
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }});

          // -- Create a new message --
            Message msg = new MimeMessage(session);
           // -- Set the FROM and TO fields --
            String[] gods={"dfgd@yahoo.com.cn","dfgdf@qq.com"};
            int len=gods.length;
            InternetAddress[] address = new InternetAddress[len];
               for (int i = 0; i < gods.length; i++) {
                address[i] = new InternetAddress(gods[i]);
               }
            msg.setFrom(new InternetAddress("fgdfgf@gmail.com"));
            //msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse("dgddfg@qq.com",false));
            msg.setRecipients(Message.RecipientType.TO,address);
            msg.setSubject("woaizhongguo");
            msg.setText("woaizhongguo");
            msg.setSentDate(new Date());
            Transport.send(msg);
            System.out.println("郵件已發送!");
           

          }
          }


           

          posted on 2007-06-22 19:09 付軒 閱讀(682) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2007年6月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 阿拉尔市| 孝昌县| 弥勒县| 阿坝县| 东乡| 准格尔旗| 辽宁省| 且末县| 平凉市| 东乌| 屯昌县| 明溪县| 满城县| 施甸县| 固镇县| 和田市| 修武县| 通道| 乾安县| 青川县| 同德县| 辉县市| 色达县| 新绛县| 勃利县| 佛冈县| 江川县| 栾城县| 汪清县| 新巴尔虎左旗| 朝阳区| 财经| 景宁| 弥渡县| 台北县| 中江县| 乐陵市| 福建省| 宁河县| 株洲市| 潍坊市|