先把文件上传到服务器,然后在发邮件。
需要写一个上传文件部分

解决方案 »

  1.   

    发送的
    package mails;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.DataHandler;
    import javax.activation.FileDataSource;public class QuickMailAttach {
      public static void sendMessage(String smtpHost,String from,String to,String subject,String messageText,String fileName)
          throws MessagingException{
        //step 1:Configure the mail session
        System.out.println("Configuring mail session for:"+smtpHost);
        SmtpAuth sa=new SmtpAuth();
        sa.getuserinfo("??","??");
        sa.getPasswordAuthentication();
        java.util.Properties props=new java.util.Properties();
        props.put("mail.smtp.auth","true");
        props.put("mail.smtp.host",smtpHost);
        Session mailSession=Session.getDefaultInstance(props,sa);    //Step2:Construct the message
        System.out.println("Constructing message -from="+from+" to="+to);
        InternetAddress fromAddress=new InternetAddress(from);
        InternetAddress toAddress=new InternetAddress(to);
        MimeMessage testMessage=new MimeMessage(mailSession);
        testMessage.setFrom(fromAddress);
        testMessage.addRecipient(javax.mail.Message.RecipientType.TO,toAddress);
        testMessage.setSentDate(new java.util.Date());
        testMessage.setSubject(subject);    //Step3:create a body part to hold the "text" portion of the message
        System.out.println("Constructing 'text' body part");
        MimeBodyPart textBodyPart=new MimeBodyPart();
        textBodyPart.setText(messageText);    //Step 4:Create a body part to hold the "file" portion of the message
        System.out.println("Attaching 'file' body part:"+fileName);
        MimeBodyPart fileBodyPart=new MimeBodyPart();
        FileDataSource fds=new FileDataSource(fileName);
        fileBodyPart.setDataHandler(new DataHandler(fds));
        fileBodyPart.setFileName(fds.getName());
        System.out.println("Finished attaching file");    //Step 5:Create a  Multipart/container and add the parts
        Multipart container=new MimeMultipart();
        container.addBodyPart(textBodyPart);
        container.addBodyPart(fileBodyPart);    //Step 6:Add the Multipart to the actual messge
        testMessage.setContent(container);
        System.out.println("Messge constructed");    //Step 7:Now send the message
        Transport.send(testMessage);
        System.out.println("Message sent.");
      }  public static void main(String[] args){
        String smtpHost="smtp.citiz.net";
        String from="[email protected]";
        String to="[email protected]";
        String filename="D:/dd.txt";
        String subject="Test message";
        StringBuffer theMessage=new StringBuffer();
        theMessage.append("ddddhello22,\n\n");
        theMessage.append("Hope all is well on you end.\n");
        theMessage.append("Cheers");
        try{
          QuickMailAttach.sendMessage(smtpHost,from,to,subject,theMessage.toString(),filename);
        }catch(javax.mail.MessagingException exc){
          exc.printStackTrace();
        }
      }  static class SmtpAuth extends javax.mail.Authenticator {
       private String user,password;   public void getuserinfo(String getuser,String getpassword){
         user = getuser;
         password = getpassword;
       }
       protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
         return new javax.mail.PasswordAuthentication(user,password);
       }
      }
    }