发送邮件成功的程序,但转到另一台服务器就报错,有会的进,不会的可以看例子,测试下噢,

wl1985 2004-10-15 12:13:10
测试邮件发送的网址
http://218.30.123.105:8080/jsp/test2
此处发送邮件能成功的例子如上,
注意用户名及密码是填写本服务器的administrator密码暂不公布,大家有兴趣可以在本机发布,源码如下:
服务器环境,支持smtpmail服务,安装了jmail组件,下了两个javamail-1.3.1和javabeans activation framework

然后解压,把mail.jar和activation.jar放到了classpath里。
放在TOMCAT的common\lib目录下:

同样的配置方法我换用另一台服务器上
结果tomcat报如下错误
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Ljava.lang.String;ILjava.lang.String;Ljava
.lang.String;)V(Service.java:264)
at javax.mail.Service.connect(Ljava.lang.String;Ljava.lang.String;Ljava.
lang.String;)V(Service.java:134)
at javax.mail.Service.connect()V(Service.java:86)
at com.sun.mail.smtp.SMTPTransport.connect()V(SMTPTransport.java:138)
at javax.mail.Transport.send0(Ljavax.mail.Message;[Ljavax.mail.Address;)
V(Transport.java:150)
at javax.mail.Transport.send(Ljavax.mail.Message;)V(Transport.java:80)
at research.logic.SmtpAuth.send()Z(SmtpAuth.java:103)
at org.apache.jsp.test2.smtpauth_jsp._jspService(Ljavax.servlet.http.Htt
pServletRequest;Ljavax.servlet.http.HttpServletResponse;)V(smtpauth_jsp.java:63)

at org.apache.jasper.runtime.HttpJspBase.service(Ljavax.servlet.http.Htt
pServletRequest;Ljavax.servlet.http.HttpServletResponse;)V(HttpJspBase.java:133)

at javax.servlet.http.HttpServlet.service(Ljavax.servlet.ServletRequest;
Ljavax.servlet.ServletResponse;)V(HttpServlet.java:856)
at org.apache.jasper.servlet.JspServletWrapper.service(Ljavax.servlet.ht
tp.HttpServletRequest;Ljavax.servlet.http.HttpServletResponse;Z)V(JspServletWrap
per.java:311)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(Ljavax.servlet.ht
tp.HttpServletRequest;Ljavax.servlet.http.HttpServletResponse;Ljava.lang.String;
Ljava.lang.Throwable;Z)V(JspServlet.java:301)
at org.apache.jasper.servlet.JspServlet.service(Ljavax.servlet.http.Http
ServletRequest;Ljavax.servlet.http.HttpServletResponse;)V(JspServlet.java:248)
at javax.servlet.http.HttpServlet.service(Ljavax.servlet.ServletRequest;
Ljavax.servlet.ServletResponse;)V(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Ljav
ax.servlet.ServletRequest;Ljavax.servlet.ServletResponse;)V(ApplicationFilterCha
in.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(Ljavax.servl
et.ServletRequest;Ljavax.servlet.ServletResponse;)V(ApplicationFilterChain.java:
204)
有大虾给看看噢,肯定是服务器的环境问题!
谢谢了!

...全文
119 点赞 收藏 5
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wl1985 2004-10-15
SmtpAuthenticator.java
---------
package research.logic;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;

public class SmtpAuthenticator extends javax.mail.Authenticator
{
String username; //用户名
String password; //密码
//实例化这个类时设置用户名和密码
public SmtpAuthenticator(String username, String password)
{
this.username = username;
this.password = password;
}

//重载getPasswordAuthentication函数
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
}

---------
前台JSP文件如下:
mailform.jsp
package research.logic;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;

public class SmtpAuthenticator extends javax.mail.Authenticator
{
String username; //用户名
String password; //密码
//实例化这个类时设置用户名和密码
public SmtpAuthenticator(String username, String password)
{
this.username = username;
this.password = password;
}

//重载getPasswordAuthentication函数
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
}

----
sendmail.jsp

<%@ page contentType="text/HTML;charset=GB2312"%>
<jsp:useBean id="mailBean" scope="page" class="research.logic.SmtpAuth"/>
<jsp:setProperty name="mailBean" property="*"/>
<%
mailBean.setTo("dxjsunday@yahoo.com");
mailBean.setSubject("ccccc");
mailBean.setBody("cccceeee");
try{
//调用send函数发送邮件
if(mailBean.send())
out.print("Send mail success. ");
else
out.print("Send mail failed.");
}catch(NullPointerException npe){
}catch(Exception e){
out.println(e);
}
%>
回复
miaoliujun 2004-10-15
可能是你换了一台机器不能访问你的mail服务器?
回复
wl1985 2004-10-15
以下是源码:
SmtpAuth.java文件
------
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SmtpAuth{
String from="qqqdxj@163.com"; //发件人地址
String smtp=""; //SMTP服务器
String user="administrator"; //用户名
String pass="lxhflj760824"; //密码
String to=""; //收件人地址
String subject="user password"; //邮件标题
String body=""; //邮件正文
//设置发件人地址
public void setFrom(String from){
this.from=from;
}
public String getFrom(){
return this.from;
}
//设置SMTP服务器
public void setSmtp(String smtp){
this.smtp=smtp;
}
public String getSmtp(){
return this.smtp;
}

//设置用户名
public void setUser(String user){
this.user=user;
}
public String getUser(){
return this.user;
}

//设置密码
public void setPass(String pass){
this.pass=pass;
}
public String getPass(){
return this.pass;
}
//设置收件人地址
public void setTo(String to){
this.to=to;
}
public String getTo(){
return this.to;
}
//设置邮件标题
public void setSubject(String subject){
try{
this.subject=new String(subject.getBytes("iso-8859-1"),"gb2312");
}catch(Exception uee){
//捕捉UnsupportedEncodingException
}
}

public String getSubject(){
return this.subject;
}

//设置邮件正文
public void setBody(String body){
try{
this.body=new String(body.getBytes("iso-8859-1"),"gb2312");
}catch(Exception uee){
//捕捉UnsupportedEncodingException
}
}
public String getBody(){
return this.body;
}
//发送邮件函数
public boolean send(){
try{
//获得系统属性
Properties props=System.getProperties();
//实例SMTP认证对象
SmtpAuthenticator sa=new SmtpAuthenticator(user,pass);
//设置SMTP服务器
props.put("mail.smtp.host", smtp);
//设置需要身份认证为真
props.put("mail.smtp.auth","true");
//获得一个会话
Session session=Session.getInstance(props,sa);
//生成邮件对象
Message msg = new MimeMessage(session);
//设置发件人
msg.setFrom(new InternetAddress(from));
//设置收件人地址
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to,false));
//设置邮件标题
msg.setSubject(subject);
//设置邮件正文
msg.setText(body);
//设置发信时间为系统当前时间
msg.setSentDate(new Date());
//发送邮件return
Transport.send(msg);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
}
回复
djpsunday 2004-10-15
javax.mail.AuthenticationFailedException服务器SMTP认证失败!
访问的程序不太对,改用下你的邮箱帐号及登陆邮箱的密码访问方式
具体请登陆上http://www.cn-java.com/index_focus.php?kind=22&title=JavaMail
有相关的文章及源码,看看就配OK了!
回复
wl1985 2004-10-15
我是换了一台服务器去发布这个程序后就不能调用发送邮件的程序了,
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告