enlight
          java學習總結

          2006年7月7日

          以前整理的一段代碼,來源于網上,大家可以使用它連接到普通的郵件服務器或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孬巭 閱讀(260) | 評論 (0)編輯 收藏
           
          主站蜘蛛池模板: 宝山区| 灵山县| 喀喇沁旗| 城口县| 瑞丽市| 沛县| 博湖县| 榆树市| 松滋市| 凤凰县| 京山县| 嘉鱼县| 恩施市| 台湾省| 当雄县| 逊克县| 海林市| 奈曼旗| 卓尼县| 临澧县| 余姚市| 航空| 吴川市| 吉林市| 冷水江市| 乳山市| 丰镇市| 岳池县| 通道| 蓬莱市| 潼关县| 兴城市| 湖北省| 荥经县| 达孜县| 龙里县| 巫溪县| 忻城县| 乌拉特前旗| 顺义区| 大同市|