我给你一个我的例子你看看合不合用
package com.investoday.util;import java.io.*;
import java.io.IOException;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import com.investoday.util.StringUtil;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
public class SmtpMail {  private String to = null; //收件人
  private String cc = null; //抄送
  private String bcc = null; //暗送
  private String from = null; //发件人
  private String subject = null;//发送的主题
  private String content = null;//发送的内容
  private String mailhost = null;//smtp的主机dns
  private String user=null;
  private String pass = null;  private String fileList = "";//你所上传的文件列表
  private Vector vfile = new Vector(10,10);
  private StringBuffer contents;
  private String[][] replaceText;
  private boolean debug = false ;  //构造函数
  public SmtpMail() {
    init();
  }  //初始化参数
  public void init() {    this.from = "[email protected]";
    this.subject = "今日投资资讯有限公司";
    this.mailhost = "218.17.93.178";
    this.content = "";
    this.contents = new StringBuffer();
    replaceText = new String[2][];
    pass="customer";
    user="fatuid.crc";
  }
  //判断是否有附件上传
  public boolean isHasAttachment() {    if(vfile.size()==0 || vfile==null)
      return false;
    return true;
  }
  //邮件发送涵数
  public boolean sendEmail(){    Properties props = System.getProperties();
    //设置SMTP主机
    Authenticator auth = new MyAuthenticator(pass,user);
    props.put("mail.smtp.host",mailhost);
    props.put("mail.smtp.auth", "true");
    Session session = Session.getDefaultInstance(props,new Email_Autherticatorbean(user,pass));
    try{
      MimeMessage msg = new MimeMessage(session);
      msg.setFrom(new InternetAddress(from));
      InternetAddress[] address =  null;
      if(debug) System.out.print("to=="+to);
      address = InternetAddress.parse(to,false);
      msg.setRecipients(Message.RecipientType.TO,address);      if(debug) System.out.print("address=="+address);
      msg.setSubject(subject,"GB2312");
      //msg.setSubject(subject,"GBK");
      msg.setSentDate(new Date());
      //设置收信人
      //设置抄送人
      if(cc!=null){
        msg.setRecipients( Message.RecipientType.CC, InternetAddress.parse( cc ) );
      }      //设置暗送人
      if(bcc!=null){
        msg.setRecipients( Message.RecipientType.BCC, InternetAddress.parse( bcc ) );
      }      boolean isHasAttachment = isHasAttachment();      Multipart mp = new MimeMultipart();      MimeBodyPart mbp1 = new MimeBodyPart();      if(debug) System.out.println(this.content);
      mbp1.setContent(this.content,"text/html;charset=gb2312");
     // mbp1.setText(this.content,"gb2312");
      mp.addBodyPart(mbp1);
        if(debug) System.out.println("step1");
      if(isHasAttachment) {
        // 邮件内容的第二部分
        Enumeration efile = vfile.elements();
        while(efile.hasMoreElements()) {
          //先读出文件的内容
          String filename = efile.nextElement().toString();
          //加入附件
          MimeBodyPart mbp2 = new MimeBodyPart();
          java.io.File file = new java.io.File(filename);
          if(file.exists()) {
            //System.out.println("发送的文件名为:"+file);
            FileDataSource fds = new FileDataSource(file);
            mbp2.setDataHandler(new DataHandler(fds));
            mbp2.setFileName(fds.getName());
            mp.addBodyPart(mbp2);
          }
        }
        //清空文件
        vfile.removeAllElements();
        //把mp加进去
      }      if(debug) System.out.println("step2");
      //最后把内容和附件放在一起
      msg.setContent(mp);
      if(debug) System.out.println("step3");
      //发送邮件        Transport transport=session.getTransport("smtp");
        transport.connect("218.17.93.175","customer","fatuid.crc");
        transport.sendMessage(msg,msg.getAllRecipients());
        transport.close();      if(debug) System.out.println("step4");
      return true;
    }
    catch(Exception mex){
      mex.printStackTrace();
      return false;
    }  }  //设置属性
  public String getTo() {
    return to;
  }  public void setTo(String _to) {
    to = _to;
  }
  public String getFrom() {
    return from;
  }  public void setFrom(String _from) {
    from = _from;
  }   public String getCc() {
    return cc;
  }  public void setCc(String _cc) {
    cc = _cc;
  }
  public String getBcc() {
    return bcc;
  }  public void setBcc(String _bcc) {
    bcc = _bcc;
  }  public String getSubject() {
    return subject;
  }  public void setSubject(String _subject) {
    subject = _subject;
  }  public String getContent() {
    return content;
  }//设置内容
  public void setContent(String _content) {
      //content=(StringUtil.getGbcode(_content));
      content= _content;
  }  public  String getSmtpHost() {
    return mailhost;
  }  public void setSmtpHost(String _smtpHost) {
    mailhost = _smtpHost;
  }  //用来将文件中的内容读出来
  public void setFilecontent(String sFileName) throws Exception,IOException {
    System.out.println(sFileName);
    FileInputStream inFile =null;
    DataInputStream inStream  = null;
    String sLine = "" ;
    StringBuffer strBuffer  = new StringBuffer();
    try{
      inFile = new FileInputStream(sFileName);
      inStream =  new DataInputStream(inFile);
      while (inStream.available() !=0){
        sLine = StringUtil.getGbcode(inStream.readLine())+"\n" ;
       // sLine= inStream.readLine();
        strBuffer.append(sLine);
      }//while
      String sRet = strBuffer.toString() ;
      if(replaceText[0]!=null&&replaceText[0].length>0){
        for(int i = 0 ; i < replaceText[0].length ; i++){
          String oldStr = replaceText[0][i] ;
          String newStr = replaceText[1][i] ;
          sRet = StringUtil.replace(sRet,oldStr,newStr) ;
        }
      }
      strBuffer = new StringBuffer(sRet);
      contents = strBuffer ;
    }catch (IOException e){
      if(e instanceof FileNotFoundException)
        throw new Exception("Open File Error:" );
      else
        throw new Exception("Read File Error");    }catch(Exception e){
    }
    finally{
      inStream.close();
    inFile.close();
    }
  }  //用来将文件中的内容读出来
 public void setEmail(String t_content){
    String sRet = t_content;
    if(replaceText[0]!=null&&replaceText[0].length>0){
      for(int i = 0 ; i < replaceText[0].length ; i++){
        String oldStr = replaceText[0][i] ;
        String newStr = replaceText[1][i] ;
        sRet = StringUtil.replace(sRet,oldStr,newStr) ;
      }
    }
    content = sRet;
    //content = StringUtil.getLine(sRet);
  }  //提示:
  /*
  在jsp页面中先定义一个数组
  String[][] arrStr = new String[2][4];
  arrStr[0][n] 表示开始的值,即是要被arrStr[1][n]代替的值
  arrStr[0][0] = "<!--username-->";
  arrStr[0][1] = "<!--userid-->";
  arrStr[0][2] = "<!--password-->";
  arrStr[0][3] = "<!--sysdate-->";
  --------------------------------
  ---------------------------------
  arrStr[1][0] = username;
  arrStr[1][1] = userid ;
  arrStr[1][2] = password ;
  Date d = new Date();
  arrStr[1][3] = (d.getYear()+1900)+"-"+(d.getMonth()+1)+"-"+d.getDate() ;  */
  public void setReplaceText(String[][] new_Value){
    replaceText = new_Value ;
  }  public void setFile(String sFileName){    vfile.addElement(StringUtil.getGbcode(sFileName));  }}//end class

