JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Application Server » GAE  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 GAE 接收mail問題
ACS097102





發文: 2
積分: 0
於 2011-09-14 17:25 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
用GAE 來接收mail , session不被允許該怎麼辦?

錯誤訊息:
javax.mail.Session is a restricted class. Please see the Google App Engine developer's guide for more details.

之前有在網路上找過方法,
他說只要在appengine-web.xml這個檔案下貼上<sessions-enabled>true</sessions-enabled>即可,
但我試完重新開啟並執行檔案後還是不行...

附上程式碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.util.Properties;
 
import javax.jdo.PersistenceManager;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;
import javax.mail.internet.MimeBodyPart;  
import javax.mail.internet.MimeMessage;  
import javax.mail.internet.MimeMultipart;  
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class GmailFetch extends HttpServlet{
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws IOException {
    
  /*Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());*/
  final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
 
  
  Properties props = System.getProperties();
  props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
  props.setProperty("mail.pop3.socketFactory.fallback", "false");
  props.setProperty("mail.pop3.port", "995");
  props.setProperty("mail.pop3.socketFactory.port", "995");
 
 
  Session session = Session.getDefaultInstance(props,null);
  
  
  URLName urln = new URLName("pop3","pop.gmail.com",995,null,
    "xxxx@gmail.com", "xxxx");
  try {
  Store store = session.getStore(urln);
  Folder inbox = null;
 
  try {
   store.connect();
   inbox = store.getFolder("INBOX");
   inbox.open(Folder.READ_ONLY);
   FetchProfile profile = new FetchProfile();
   profile.add(FetchProfile.Item.ENVELOPE);
   Message[] messages = inbox.getMessages();
   inbox.fetch(messages, profile);
   System.out.println("mail number:" + messages.length);
   for (int i = 0; i < messages.length; i++) {
   
    String from = decodeText(messages[i].getFrom()[0].toString());
    InternetAddress ia = new InternetAddress(from);
    
    String gmail = messages[i].getSubject();
    Mail mail = new Mail (gmail);
    PersistenceManager pm = PMF.get().getPersistenceManager();
    
    try {
      pm.makePersistent(mail);
      
    } finally {
        pm.close();
        
    }  
    resp.sendRedirect("/barcode.jsp");
    
    System.out.println("FROM:" + ia.getPersonal()+'('+ia.getAddress()+')');
    System.out.println("TITLE:" + mail);
    System.out.println("DATE:" + messages[i].getSentDate()+"\n");
    
 
    
   }
   
  } finally {
   try {
    inbox.close(false);
   } catch (Exception e) {}
   try {
    store.close();
   } catch (Exception e) {}
   
   
  }
  }catch (Exception e) {}
 }
 
 protected static String decodeText(String text)
   throws UnsupportedEncodingException {
  if (text == null)
   return null;
  if (text.startsWith("=?GB") || text.startsWith("=?gb"))
   text = MimeUtility.decodeText(text);
  else
   text = new String(text.getBytes("ISO8859_1"));
  return text;
 }
 
 
}


reply to postreply to post
作者 Re:GAE 接收mail問題 [Re:ACS097102]
timeguard





發文: 5
積分: 0
於 2011-09-15 00:05 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
http://code.google.com/intl/zh-TW/appengine/docs/java/mail/usingjavamail.html

請參考google文件庫的內容 注意不支援Javamail的地方

雖然英文版的比較齊全 不過我想這個是不變的...

不支援 JavaMail 功能
應用程式收不到電子郵件訊息。
應用程式無法使用 JavaMail 介面來連線其他郵件服務以傳送或接收電子郵件訊息。新增至 Transport 或 Session 的 SMTP 設定會被忽略。


reply to postreply to post
作者 Re:GAE 接收mail問題 [Re:timeguard]
ACS097102





發文: 2
積分: 0
於 2011-09-15 15:54 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
謝謝樓上大大解答Smile

不過我在英文版的文件中有看到可以接收mail的
以下是文件連接
http://code.google.com/intl/en/appengine/docs/java/mail/receiving.html

而且文件最底下的範例有用到session
才疑惑我的程式碼怎麼不能用..


ACS097102 edited on 2011-09-16 14:37
reply to postreply to post
作者 Re:GAE 接收mail問題 [Re:ACS097102]
mylee





發文: 764
積分: 1
於 2011-09-15 16:08 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
ACS097102 wrote:
謝謝樓上大大解答Smile

不過我在英文版的文件中有看到可以接收mail的
以下是文件連接
http://code.google.com/intl/en/appengine/docs/java/mail/receiving.htm

而且文件最底下的範例有用到session
才疑惑我的程式碼怎麼不能用..


一個是 mail session,一個是 http session,你 enable 的是後者,如果這樣就可以收javamail,張飛就可以打岳飛了。


reply to postreply to post
» JWorld@TW »  Application Server » GAE

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8