package com.firsttech.ivan;import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
//import javax.activation.*;
public class TestMail {
private Session session=null;
private String server,username,password;
public MimeMessage getMessage(){
Properties props=new Properties();
props.put("mail.smtp.host",server);
props.put("mail.smtp.auth","true");
Authenticator auth=new MailAuthenticator(username,password);
//session=Session.getDefaultInstance(props,auth);
session=Session.getInstance(props,auth);
session.setDebug(true);
MimeMessage mimemessage=new MimeMessage(session);
return mimemessage;
}
public void setHost(String server,String username,String password){
this.server=server;
this.username=username;
this.password=password;
}
public void sendCommonMail(String subject,String from,String to,String content)throws MessagingException{
MimeMessage mimemessage=getMessage();
mimemessage.setSubject(subject);
mimemessage.setSentDate(new Date());
mimemessage.setText(content);
Address address=new InternetAddress(from);
mimemessage.setFrom(address);
String to_addr[]=to.split(",");
Address toaddress[]=new InternetAddress[to_addr.length];
for(int i=0;i<to_addr.length;i++)
toaddress[i]=new InternetAddress(to_addr[i]);
mimemessage.addRecipients(Message.RecipientType.TO,toaddress);
mimemessage.saveChanges();
Transport transport=session.getTransport("smtp");
transport.connect(server,username,password);
System.out.print(transport.isConnected());
transport.sendMessage(mimemessage,mimemessage.getAllRecipients());
transport.close();
} public static void main(String[] args)throws MessagingException {
String server="smtp.163.com";
String username="username";
String password="password";
String subject="这是邮件的主题";
String from="[email protected]";
String to="[email protected]";
String content="这是一个邮件的正文部分";
TestMail sm=new TestMail();
sm.setHost(server, username, password);
sm.sendCommonMail(subject, from, to, content);
}
}class MailAuthenticator extends Authenticator{
private String username = null;
private String password = null; public MailAuthenticator(){
super();
}
public MailAuthenticator(String username, String password){
super();
this.username=username;
this.password=password;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,password);
}
}
执行的时候提示
DEBUG: setDebug: JavaMail version 1.3.1
java.lang.NoClassDefFoundError: com/sun/mail/util/SharedByteArrayInputStream
at com.firsttech.ivan.TestMail.getMessage(TestMail.java:25)
at com.firsttech.ivan.TestMail.sendCommonMail(TestMail.java:36)
at com.firsttech.ivan.TestMail.main(TestMail.java:65)
Exception in thread "main"
我的jdk是1.4的
各位大侠帮帮忙,谢谢
import javax.mail.internet.*;
import java.util.*;
//import javax.activation.*;
public class TestMail {
private Session session=null;
private String server,username,password;
public MimeMessage getMessage(){
Properties props=new Properties();
props.put("mail.smtp.host",server);
props.put("mail.smtp.auth","true");
Authenticator auth=new MailAuthenticator(username,password);
//session=Session.getDefaultInstance(props,auth);
session=Session.getInstance(props,auth);
session.setDebug(true);
MimeMessage mimemessage=new MimeMessage(session);
return mimemessage;
}
public void setHost(String server,String username,String password){
this.server=server;
this.username=username;
this.password=password;
}
public void sendCommonMail(String subject,String from,String to,String content)throws MessagingException{
MimeMessage mimemessage=getMessage();
mimemessage.setSubject(subject);
mimemessage.setSentDate(new Date());
mimemessage.setText(content);
Address address=new InternetAddress(from);
mimemessage.setFrom(address);
String to_addr[]=to.split(",");
Address toaddress[]=new InternetAddress[to_addr.length];
for(int i=0;i<to_addr.length;i++)
toaddress[i]=new InternetAddress(to_addr[i]);
mimemessage.addRecipients(Message.RecipientType.TO,toaddress);
mimemessage.saveChanges();
Transport transport=session.getTransport("smtp");
transport.connect(server,username,password);
System.out.print(transport.isConnected());
transport.sendMessage(mimemessage,mimemessage.getAllRecipients());
transport.close();
} public static void main(String[] args)throws MessagingException {
String server="smtp.163.com";
String username="username";
String password="password";
String subject="这是邮件的主题";
String from="[email protected]";
String to="[email protected]";
String content="这是一个邮件的正文部分";
TestMail sm=new TestMail();
sm.setHost(server, username, password);
sm.sendCommonMail(subject, from, to, content);
}
}class MailAuthenticator extends Authenticator{
private String username = null;
private String password = null; public MailAuthenticator(){
super();
}
public MailAuthenticator(String username, String password){
super();
this.username=username;
this.password=password;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,password);
}
}
执行的时候提示
DEBUG: setDebug: JavaMail version 1.3.1
java.lang.NoClassDefFoundError: com/sun/mail/util/SharedByteArrayInputStream
at com.firsttech.ivan.TestMail.getMessage(TestMail.java:25)
at com.firsttech.ivan.TestMail.sendCommonMail(TestMail.java:36)
at com.firsttech.ivan.TestMail.main(TestMail.java:65)
Exception in thread "main"
我的jdk是1.4的
各位大侠帮帮忙,谢谢
JavaMail的.jar文件都放到项目的/lib下面了?还有Classpath下面的\lib目录下了?
mail.jsp<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<%@ page contentType="text/html;charset=gb2312"%>
<title>填写邮件信息</title>
</head><body bgcolor="#FFFFCC">
<form name="form1" method="post" action="sendMail.jsp">
<table width="75" border="0" align="center" cellspacing="1" bgcolor="#006600" height="258"><div align="center"><font size="5" color="blue">填写邮件信息</font>
<tr bgcolor="#FFFFFF">
<td width="30%" height="34">发件人地址:</td>
<td width="70%" height="34">
<input name="from" type="text" id="from" CLASS="kuang-bg"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="30%" height="34">密码:</td>
<td width="70%" height="34">
<input name="password" type="text" id="password" CLASS="kuang-bg"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="30%" height="34">收信人地址:</td>
<td width="70%" height="34">
<input name="to" type="text" id="to" CLASS="kuang-bg"></td>
</tr><tr bgcolor="#FFFFFF">
<td width="30%" height="25">主题:</td>
<td width="70%" height="25">
<input name="title" type="text" id="title" CLASS="kuang-bg"></td>
</tr>
<tr>
<td height="119" colspan="2" bgcolor="#FFFFFF">
<textarea name="content" cols="50" rows="5" id="content" CLASS="kuang-bg"></textarea></td>
</tr>
<tr align="center">
<td colspan="2" bgcolor="#FFFFFF" height="27">
<input type="submit" name="Submit" value="发 送" CLASS="botton">
<input type="reset" name="Submit2" value="重 写" CLASS="botton">
</td>
</tr>
</table>
</form>
</body>
</html>
sendMail.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<!--%request.setCharacterEncoding("gb2312");%--><!--中文处理代码-->
<!--导入要用到的类库-->
<%@ page import="java.util.*"%>
<%@ page import="javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%>
<title>发送邮件</title>
<%
try{
//从html表单中获取邮件信息
String tfrom=request.getParameter("from");
String password=request.getParameter("password");
String tto=request.getParameter("to");
String ttitle=request.getParameter("title");
String tcontent=request.getParameter("content");
String strHost=null;
out.println(tfrom);
//out.println(tto);
//out.println(ttitle);
//out.println(tcontent);
int a=tfrom.indexOf('@');
String s2=tfrom.substring(a);
//System.out.println(s2);
String s3=s2.substring(1,3);
//System.out.println(s3);
if(s3.equals("si")){
strHost="smtp.sina.com.cn";
}
else if(s3.equals("16")){
strHost="smtp.163.com";
}
else if(s3.equals("21")){
strHost="smtp.21cn.com";
}
else if(s3.equals("12")){
strHost="smtp.126.com";
}
else if(s3.equals("ya")){
out.println("<script>alert(\"不支持yahoo邮箱!\");window.location.href='mail.jsp'</script>");
}
else if(s3.equals("qq")){
out.println("<script>alert(\"不支持QQ邮箱!\");window.location.href='mail.jsp'</script>");
}
//JavaMail需要Properties来创建一个session对象。它将寻找字符串"mail.smtp.host",属性值就是发送邮件的主机.
//Properties对象获取诸如邮件服务器、用户名、密码等信息,以及其他可在整个应用程序中 共享的信息。Properties props=new Properties();//也可用Properties props = System.getProperties();
props.put("mail.smtp.host",strHost);//存储发送邮件服务器的信息
props.put("mail.smtp.auth","true");//同时通过验证//方法二:(如果是在weblogin配置JavaMail:则需指定JNDI名检索
//Context ctx=new InitialContext();
//Session s=(Session)ctx.lookup("MailSession");
//Message msg=new MimeMessage(s);
//这个Session类代表JavaMail 中的一个邮件session. 每一个基于 JavaMail的应用程序至少有一个session但是可以有任意多的session。
//Session类定义全局和每个用户的与邮件相关的属性。这此属性说明了客房机和服务器如何交流信息。
Session s=Session.getDefaultInstance(props,null);//根据属性新建一个邮件会话,null参数是一种Authenticator(验证程序) 对象
s.setDebug(true);//设置调试标志,要查看经过邮件服务器邮件命令,可以用该方法// 一旦创建了自己的Session对象,就是该去创建要发送的消息的 时候了。这时就要用到消息类型(MimeMessage是其中一种类型)。
// Message对象将存储我们实际发送的电子邮件信息,Message对象被作为一个MimeMessage对象来创建并且需要知道应当选择哪一个JavaMail session。
// Message类表示单个邮件消息,它的属性包括类型,地址信息和所定义的目录结构。Message message=new MimeMessage(s);//由邮件会话新建一个消息对象//message.setContent("hello","test/plain");//设置消息的内容类型,如果发送的格式有HTML格式就必须设置,
//message.setText("Hello");//发送一般文本格式的消息//设置邮件,一旦您创建了 Session 和 Message,并将内容填入消息后,就可以用Address确定信件地址了。
//如果想让一个名字出现在电子邮件地址后,也可以将其传递给构造器:
//Address from=new InternetAddress("[email protected]","qdison");//发件人的邮件地址Address from=new InternetAddress(tfrom);//发件人的邮件地址
message.setFrom(from);//设置发件人Address to=new InternetAddress(tto);//收件人的邮件地址
message.setRecipient(Message.RecipientType.TO,to);//设置收件人,并设置其接收类型为TO,还有3种预定义类型如下://Message.RecipientType.TO
//Message.RecipientType.CC
//Message.RecipientType.BCC message.setSubject(ttitle);//设置主题
message.setText(tcontent);//设置信件内容
message.setSentDate(new Date());//设置发信时间message.saveChanges();//存储邮件信息
// Transport 是用来发送信息的,
// 用于邮件的收发打操作。
Transport transport=s.getTransport("smtp");
transport.connect(strHost,25,tfrom,password);//以smtp方式登录邮箱 25
transport.sendMessage(message,message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址
transport.close();%>
<div align="center">
<p><font color="#FF6600">发送成功!</font></p>
<p><a href="mail.jsp">再发一封</a> </p>
</div>
<%
}catch(MessagingException e){
out.println(e.toString()+"发送失败");
}
%>
自己看看吧