解决方案 »

  1.   

    给你找了一些:
    http://dev.csdn.net/develop/article/47/47589.shtm
    http://dev.csdn.net/develop/article/55/55318.shtm
    http://www.csdn.net/Develop/Read_Article.asp?Id=55732
    http://dev.csdn.net/develop/article/51/51659.shtm
      

  2.   

    呵呵,好多的....
    csdn搜一下一把
      

  3.   

    //发送附件//********************* SendAttch.java **************//import java.util.Properties;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;public class SendAttach {
      public static void main(String args[]) throws Exception {
        try{
          String host = "smtp.126.com"; 
          String from = "[email protected]";       
    String to = "[email protected]";       String filename = "E:\\table.htm"; //args[3];
          // Get system properties
          Properties props = System.getProperties();
          //认证
          PopupAuthenticator popAuthenticator = new PopupAuthenticator("user_name",
              "user_password");
          // Setup mail server
          props.put("mail.smtp.host", host);
          // Get session
          Session session = Session.getInstance(props, popAuthenticator);
          //use Authenticator
          props.put("mail.smtp.auth", "true");
          // Define message
          Message message = new MimeMessage(session);
          message.setFrom(new InternetAddress(from));
          message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
          message.setSubject("Hello JavaMail Attachment");
          // Create the message part
          BodyPart messageBodyPart = new MimeBodyPart();
          // Fill the message
          messageBodyPart.setText("Here's the file");
          // Create a Multipart
          Multipart multipart = new MimeMultipart();
          // Add part one //
          multipart.addBodyPart(messageBodyPart);      // Part two is attachment //
          // Create second body part
          messageBodyPart = new MimeBodyPart();
          // Get the attachment
          DataSource source = new FileDataSource(filename);
          // Set the data handler to the attachment
          messageBodyPart.setDataHandler(new DataHandler(source));
          // Set the filename
          messageBodyPart.setFileName(filename);
          // Add part two
          multipart.addBodyPart(messageBodyPart);
          // Put parts in message
          message.setContent(multipart);
          // Send the message 
          Transport.send(message);
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
      }
    }