package edu.Flatmanager;/**
 * filename: 
 * @author:
 * date: 
 * note: 
 * @version 
 */import java.util.* ;
import java.io.* ;
import javax.mail.* ;
import javax.mail.internet.* ;
import javax.activation.* ;public class Mail { //定义发件人、收件人、主题等
private String to;
private String from;
private String host;
private String filename;
private String subject;
private String body;
public static String FILE_PATH = "/sendmail/attachfile/";
public static String SMTP_SERVER = "smtp.xxxx.com"; //用于保存发送附件的文件名的集合
Vector file = new Vector(); //做一个可以传发件人等参数的构造
public Mail (String to,String from,String smtpServer,String subject,String body){
//初始化发件人、收件人、主题等
this.to=to;
this.from=from;
this.host=smtpServer;
this.subject=subject;
this.body = body;
} //该方法用于收集附件名
public void attachfile(String fname){
file.addElement(fname);
} //开始发送信件的方法
public boolean startSend(){ //创建Properties对象
Properties props = System.getProperties(); //创建信件服务器
props.put("mail.smtp.host", host); //得到默认的对话对象
Session session=Session.getDefaultInstance(props, null); try { //创建一个消息,并初始化该消息的各项元素
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address); msg.setSubject(subject); //后面的BodyPart将加入到此处创建的Multipart中
Multipart mp = new MimeMultipart(); //msg.setText(body);
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
mp.addBodyPart(messageBodyPart); //利用枚举器方便的遍历集合
Enumeration efile=file.elements(); //检查序列中是否还有更多的对象
while(efile.hasMoreElements()){

MimeBodyPart mbp=new MimeBodyPart();
//选择出每一个附件名
filename=efile .nextElement().toString();
//得到数据源
FileDataSource fds=new FileDataSource(filename);
//得到附件本身并至入BodyPart
mbp.setDataHandler(new DataHandler(fds));
//得到文件名同样至入BodyPart
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
} //移走集合中的所有元素
file.removeAllElements(); //Multipart加入到信件
msg.setContent(mp); //设置信件头的发送日期
msg.setSentDate(new Date()); //发送信件
Transport.send(msg);
}
catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex=mex.getNextException())!=null){
ex.printStackTrace();
}
return false;
}
return true;
}
}

解决方案 »

  1.   

    <%@ page contentType="text/html; charset=GBK" %>
    <%@ page language="java" import="java.util.*,javax.mail.*,javax.mail.internet.*"%><%
    try{
                String host = "邮件发送服务器地址";
                String from =   "发送邮箱名";
                String to = "接收邮箱名";
                String username = "发送邮箱的发送验证用户名";
                String password = "发送邮箱的发送验证口令";            // Get system properties
                // Properties props = System.getProperties(); 很多例子中是这样的,其实下面这句更好,可以用在applet中
                Properties props = new Properties();            // Setup mail server
                props.put("mail.smtp.host", host);
                props.put("mail.smtp.auth", "true"); //这样才能通过验证            // Get session
                Session sessionDLT = Session.getDefaultInstance(props);            // watch the mail commands go by to the mail server
                sessionDLT.setDebug(true);            // Define message
                MimeMessage message = new MimeMessage(sessionDLT);
                message.setFrom(new InternetAddress(from));
                message.addRecipient(Message.RecipientType.TO,
                   new InternetAddress(to));
                message.setSubject("Hello JavaMail");
                message.setText("Welcome to JavaMail");            // Send message
                message.saveChanges();
                Transport transport = sessionDLT.getTransport("smtp");
                transport.connect(host, username, password);
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();
                out.println("您的邮件发送成功!");}catch(Exception e){
      out.println(e.getMessage());
    }%>
    非常方便!