Java论坛网»Java技术»求教:关于电子邮件自动发送的问题(jsp)(急!)
求教:关于电子邮件自动发送的问题(jsp)(急!)
问?:
我编写了一个程序以学习JavaMail API,下载的JavaMail API为1.4版本,JavaBeans Activation Framework不jaf-1.1,编写的bean如下:
package bean;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.rmi.*;
import javax.activation.*;
import java.io.*;
public class MailManager
{
private final String pop3_host_name="pop3.sohu.com";
private final String smtp_host_name="smtp.sohu.com";
private final String smtp_auth_user="hjjq101752";
private final String smtp_auth_pwd="19810929";
private Authenticator auth=new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(smtp_auth_user,smtp_auth_pwd);
}
};
public void sendMail(String toAddr,String subject,String body,String fromAddr,String contentType)
{
try
{
Properties props=new Properties();
props.put("mail.smtp.host",smtp_host_name);
props.put("mail.smtp.auth","true");
Session session=Session.getDefaultInstance(props,auth);
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(fromAddr));
InternetAddress[] tos={new InternetAddress(toAddr)};
msg.setRecipients(Message.RecipientType.TO,tos);
msg.setSubject(subject);
msg.setText(body);
msg.setSentDate(new Date());
msg.setContent(body,contentType);
Transport.send(msg);
}
catch(Exception e)
{
System.out.println(e);
}
}
};
又编写了两个jsp页面,sendmail.jsp及action_sendmail.jsp
sendmail.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>电子邮件</title>
<script language="JavaScript">
function checkdata()
{
var txt=document.forms[0].email.value;
if(txt.search("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")!=0)
{
alert("请输入正确的电子邮件!");
document.forms[0].email.select();
return false;
}
return true;
}
</script>
</head>
<body>
<p>
<form action="action_sendmail.jsp" method="post" onsubmit="return checkdata()">
<p>请输入电子邮件:<input type="text" name="email">
<p><input type="submit" value="发送">
</form>
</body>
</html>
action_sendmail.jsp
<%@ page contentType="text/html;charset=GBK" %>
<html>
<head>
<title>发送电子邮件</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<jsp:useBean id="mail" scope="page" class="bean.MailManager" />
<%
String email = request.getParameter("email");
if(email==null || email.trim().length()==0||!email.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")) {
out.print("邮件地址为空");
return;
}
mail.sendMail(email, "Test", "Welcome to javaMail", "kx929@163.com", "text/plain");
out.print("邮件已发送");
%>
</body>
</html>
其中,bean的编译没有问题,运行sendmail.jsp,填写收信人的电子邮件后,也提示“邮件已发送”,但是到邮件里查询却没收到。我不知道这是什么原因,请大家指教,谢谢!
package bean;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.rmi.*;
import javax.activation.*;
import java.io.*;
public class MailManager
{
private final String pop3_host_name="pop3.sohu.com";
private final String smtp_host_name="smtp.sohu.com";
private final String smtp_auth_user="hjjq101752";
private final String smtp_auth_pwd="19810929";
private Authenticator auth=new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(smtp_auth_user,smtp_auth_pwd);
}
};
public void sendMail(String toAddr,String subject,String body,String fromAddr,String contentType)
{
try
{
Properties props=new Properties();
props.put("mail.smtp.host",smtp_host_name);
props.put("mail.smtp.auth","true");
Session session=Session.getDefaultInstance(props,auth);
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(fromAddr));
InternetAddress[] tos={new InternetAddress(toAddr)};
msg.setRecipients(Message.RecipientType.TO,tos);
msg.setSubject(subject);
msg.setText(body);
msg.setSentDate(new Date());
msg.setContent(body,contentType);
Transport.send(msg);
}
catch(Exception e)
{
System.out.println(e);
}
}
};
又编写了两个jsp页面,sendmail.jsp及action_sendmail.jsp
sendmail.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>电子邮件</title>
<script language="JavaScript">
function checkdata()
{
var txt=document.forms[0].email.value;
if(txt.search("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")!=0)
{
alert("请输入正确的电子邮件!");
document.forms[0].email.select();
return false;
}
return true;
}
</script>
</head>
<body>
<p>
<form action="action_sendmail.jsp" method="post" onsubmit="return checkdata()">
<p>请输入电子邮件:<input type="text" name="email">
<p><input type="submit" value="发送">
</form>
</body>
</html>
action_sendmail.jsp
<%@ page contentType="text/html;charset=GBK" %>
<html>
<head>
<title>发送电子邮件</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<jsp:useBean id="mail" scope="page" class="bean.MailManager" />
<%
String email = request.getParameter("email");
if(email==null || email.trim().length()==0||!email.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")) {
out.print("邮件地址为空");
return;
}
mail.sendMail(email, "Test", "Welcome to javaMail", "kx929@163.com", "text/plain");
out.print("邮件已发送");
%>
</body>
</html>
其中,bean的编译没有问题,运行sendmail.jsp,填写收信人的电子邮件后,也提示“邮件已发送”,但是到邮件里查询却没收到。我不知道这是什么原因,请大家指教,谢谢!
答!: 1:
第一种可能:的确发送成功!但过一会会收到,第二:
你让send 方法 抛 Exception 在jsp中catch然后输出html代码,现实在web page上
try
{
mail.sendMail(email, "Test", "Welcome to javaMail", "kx929@163.com", "text/plain");
}
catch(Exception e)
{
out.print(e);
}
你让send 方法 抛 Exception 在jsp中catch然后输出html代码,现实在web page上
try
{
mail.sendMail(email, "Test", "Welcome to javaMail", "kx929@163.com", "text/plain");
}
catch(Exception e)
{
out.print(e);
}
答!: 2:
楼上的你好,先谢谢你,我用你说的方法试了一下,但是并没有捕获到异常。
相关JAVA教程:
急需高手帮忙!高手请进分不够再给
struts-menu实现的问题
发现java .net 没什么好搞的!
安装j2ee1.4遇到的问题
这个问题应该怎么解决,郁闷了好几天
Hibernate应用程序中能不能同时读取两个hibernate.hbm.xml????
Struts 上传问题 (急) (在线等)
急问:如何将tomcat下的web应用迁移到Resin下?
关于面板数组的使用
如何在jpanel的mouseClicked事件中,判断ctrl和shift键是否按下??
页面报错问题
关闭其他JAR包中窗体的问题