[分享]自己改写的一个javamail类
前提是你要下载好mail.jar和activation.jar并配置好路径。****************************************************JSP文件:*******************************************
<%@ page contentType="text/html; charset=GBK" %>
<jsp:useBean id=sm class="mymail.sendmail" scope="page" />
<%
String toAddr="zhangsan@163.com";
String subject="你好,张三";
String body="张三你好,这是用JavaMail发送的邮件,我是snowingsky";
String fromaddr="不告诉你@163.com";
try{
sm.sendmail(toAddr,subject,body,fromaddr);
out.println("邮件成功发送!");
}catch(Exception e){
out.println("出问题啦。。。。");
}
%>
************************************************************Bean文件**************************************
package mymail;
import java.rmi.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.activation.*;
public class sendmail{
public void sendmail(String toAddr, String subject, String body, String fromAddr)throws RemoteException{
try{
Properties props = new Properties();
props.put("mail.smtp.host","10.31.252.36");//这里把IP地址改成你的邮件服务器地址
//Here we specify the SMTP server through
//which the mail should be delivered
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(fromAddr));
//Specify the From Address
InternetAddress[] tos =InternetAddress.parse(toAddr);
//Specify the To Address
msg.setRecipients(Message.RecipientType.TO,tos);
msg.setSubject(subject);
//Specify the Subject
msg.setText(body);
//Specify the Body
Transport.send(msg);
//System.out.println("信件已经发送");
}
catch(Exception e){
System.out.println(e);
}
}
}