当前位置: 技术问答>java相关
javamail包中 这个 javax.activation.*;在那里(有提示作用的有分)
来源: 互联网 发布时间:2017-04-10
本文导语: 我在在SUN公司的网站下载了javamail-1.3 有这四个包imap.jar,mailapi.jar,pop3.jar,smtp.jar 可import 这个就出错javax.activation.*;请问这个包在那里? | download activation.jar from http://java.sun.com/products/javabeans/glasgow/jaf...
我在在SUN公司的网站下载了javamail-1.3
有这四个包imap.jar,mailapi.jar,pop3.jar,smtp.jar
可import 这个就出错javax.activation.*;请问这个包在那里?
有这四个包imap.jar,mailapi.jar,pop3.jar,smtp.jar
可import 这个就出错javax.activation.*;请问这个包在那里?
|
download activation.jar from http://java.sun.com/products/javabeans/glasgow/jaf.html; add it to your class path
2. yes
2. yes
|
一个发送MAIL的BEAN
package homepage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Hashtable;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
/**
* Title: JavaMail system
* Description:
* Copyright: Copyright (c) 2002
* Company: home
* @author coolwen.com
* @version 1.0
*/
public class SmtpAuthenticator extends javax.mail.Authenticator{
//SMTP身份验证
protected javax.mail.PasswordAuthentication getPasswordAuthentication()
{
return new javax.mail.PasswordAuthentication("coolwen.com","password");
}
}
package homepage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Hashtable;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.File;
/**
* Title: JavaMail system
* Description:
* Copyright: Copyright (c) 2002
* Company: home
* @author coolwen.com
* @version 1.0
*/
public class SendMail{
private String smtp="smtp.163.com",from="coolwen.com@163.com";
//发送一个邮件
public boolean SendMail(String subject,String body,String to,String filename){
try {
SmtpAuthenticator sa=new SmtpAuthenticator();
Properties props=System.getProperties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host",smtp);
Session sess=Session.getInstance(props,sa);
sess.setDebug(true);
Message msg = new MimeMessage(sess);
msg.setDataHandler(new DataHandler(body,"text/html; charset=gb2312"));
if(filename.equals("") || filename == null||filename.equals("null"))
{
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
msg.setSubject(subject);
}else{
msg.setFrom(new InternetAddress(from));
msg.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body);
MimeBodyPart mbp2 = new MimeBodyPart();
//File file=new File(filename);
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setContent(mp);
}
msg.setSentDate(new Date());
//msg.setText(body);
Transport.send(msg);
return true;
} catch (Exception e) {
return false;
}
}
}
在JSP中使用
原作者:树叶
来 源:网易
package homepage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Hashtable;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
/**
* Title: JavaMail system
* Description:
* Copyright: Copyright (c) 2002
* Company: home
* @author coolwen.com
* @version 1.0
*/
public class SmtpAuthenticator extends javax.mail.Authenticator{
//SMTP身份验证
protected javax.mail.PasswordAuthentication getPasswordAuthentication()
{
return new javax.mail.PasswordAuthentication("coolwen.com","password");
}
}
package homepage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Hashtable;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.File;
/**
* Title: JavaMail system
* Description:
* Copyright: Copyright (c) 2002
* Company: home
* @author coolwen.com
* @version 1.0
*/
public class SendMail{
private String smtp="smtp.163.com",from="coolwen.com@163.com";
//发送一个邮件
public boolean SendMail(String subject,String body,String to,String filename){
try {
SmtpAuthenticator sa=new SmtpAuthenticator();
Properties props=System.getProperties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host",smtp);
Session sess=Session.getInstance(props,sa);
sess.setDebug(true);
Message msg = new MimeMessage(sess);
msg.setDataHandler(new DataHandler(body,"text/html; charset=gb2312"));
if(filename.equals("") || filename == null||filename.equals("null"))
{
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
msg.setSubject(subject);
}else{
msg.setFrom(new InternetAddress(from));
msg.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body);
MimeBodyPart mbp2 = new MimeBodyPart();
//File file=new File(filename);
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setContent(mp);
}
msg.setSentDate(new Date());
//msg.setText(body);
Transport.send(msg);
return true;
} catch (Exception e) {
return false;
}
}
}
在JSP中使用
原作者:树叶
来 源:网易
|
1,tomcatcommonlibactivation.jar(安装tomcat后会有自带)
2,yes
2,yes
|
你要是有j2ee.jar
import它,什么都省了
import它,什么都省了
|
加入类路径中不就行了吗