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

» JWorld@TW » EJB / 其它Java EE 討論區 » JavaMail  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Unknown SMTP host: smtp.gmail.com的問題
genesis0000





發文: 3
積分: 0
於 2015-12-11 15:30 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
請教各位前輩
我在自己電腦跑localhost測試寄信都是成功的,後來我用學校電腦架了server,用學校電腦跑寄信的時候失敗了
錯誤為:Unknown SMTP host: smtp.gmail.com
nslookup戳一下smtp.gmail.com也表示找不到,但如果是用自己電腦localhost戳就會回傳address

這是我的code

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
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication; 
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
 
@WebServlet("/emailtest")
public class emailtest extends HttpServlet {
  private static final long serialVersionUID = 1;
  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF-8"); 
    InternetAddress[] address = null ;
    String mailServer = "smtp.gmail.com";
    String From = "myemail@gmail.com";
    String To = "myemail@yahoo.com.tw";
    String error="";
    String Subject  = "這是一封測試信";
    String messageText  = "您的密碼是@#$#$\n建議盡速更改您的密碼";
    try {
      Properties props = System.getProperties();
      props.put("mail.host",mailServer);
      props.put("mail.transport.protocol","smtp");
      props.put("mail.smtp.auth", "true");
       props.put("mail.smtp.socketFactory.port", "587");
       props.put("mail.smtp.socketFactory.class", "javax.net.SocketFactory");
       props.put("mail.smtp.auth", "true");
       props.put("mail.smtp.port", "587");
       props.put("mail.smtp.ssl.enable", "false");
       props.put("mail.smtp.starttls.enable", "true");
       props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
      
      //建立認證物件
       Authenticator auth = new javax.mail.Authenticator() {   
      String userName="帳號"; 
      String password = "密碼";
      protected PasswordAuthentication getPasswordAuthentication(){   
        return new PasswordAuthentication(userName, password);   
        }   
      };   
       
      Session mailSession = javax.mail.Session.getInstance(props,auth); 
      mailSession.setDebug(false);
 
      MimeMessage msg = new MimeMessage(mailSession);
      
      msg.setFrom(new InternetAddress(From));
      
      address = InternetAddress.parse(To,false);
 
      msg.setRecipients(Message.RecipientType.TO, address);
      msg.setSubject(Subject,"Big5"); 
      msg.setSentDate(new Date());   
      msg.setText(messageText, "Big5"); 
      Transport.send(msg);
      System.out.println("成功傳送");
      error="email.jsp?error=sucess";
    }
    catch (MessagingException mex) {
      System.out.println("發生錯誤,例外物件的型別為[");
      System.out.println(mex.toString()); 
      System.out.println("]");
      error="email.jsp?error="+"mex.toString()";
    } 
    response.sendRedirect(error);
  }
  
  
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
  }
 
}
 


爬文過,知道應該是防火牆的問題,我設定了port587但也還是不行(如圖)
有可能我防火牆設定錯了,但我不知道錯在哪&要怎麼改Sad


reply to postreply to post
作者 Re:Unknown SMTP host: smtp.gmail.com的問題 [Re:genesis0000]
weber





發文: 194
積分: 0
於 2015-12-15 14:03 user profilesend a private message to usersend email to weberreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
問題應不在於您的電腦或您的防火牆設定, 而在於您學校的網路是否允許您
access smtp.gamil.com.


reply to postreply to post
» JWorld@TW »  EJB / 其它Java EE 討論區 » JavaMail

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