我想你不是在服务器所在的机器上发的。
你的附件没有上传到服务器,本地机器有,但服务器机器没有这个文件。

解决方案 »

  1.   

    我当然不是在服务器上发的。我怎样才能把文件上传到服务器呢???
      

  2.   

    你这程序一半一半的,看不懂,另外附件不用上传到服务器上,在本机就可以
      

  3.   

    全部源程序如下:
    //sendMailBeanpackage tis.pub.javabean.SQL;import javax.servlet.jsp.*;
    import java.sql.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    import java.util.*;
    import javax.swing.*;
    import java.io.*;public class sendMailBean{
      private String choose1;               //附件1的路径和文件名
      private String choose2;    //附件2的路径和文件名
      private String choose3;    //附件3的路径和文件名
      private String choose4;    //附件4的路径和文件名
      private String username;
      private String password;
      private String mailHost;               //smtp邮件转发主机
      private Session session;               //javamail Session对象
      private String from;                   //发信人的邮件地址
      private String body;                   //邮件正文主体
      private String subject;                //邮件主题
      private String to;   
      private String cc ;         private String bcc;
      private String filePath="";            //附件文件的全路径
      private ArrayList file=new ArrayList();//附件列表,字符串形式
      private String errMsg;                 //出错消息
      
        public String getfilePath(){   
      return filePath;
      }  public void setfilePath(String filePath){
      this.filePath=filePath;
      }  public String getchoose1(){
      return choose1;
      }  public void setchoose1(String choose1){
      this.choose1=choose1;
      }  public String getchoose2(){
      return choose2;
      }  public void setchoose2(String choose2){
      this.choose2=choose2;
      }   public String getchoose3(){
      return choose3;
      }  public void setchoose3(String choose3){
      this.choose3=choose3;
      }   public String getchoose4(){
      return choose4;
      }  public void setchoose4(String choose4){
      this.choose4=choose4;
      }  public String getusername(){
      return username;
      }  public void setusername(String username){
      this.username=username;
      }  public String getpassword(){
      return password;
      }  public void setpassword(String password){
      this.password=password;
      }  public String getsubject(){
        return subject;
      }  public void setsubject(String subject){
        this.subject=subject;
      }  public String getmailHost(){
        return mailHost;
      }  public void setmailHost(String mailHost){
        this.mailHost=mailHost;
      }  public Session getsession(){
        return session;
      }  public void setsession(Session session){
        this.session=session;
      }  public String getfrom() {
        return from;
      }  public void setfrom(String from) {
        this.from = from;
      }  public String getto() {
        return to;
      }
      
      public void setto(String to){
       this.to =to;
      }
      
      public String getcc(){
      return cc;
      }
      
      public void setcc(String cc){
      this.cc=cc;
      } 
      
      public String getbcc() {
        return bcc;
      }  
      
      public void setbcc(String bcc){
       this.bcc=bcc;
      }  public String getbody(){
        return body;
      }  public void setbody(String body){
        this.body=body;
      }
      
      
      /**
       * 通常由程序在调用doSend之前调用
       * 也可以由doSend调用来进行验证
       */
      public boolean isComplete() {
        errMsg = "";
        if (from == null || from.length() == 0) {
          errMsg += "发件人地址不能为空";
          return false;
        }
        if (subject == null || subject.length() == 0) {
          errMsg += "主题不能为空";
          return false;
        }
        if (to==null || to.length()==0) {
          errMsg += "收件人不能为空";
          return false;
        }
        //内容可以为空但是主体不能为空
        if (body == null || body.length() == 0) {
          body = "";
        }
        if (mailHost == null || mailHost.length() == 0) {
          errMsg += "邮件服务器地址不能为空";
          return false;
        }
        return true;
      }
      /**
       * 将地址列表转换为ArrayList, 这样对"tom, mary@host, 123.456@host"这样的简单名字起作用
       */
      protected ArrayList tokenize(String s) {
        ArrayList al = new ArrayList();
        StringTokenizer tf = new StringTokenizer(s, ",");   
        while (tf.hasMoreTokens()) {      
          al.add(tf.nextToken().trim());
        }
        return al;
      }
      
      /**
       * 得到要发送的附件路径和文件名
       */
    private void setSlaveFile(){
        if(!(choose1==null || choose1.length()==0)){
           if(!(this.filePath==null || this.filePath.length()==0)){
                   this.filePath=this.filePath+","+choose1;
                  }
           else{
            this.filePath=choose1;
              }
           }
    if(!(choose2==null || choose2.length()==0)){
    if(!(this.filePath==null || this.filePath.length()==0)){
     this.filePath=this.filePath+","+choose1;
     }
    else{
    this.filePath=choose2;
     }

     }
    if(!(choose3==null || choose3.length()==0)){
     if(!(this.filePath==null || this.filePath.length()==0)){
    this.filePath=this.filePath+","+choose3;
     }
    else{
    this.filePath=choose3;
    }

     }
     if(!(choose4==null || choose4.length()==0)){
     if(!(this.filePath==null || this.filePath.length()==0)){
    this.filePath=this.filePath+","+choose4;
    }
    else{
    this.filePath=choose4;
    }

     }
      }  /**
       * 分解各个附件文件路径和文件名
       * @param filePath
       */
      public void setFile(String filePath){
        this.file=tokenize(filePath);
      }  /**
       * 邮件的发送
       * @throws MessagingException
       */
      public  String doSend() throws MessagingException {     
       this. setSlaveFile();
       this.setFile(this.filePath);   if (!isComplete()) {      
     return this.errMsg;
       }
       
       Properties props = new Properties();
       props.put("mail.smtp.host", mailHost);
       //创建Session对象
       if (session == null) {
         session = session.getDefaultInstance(props, null);
       }
       //创建邮件
       final Message msg = new MimeMessage(session);
       InternetAddress[] addresses;   //To地址列表
       if(to==null || to.length()==0);    
       else{
     ArrayList tolist=this.tokenize(to);
     addresses = new InternetAddress[tolist.size()];
     for (int i = 0; i < addresses.length; i++) {
     addresses[i] = new InternetAddress( (String) tolist.get(i));  
            }
        msg.setRecipients(Message.RecipientType.TO, addresses);
       }   //From地址
       msg.setFrom(new InternetAddress(from));
       
       //主题
       msg.setSubject(subject);
      //设置时间
       msg.setSentDate(new java.util.Date());
       
       
           //附件列表 
          
          String[] strFile=new String[file.size()];
          for(int i=0;i<strFile.length;i++){
    strFile[i]=new String((String)file.get(i));
            }
         try {
             Multipart mp=new MimeMultipart();
             MimeBodyPart[] fileBodyPart=new MimeBodyPart[strFile.length];
             FileDataSource fds[]=new FileDataSource[strFile.length];         for(int i=0;i<strFile.length;i++){  
               fds[i]=new FileDataSource(strFile[i]);
               fileBodyPart[i] = new MimeBodyPart();
               fileBodyPart[i].setDataHandler(new DataHandler(fds[i]));             
               fileBodyPart[i].setFileName(fds[i].getName());    
               mp.addBodyPart(fileBodyPart[i]);
             }       //this.body为纯文本格式(类似正文为纯文字格式并且不含附件时的部分)
            MimeBodyPart bodyPart = new MimeBodyPart();
            bodyPart.setContent(this.body, "text/plain"); 
            mp.addBodyPart(bodyPart);
           
            msg.setContent(mp);

          }
         catch (Exception e) {
           System.out.println( "doSend: translate false Exception info: "+
                    e.getMessage());
       }  
       
       Transport.send(msg);
       return "send success!!!";   
     }
    }