记得采纳啊
import javax.mail.*//需要JAVAMAIL或J2EE
import javax.mail.internet.*
import java.util.*
import javax.activation.*
//耐滚需要JAF支持
public class sendmailbean
{
private String from
private String subject
private String toaddress
private String text
private String mailhost
private String result
public sendmailbean()
{
from=null
subject=null
toaddress=null
text=null
mailhost="127.0.0.1"
result=null
}
public void setfrom(String s)
{
from=s
}
public void setsubject(String s)
{
subject=s
}
public void settoaddress(String s)
{
toaddress=s
}
public void settext(String s)
{
text=s
}
public void setmailhost(String s)
{
mailhost=s
System.out.println ("setmailhost: "+s)
}
public String getfrom()
{
return from
}
public String getmailhost()
{
return mailhost
}
public String getsubject()
{
return subject
}
public String gettext()
{
return text
}
public String gettoaddress()
{
return toaddress
}
public String sendmail()
//发送邮件并返回结果result
{
try {
java.util.Properties p=new Properties()
p.put("mail.transport.smtp",mailhost)
//和磨设置邮件服务器为本机。
javax.mail.Session s=Session.getInstance(p,null)
Message msg=new MimeMessage(s)
msg.setFrom(new javax.mail.internet.InternetAddress(from))
//设置发信人地址
msg.setSubject(subject)
//设置主题
javax.mail.internet.InternetAddress mailtoaddress=new InternetAddress()
mailtoaddress.setAddress(toaddress)
//设置收信人地址
msg.setRecipient(Message.RecipientType.TO,mailtoaddress)
msg.setText(text)
//设置内容
Transport trans=s.getTransport("smtp")
trans.send(msg)//发送信件
result="邮件发送成功"//设置返回信息为成功
}
catch (Exception ex) {
result="邮件发送失败: "+ex.toString()//发生异常时设置返回信息为失败并显示异常
}
return result
}
}
上面是bean.
下面是jsp页面.
<%@ page contentType="text/htmlcharset=gb2312" %>
<%@ page import="java.util.*"%>
<jsp:useBean id="mail" scope="application" class="xue.mail.sendmailbean"/>
<%! String from=null%>
<%! String subject=null%>
<%! String toaddress=null%>
<%! String text=null%>
<%! String result=null%>
<html>
<head><title>发送唤亩斗邮件</title></head>
<body>
正在发送邮件....<BR>
<%
text=new String(request.getParameter("text").getBytes("ISO8859_1"))
subject=new String(request.getParameter("subject").getBytes("ISO8859_1"))
from=request.getParameter("from")
toaddress=request.getParameter("toaddress")
mail.setfrom(from)
mail.settoaddress(toaddress)
mail.settext(text)
mail.setsubject(subject)
result=mail.sendmail()
out.println(result)
%>
</body>
</html>
最下面是html表单.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>发送邮件</TITLE>
</HEAD>
<BODY>
<FORM METHOD=POST ACTION="sendmail.jsp">
收信人:<INPUT TYPE="text" NAME="toaddress"><BR>
发信人:<INPUT TYPE="text" NAME="from"><BR>
主 题:<INPUT TYPE="text" NAME="subject"><BR>
内 容:<BR>
<TEXTAREA NAME="text" ROWS="6" COLS="27"></TEXTAREA><BR>
<INPUT TYPE="submit">
</FORM>
</BODY>
</HTML>
全都放到你服务器上正确位置,再改动bean中的mailhost为你的邮件服务器.
就可以用了,
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)