enlight
          java學習總結

          2006年6月23日

          以前整理的一段代碼,來源于網上,大家可以使用它連接到普通的郵件服務器或gmail郵件服務器發送郵件通知等.
          現在還只是發送文本郵件,有興趣的可以再看一下如何發送超文本郵件,帶附件等.
          //Author:Adrian 20060707

          package asdf.asdf.mail;
          import java.util.Properties;
          import javax.mail.Message;
          import javax.mail.PasswordAuthentication;
          import javax.mail.Session;
          import javax.mail.Transport;
          import javax.mail.internet.InternetAddress;
          //使用gmail的郵件服務器
          public ?class EmailNoticeEx{
          ? ?public static String sendSSLGmail(String fromName,String fromEmail,String smtpServer,String smtpUser,String smtpPassword,String recipient_name,String recipients[],String subject,String message,String encoding){
          ? try{
          ? ?boolean debug = false;
          ? ?java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
          ? ?// Set the host smtp address
          ? ?// ?設置系統屬性
          ? ?Properties props = new Properties();//獲得系統屬性對象
          ? ?props.put("mail.transport.protocol", "smtp"); ? ?
          ? ?props.put("mail.host", smtpServer);//設置SMTP主機

          ? ?MyAuthenticator auth = new MyAuthenticator(smtpUser, smtpPassword);
          ? ?//獲得郵件會話對象
          ? ?Session mailSession = Session.getDefaultInstance(props, auth);
          ? ?mailSession.setDebug(debug);
          ? ?javax.mail.internet.MimeMessage mimeMsg; //MIME郵件對象
          ? ?
          ? ?//創建MIME郵件對象
          ? ?mimeMsg = new javax.mail.internet.MimeMessage(mailSession);
          ? ?// create a message
          ? ?//Message msg = new MimeMessage(session);

          ? ?// set the from and to address
          ? ?InternetAddress addressFrom = new InternetAddress(fromEmail,fromName);
          ? ?mimeMsg.setFrom(addressFrom);

          ? ?InternetAddress[] addressTo = new InternetAddress[recipients.length];
          ? ?for (int i = 0; i < recipients.length; i++) {
          ? ? ? ?addressTo[i] = new InternetAddress(recipients[i]);
          ? ?}
          ? ?mimeMsg.setRecipients(Message.RecipientType.TO, addressTo);
          ? ?// Setting the Subject and Content Type
          ? ?mimeMsg.setSubject(subject,encoding);
          ? ?mimeMsg.setContent(message, "text/plain");
          ? ?mimeMsg.setText(message,encoding);
          ? ?Transport.send(mimeMsg);
          ? }catch(Exception e){
          ? ? ? e.printStackTrace();
          ? ? ? return e.getMessage();
          ? }
          ? return null;
          ?}
          //使用普通的郵件服務器 ?
          ? ?public static String sendMail(String fromName,String fromEmail,String smtpServer,String smtpUser,String smtpPassword,String recipient_name,String recipients[],String subject,String message,String encoding){
          ? ? ? ? ? try{
          ? ? ? ? ? ?boolean debug = false;
          ? ? ? ? ? ?// Set the host smtp address
          ? ? ? ? ? ?// ?設置系統屬性
          ? ? ? ? ? ?Properties props = new Properties();//獲得系統屬性對象
          ? ? ? ? ? ?props.put("mail.transport.protocol", "smtp");
          ? ? ? ? ? ?props.put("mail.smtp.starttls.enable","true");
          ? ? ? ? ? ?props.put("mail.smtp.host", smtpServer);//設置SMTP主機
          ? ? ? ? ? ?props.put("mail.smtp.auth", "true");
          ? ? ? ? ? ?
          ? ? ? ? ? ?MyAuthenticator auth = new MyAuthenticator(smtpUser, smtpPassword);
          ? ? ? ? ? ?//獲得郵件會話對象
          ? ? ? ? ? ?Session mailSession = Session.getDefaultInstance(props, auth);
          ? ? ? ? ? ?mailSession.setDebug(debug);
          ? ? ? ? ? ?javax.mail.internet.MimeMessage mimeMsg; //MIME郵件對象
          ? ? ? ? ? ?
          ? ? ? ? ? ?//創建MIME郵件對象
          ? ? ? ? ? ?mimeMsg = new javax.mail.internet.MimeMessage(mailSession);
          ? ? ? ? ? ?// create a message
          ? ? ? ? ? ?//Message msg = new MimeMessage(session);

          ? ? ? ? ? ?// set the from and to address
          ? ? ? ? ? ?InternetAddress addressFrom = new InternetAddress(fromEmail,fromName);
          ? ? ? ? ? ?mimeMsg.setFrom(addressFrom);

          ? ? ? ? ? ?InternetAddress[] addressTo = new InternetAddress[recipients.length];
          ? ? ? ? ? ?for (int i = 0; i < recipients.length; i++) {
          ? ? ? ? ? ? ? ?addressTo[i] = new InternetAddress(recipients[i]);
          ? ? ? ? ? ?}
          ? ? ? ? ? ?mimeMsg.setRecipients(Message.RecipientType.TO, addressTo);
          ? ? ? ? ? ?// Setting the Subject and Content Type
          ? ? ? ? ? ?mimeMsg.setSubject(subject,encoding);
          ? ? ? ? ? ?mimeMsg.setContent(message, "text/plain");
          ? ? ? ? ? ?mimeMsg.setText(message,encoding);
          ? ? ? ? ? ?Transport.send(mimeMsg);
          ? ? ? ? ? }catch(Exception e){
          ? ? ? ? ? ? ? e.printStackTrace();
          ? ? ? ? ? ? ? return e.getMessage();
          ? ? ? ? ? }
          ? ? ? ? ? return null;
          ? ? ? ? ?}
          ? ? ? ? ? ?

          ?
          }// end class

          // smtp需要驗證時候的驗證類
          class MyAuthenticator
          ? ?extends javax.mail.Authenticator {
          ?private String strUser;
          ?private String strPwd;
          ?public MyAuthenticator(String user, String password) {
          ? ?this.strUser = user;
          ? ?this.strPwd = password;
          ?}

          ?protected PasswordAuthentication getPasswordAuthentication() {
          ? ?return new PasswordAuthentication(strUser, strPwd);
          ?}
          }
           樓主:navIme2
          posted @ 2006-07-07 17:41 java孬巭 閱讀(261) | 評論 (0)編輯 收藏
           
          plpl!gjgj!
          posted @ 2006-06-23 14:51 java孬巭 閱讀(130) | 評論 (0)編輯 收藏
           
          主站蜘蛛池模板: 安乡县| 区。| 景洪市| 雷山县| 米脂县| 宝清县| 汉源县| 遵化市| 湘潭县| 广州市| 大姚县| 毕节市| 万山特区| 澄江县| 田阳县| 禄丰县| 冕宁县| 榆树市| 开鲁县| 天镇县| 达日县| 翁源县| 营口市| 卓尼县| 寿光市| 六枝特区| 海安县| 萨嘎县| 阿巴嘎旗| 米脂县| 行唐县| 江口县| 罗城| 仪征市| 合阳县| 四子王旗| 伊春市| 南和县| 胶州市| 孟州市| 韶山市|