天空是藍色的

          做好軟件為中國 #gcc -c helloworld.c -o helloworld.o //編譯目標文件 #gcc helloworld.o -o helloworld //編譯成可執行exe #helloworld //運行exe
          數據加載中……
          [轉載]javamail發送郵件
          http://spaces.msn.com/members/tcjava/
          /*
          在java版經常看到有人問如何用javamail發送郵件?如何接收郵件?如何訪問多個文件夾等。問題零散,而歷史的回復早已經淹沒在問題的海洋之中。

          本人之前所做過一個java項目,其中包含有WebMail功能,當初為用java實現而對javamail摸索了一段時間,總算有點收獲。看到論壇中的經常有此方面的問題,因此把我的一些經驗帖出來,希望對大家有些幫助。

          此篇僅介紹用javamail實現發送郵件功能,其中涉及smtp認證,郵件附件發送,及HTML內容郵件等。
          其它有關多郵箱的實現,接收POP3郵件及IMAP等內容,將在后續文章中介紹。

          如下程序需要:javamail,JAF包,j2ee.jar包含了上述兩個包,建議大家安裝J2SDKEE或直接拷貝j2ee.jar,將其添加到jbuilder的library中,或系統ClassPath中

          */



          package com.me.util.mail
          ;

          /**
          * @author Zhangkun aistill@msn.com
          * @version 
          1.0
          */

          import java.util.*
          ;
          import javax.mail.*;
          import javax.mail.internet.*;
          import java.util.Date;
          import javax.activation.*;
          import java.io.*;
          import com.me.util.*;

          public class sendMail {

          private MimeMessage mimeMsg
          ; //MIME郵件對象

          private Session session
          ; //郵件會話對象
          private Properties props; //系統屬性
          private boolean needAuth = false; //smtp是否需要認證

          private String username 
          = ""; //smtp認證用戶名和密碼
          private String password = "";

          private Multipart mp
          ; //Multipart對象,郵件內容,標題,附件等內容均添加到其中后再生成MimeMessage對象



          /**

          */
          public sendMail() {
          setSmtpHost(getConfig.mailHost)
          ;//如果沒有指定郵件服務器,就從getConfig類中獲取
          createMimeMessage();
          }

          public sendMail(String smtp){
          setSmtpHost(smtp)
          ;
          createMimeMessage();
          }



          /**
          * @param hostName String
          */
          public void setSmtpHost(String hostName) {
          System.out.println(
          "設置系統屬性:mail.smtp.host = "+hostName);
          if(props == null)props = System.getProperties(); //獲得系統屬性對象

          props.put(
          "mail.smtp.host",hostName); //設置SMTP主機
          }


          /**
          * @return boolean
          */
          public boolean createMimeMessage()
          {
          try{
          System.out.println(
          "準備獲取郵件會話對象!");
          session = Session.getDefaultInstance(props,null); //獲得郵件會話對象
          }
          catch(Exception e){
          System.err.println(
          "獲取郵件會話對象時發生錯誤!"+e);
          return false;
          }

          System.out.println(
          "準備創建MIME郵件對象!");
          try{
          mimeMsg 
          = new MimeMessage(session); //創建MIME郵件對象
          mp = new MimeMultipart();

          return true
          ;
          }
          catch(Exception e){
          System.err.println(
          "創建MIME郵件對象失敗!"+e);
          return false;
          }
          }



          /**
          * @param need boolean
          */
          public void setNeedAuth(boolean need) {
          System.out.println(
          "設置smtp身份認證:mail.smtp.auth = "+need);
          if(props == null)props = System.getProperties();

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



          /**
          * @param name String
          * @param pass String
          */
          public void setNamePass(String name
          ,String pass) {
          username 
          = name;
          password = pass;
          }


          /**
          * @param mailSubject String
          * @return boolean
          */
          public boolean setSubject(String mailSubject) {
          System.out.println(
          "設置郵件主題!");
          try{
          mimeMsg.setSubject(mailSubject)
          ;
          return true;
          }
          catch(Exception e) {
          System.err.println(
          "設置郵件主題發生錯誤!");
          return false;
          }
          }



          /**
          * @param mailBody String
          */
          public boolean setBody(String mailBody) {
          try{
          BodyPart bp 
          = new MimeBodyPart();
          bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+mailBody,"text/html;charset=GB2312");
          mp.addBodyPart(bp);

          return true
          ;
          }
          catch(Exception e){
          System.err.println(
          "設置郵件正文時發生錯誤!"+e);
          return false;
          }
          }


          /**
          * @param name String
          * @param pass String
          */
          public boolean addFileAffix(String filename) {

          System.out.println(
          "增加郵件附件:"+filename);

          try{
          BodyPart bp 
          = new MimeBodyPart();
          FileDataSource fileds = new FileDataSource(filename);
          bp.setDataHandler(new DataHandler(fileds));
          bp.setFileName(fileds.getName());

          mp.addBodyPart(bp)
          ;

          return true
          ;
          }
          catch(Exception e){
          System.err.println(
          "增加郵件附件:"+filename+"發生錯誤!"+e);
          return false;
          }
          }



          /**
          * @param name String
          * @param pass String
          */
          public boolean setFrom(String from) {
          System.out.println(
          "設置發信人!");
          try{
          mimeMsg.setFrom(new InternetAddress(from))
          ; //設置發信人
          return true;
          }
          catch(Exception e)
          { return false
          ; }
          }


          /**
          * @param name String
          * @param pass String
          */
          public boolean setTo(String to){
          if(to 
          == null)return false;

          try{
          mimeMsg.setRecipients(Message.RecipientType.TO
          ,InternetAddress.parse(to));
          return true;
          }
          catch(Exception e)
          { return false
          ; }

          }

          /**
          * @param name String
          * @param pass String
          */
          public boolean setCopyTo(String copyto)
          {
          if(copyto 
          == null)return false;
          try{
          mimeMsg.setRecipients(Message.RecipientType.CC
          ,(Address[])InternetAddress.parse(copyto));
          return true;
          }
          catch(Exception e)
          { return false
          ; }
          }


          /**
          * @param name String
          * @param pass String
          */
          public boolean sendout()
          {
          try{
          mimeMsg.setContent(mp)
          ;
          mimeMsg.saveChanges();
          System.out.println("正在發送郵件.");

          Session mailSession 
          = Session.getInstance(props,null);
          Transport transport = mailSession.getTransport("smtp");
          transport.connect((String)props.get("mail.smtp.host"),username,password);
          transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
          //transport.send(mimeMsg);

          System.out.println(
          "發送郵件成功!");
          transport.close();

          return true
          ;
          }
          catch(Exception e)
          {
          System.err.println(
          "郵件發送失敗!"+e);
          return false;
          }
          }


          /**
          * Just do it as this
          */
          public static void main(String
          [] args) {

          String mailbody 
          = "<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+
          "<div align=center><a href=http://www.csdn.net> csdn </a></div>";

          sendMail themail 
          = new sendMail("smtp.msn.com");
          themail.setNeedAuth(true);

          if(themail.setSubject(
          "標題"== false) return;
          if(themail.setBody(mailbody) == false) return;
          if(themail.setTo("gates@msn.com"== false) return;
          if(themail.setFrom("bill@msn.com"== false) return;
          if(themail.addFileAffix("c:\\boot.ini"== false) return;
          themail.setNamePass("user","password");

          if(themail.sendout() 
          == false) return; 
          }
          }


          http://dev2dev.bea.com.cn/bbs/thread.jspa?forumID=124&threadID=27140&tstart=330
          我是按照weblogic公司的幫助文檔做的。不知還要配置什么?

          1. 通過console方式創建一個MailSession.

          2. 在Configuration欄中輸入以下參數:
          Name: MyMailSession
          JNDIName: MyMailSession
          Properties
          mail.smtp.host=winqiu
          3. 然后在Targets欄中將mailsession加入你的Target Servers.
          4. 這樣你的mailsession就創建成功了.
          5. 在程序中調用的方法如下:
          import java.util.*;
          import javax.activation.*;
          import javax.mail.Session;
          import javax.mail.*;
          import javax.mail.internet.*;
          import javax.naming.*;

          public class SendMail {
            Context ic 
          = null;

            public SendMail() {
            }

            public void send() {
              
                //使用JNDI查詢Mail Session,
                try
                {
                
                  Hashtable ht 
          = new Hashtable();
                   ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
                   ht.put(Context.PROVIDER_URL, "t3://winqiu:7001");
                   ic = new InitialContext(ht);
                    Session session = (Session) ic.lookup("myMailSession");     

                //設置屬性,然后創建Session實例
                Properties props 
          = new Properties();
                props.put("mail.transport.protocol", "smtp");
                props.put("mail.smtp.host", "winqiu");
                InternetAddress emailAddress = new InternetAddress(
                    
          "win_qiu@yahoo.com.cn");
                props.put("mail.from", emailAddress);
                Session session2 =session.getInstance(props);

                //創建消息
                Message msg 
          = new MimeMessage(session2);
                msg.setFrom();
                String to = "win_qiu@hotmail.com.cn";
                msg.setRecipients(Message.RecipientType.TO,
                                  InternetAddress.parse(to
          , false));
                String subject = "hello";
                msg.setSubject(subject);
                msg.setSentDate(new Date());
              // 將消息內容存放到MimeBodyPart對象中,然后把MimeBodyPart對象
              //存入Multipart對象中,最后,把代表附件的Multipart對象加入待
              //發送的消息中
                MimeBodyPart mbp 
          = new MimeBodyPart();
                String messageTxt = "first mail ";
                mbp.setText(messageTxt);
                
                Multipart mp 
          = new MimeMultipart();
                mp.addBodyPart(mbp);
                msg.setContent(mp);
                
                msg.saveChanges()
          ;
                //發送消息
                Transport.send(msg)
          ;
              }
              catch (NamingException e) {
                e.printStackTrace()
          ;

              }
              catch (MessagingException e) {
                e.printStackTrace()
          ;

              }

            }
            public static void main(String args
          [])
            {
              SendMail sendMail
          =new SendMail();
              sendMail.send();
              System.out.println("send ok");
            }
          }


          但是我的發送郵件時候就出現這樣的異常:
          javax.naming.ConfigurationException.  Root exception is java.rmi.MarshalException: error marshalling return
          ; nested exception is: 
          java.io.NotSerializableException: javax.mail.Session

          at weblogic.rjvm.BasicOutboundRequest.sendReceive(BasicOutboundRequest.java:
          108)

          at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteRef.java:
          284)

          at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteRef.java:
          244)

          at weblogic.jndi.internal.ServerNamingNode_812_WLStub.lookup(Unknown Source)

          at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:
          343)

          at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:
          336)

          at javax.naming.InitialContext.lookup(InitialContext.java:
          347)

          at javamail.SendMail.send(SendMail.java:
          42)

          at javamail.SendMail.main(SendMail.java:
          94)

          Caused by: java.io.NotSerializableException: javax.mail.Session

          at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:
          1054)

          at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:
          278)

          at weblogic.common.internal.ChunkedObjectOutputStream.writeObject(ChunkedObjectOutputStream.java:
          116)

          at weblogic.rjvm.MsgAbbrevOutputStream.writeObject(MsgAbbrevOutputStream.java:
          93)

          at weblogic.jndi.internal.RootNamingNode_WLSkel.invoke(Unknown Source)

          at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:
          477)

          at weblogic.rmi.cluster.ReplicaAwareServerRef.invoke(ReplicaAwareServerRef.java:
          108)

          at weblogic.rmi.internal.BasicServerRef$
          1.run(BasicServerRef.java:420)

          at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:
          353)

          at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:
          144)

          at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:
          415)

          at weblogic.rmi.internal.BasicExecuteRequest.execute(BasicExecuteRequest.java:
          30)

          at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:
          197)

          at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:
          170)

          posted on 2005-12-13 11:41 bluesky 閱讀(1228) 評論(0)  編輯  收藏 所屬分類: 工作總結

          主站蜘蛛池模板: 铜山县| 河南省| 陈巴尔虎旗| 怀化市| 金阳县| 冷水江市| 通许县| 塔城市| 成都市| 南昌县| 佛坪县| 巴林左旗| 镇康县| 读书| 苏尼特左旗| 宝鸡市| 奎屯市| 商都县| 乌拉特前旗| 澄江县| 佛山市| 永安市| 建德市| 鞍山市| 高台县| 安化县| 武穴市| 抚顺县| 绵竹市| 连城县| 陵水| 营山县| 黎川县| 同心县| 高青县| 浦北县| 尉犁县| 慈溪市| 饶平县| 铜梁县| 墨玉县|