//发送带一个附件的邮件的代码
   public static String sendmail(String from,String to,String reply_to,String host,String subject,String body,String filename) 
   { 
    
    Properties props = System.getProperties();
    props.put("mail.smtp.host", host);
    props.setProperty("mail.smtp.auth","true");
    Session session = Session.getDefaultInstance(props, new myAuth());    
    try {
        // create a message
        if(from.trim().length()<1) 
         {if(reply_to.trim().length()<1)  {from=to;reply_to=to;}
          else from=reply_to;
         }
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        InternetAddress[] address1 = {new InternetAddress(to)};
        msg.setRecipients(Message.RecipientType.TO, address1);
        InternetAddress[] address2 = {new InternetAddress(reply_to)};
        msg.setReplyTo(address2);
        
        
        msg.setSubject(subject);        // create and fill the first message part
        MimeBodyPart mbp1 = new MimeBodyPart();
        mbp1.setText(body);        // create the second message part
        MimeBodyPart mbp2 = new MimeBodyPart();            // attach the file to the message
        FileDataSource fds = new FileDataSource(filename);
        mbp2.setDataHandler(new DataHandler(fds));
        mbp2.setFileName(new String(fds.getName().getBytes("GBK"),"iso8859-1"));        // create the Multipart and its parts to it
        Multipart mp = new MimeMultipart();
        mp.addBodyPart(mbp1);
        mp.addBodyPart(mbp2);        // add the Multipart to the message
        msg.setContent(mp);        // set the Date: header
        msg.setSentDate(new java.util.Date());
        
        // send the message
        Transport.send(msg);
        
       }
     catch (Exception e) {e.printStackTrace();return "发送邮件失败:"+e.toString()+"!";} 
     return "邮件发送成功!";
    
    }
    
//myAuth.java
public class myAuth extends javax.mail.Authenticator
{
    
    public myAuth () {}
    
    
    protected javax.mail.PasswordAuthentication getPasswordAuthentication() 
     {
        return new javax.mail.PasswordAuthentication(用户名,密码);
        
     }
}