首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

高分求JAVAMAIL连接SMTP服务器的有关问题,绝顶高手快来啊

2013-09-11 
高分求JAVAMAIL连接SMTP服务器的问题,急急急,绝顶高手快来啊!!!!!!!!!!!!!!!JSP源代码:%@pageimport ja

高分求JAVAMAIL连接SMTP服务器的问题,急急急,绝顶高手快来啊!!!!!!!!!!!!!!!
JSP源代码:
<%@   page   import= "java.util.* "   %>
<%@   page   import= "javax.mail.* "   %>
<%@   page   import= "javax.activation.* "   %>
<%@   page   import= "javax.mail.internet.* "   %>

<%@   page   contentType= "text/html;charset=GB2312 "   %>

<html>
<head>
    <title> CH17   -   JavaMail2.jsp </title>
</head>
<body>
<h2> 利用JavaMail来传送电子邮件   -   HTML格式 </h2>


<%
request.setCharacterEncoding( "GB2312 ");

String   host       =   "smtp.163.com ";
String   rec                   =   "jonathantest@163.com ";//request.getParameter( "To ");
String   sen                     =   "jonathantest@163.com ";//request.getParameter( "From ");
String   sub             =   "Hello ";//request.getParameter( "Subject ");
String   mess         =   "success ";//request.getParameter( "Message ");
String   username       =   "jonathantest ";      
String   password           =   "aabbcc1122 ";      

    boolean   sessionDebug   =   false;
Properties   prop   =   System.getProperties();
prop.put( "mail.host ",host);
prop.put( "mail.transport.protocol ", "smtp ");
prop.put( "mail.smtp.auth ", "true ");

javax.mail.Session   mailsess   =   Session.getDefaultInstance(prop);
mailsess.setDebug(sessionDebug);

Message   msg   =   new   MimeMessage(mailsess);
msg.setFrom(new   InternetAddress(sen));
InternetAddress[]   address=   {new   InternetAddress(rec)};
msg.setRecipients(Message.RecipientType.TO,   address);
msg.setSubject(sub);
msg.setText(mess);

Transport   transport   =   mailsess.getTransport( "smtp ");
transport.connect(host,   username,   password);
transport.sendMessage(msg,msg.getAllRecipients());      
transport.close();
%>
</body>
</html>
出错提示:
type   Exception   report

message  

description   The   server   encountered   an   internal   error   ()   that   prevented   it   from   fulfilling   this   request.

exception  

org.apache.jasper.JasperException:   Exception   in   JSP:   /JavaMail.jsp:45

42:   msg.setText(mess);
43:  
44:   Transport   transport   =   mailsess.getTransport( "smtp ");
45:   transport.connect(host,   username,   password);
46:   transport.sendMessage(msg,msg.getAllRecipients());      
47:   transport.close();
48:   //transport.send(msg);


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:506)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)


org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root   cause  

javax.servlet.ServletException:   Could   not   connect   to   SMTP   host:   smtp.163.com,   port:   25
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:843)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:776)
org.apache.jsp.JavaMail_jsp._jspService(JavaMail_jsp.java:187)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root   cause  

javax.mail.MessagingException:   Could   not   connect   to   SMTP   host:   smtp.163.com,   port:   25;
    nested   exception   is:
java.net.ConnectException:   Connection   timed   out:   connect
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
javax.mail.Service.connect(Service.java:275)
javax.mail.Service.connect(Service.java:156)
org.apache.jsp.JavaMail_jsp._jspService(JavaMail_jsp.java:90)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note   The   full   stack   trace   of   the   root   cause   is   available   in   the   Apache   Tomcat/5.5.20   logs.

我关闭过防火墙,网络连接正常,但是怎么也连接不上SMTP服务器,请高手们指点!

[解决办法]
我不是绝顶高手所以。。。。。。。。。。。。。。。。。

[解决办法]
顶!
同样的问题!
[解决办法]
顶,同样的问题
[解决办法]
props.put( "mail.smtp.host ", smtpHost);不是prop.put( "mail.host ",host);
props.put( "mail.pop3.host ", popHost);
props.put( "mail.smtp.auth ", "true ");
只要有以上3项就能收发email了,当然要重写验证哪个类.
[解决办法]
Could not connect to SMTP host: smtp.163.com, port: 25

服务器地址没错吧。。。
[解决办法]
不知道这个可以不可以!~
package com.utils;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;


import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import sun.misc.BASE64Encoder;

//import com.me.util.*;

public class sendMail {

private MimeMessage mimeMsg; // MIME邮件对象

private Session session; // 邮件会话对象

private Properties props; // 系统属性

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));
BASE64Encoder enc = new BASE64Encoder();
bp.setFileName( "=?GBK?B? "+enc.encode((fileds.getName()).getBytes())+ "?= ");
//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));

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) {

// mailbody-> 发送 的邮件 内容
String mailbody = " <meta http-equiv=Content-Type content=text/html; charset=gb2312> "
+ " <div align=center> <table border=1> <tr> <td> 姓名 </td> <td> 性别 </td> </tr> <tr> <td> <font color=blue> 大白菜 </font> </td> <td> 120 </td> </tr> </table> </div> ";

// themail表示要发送到那个smtp上去(转发的地位)
sendMail themail = new sendMail( "smtp.sohu.com ");

// 是否要验证用户身份
themail.setNeedAuth(true);

// 邮件标题
if (themail.setSubject( "大白菜say hello! ") == false)
return;
if (themail.setBody(mailbody) == false)
return;

// 指明发送到那个用户
if (themail.setTo( "asdccn@cmr.com.cn ") == false)
return;

// 显示是谁发来得
if (themail.setFrom( "qinhaijun1@sohu.com ") == false)
return;


// 表明要带的附件
if (themail.addFileAffix( " ") == false)
return;
themail.setNamePass( "yubinbin678 ", "123123 ");

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

我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html

热点排行