如何在java中附加生成的pdf文件邮件

时间:2013-08-06 08:35:24

标签: java servlets

请告诉我如何将pdf file附加到mail。 我使用mail.jaractivation.jar发送mail。 我可以发送mail,但我不知道如何发送pdf file附件。 所以请建议我 谢谢。

我试试

           String filename = "file.pdf";
            Multipart multipart1 = new MimeMultipart();
            BodyPart messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(filename);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(filename);
            multipart1.addBodyPart(messageBodyPart);

但是它仍然没有得到数据,它是空的。

3 个答案:

答案 0 :(得分:4)

使用JavaMail发送带附件的电子邮件:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;  

class SendAttachment
{ 
    public static void main(String [] args)
    {    
        String to="ABC@gmail.com";//change accordingly   
        final String user="ABC@XYZ.com";//change accordingly   
        final String password="xxxxx";//change accordingly     

        //1) get the session object      
        Properties properties = System.getProperties();  
        properties.setProperty("mail.smtp.host", "mail.javatpoint.com");   
        properties.put("mail.smtp.auth", "true");    

        Session session = Session.getDefaultInstance(properties,   
                new javax.mail.Authenticator() {   
            protected PasswordAuthentication getPasswordAuthentication() {   
                return new PasswordAuthentication(user,password);    }   });       

        //2) compose message      
        try{    
            MimeMessage message = new MimeMessage(session);    
            message.setFrom(new InternetAddress(user));     
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));    
            message.setSubject("Message Aleart");         

            //3) create MimeBodyPart object and set your message text        
            BodyPart messageBodyPart1 = new MimeBodyPart();     
            messageBodyPart1.setText("This is message body");          

            //4) create new MimeBodyPart object and set DataHandler object to this object        
            MimeBodyPart messageBodyPart2 = new MimeBodyPart();      
            String filename = "SendAttachment.java";//change accordingly     
            DataSource source = new FileDataSource(filename);    
            messageBodyPart2.setDataHandler(new DataHandler(source));    
            messageBodyPart2.setFileName(filename);             

            //5) create Multipart object and add MimeBodyPart objects to this object        
            Multipart multipart = new MimeMultipart();    
            multipart.addBodyPart(messageBodyPart1);     
            multipart.addBodyPart(messageBodyPart2);      

            //6) set the multiplart object to the message object    
            message.setContent(multipart );        

            //7) send message    
            Transport.send(message);      
            System.out.println("message sent....");   

        }catch (MessagingException ex) {ex.printStackTrace();}  
    }
} 

来源:http://www.javatpoint.com/example-of-sending-attachment-with-email-using-java-mail-api

答案 1 :(得分:0)

试试这个:

        String SMTP_HOST_NAME = "mail.domain.com";  
        String SMTP_PORT = "111";   

        String SMTP_FROM_ADDRESS="xxx@domain.com";  
        String SMTP_TO_ADDRESS="yyy@domain.com";  
        String subject="Textmsg";  
        String fileAttachment = "C:\\filename.pdf";  

          Properties props = new Properties();  

          props.put("mail.smtp.host", SMTP_HOST_NAME);  
          props.put("mail.smtp.auth", "true");  
          props.put("mail.debug", "true");  
          props.put("mail.smtp.port", SMTP_PORT );  
          Session session = Session.getInstance(props,new javax.mail.Authenticator()  
            {protected javax.mail.PasswordAuthentication   
                getPasswordAuthentication()   
            {return new javax.mail.PasswordAuthentication("xxxx@domain.com","password");}});  
          try{  

          Message msg = new MimeMessage(session);  

          msg.setFrom(new InternetAddress(SMTP_FROM_ADDRESS));  
       // create the message part   
          MimeBodyPart messageBodyPart =   
            new MimeBodyPart();  
      //fill message  
          messageBodyPart.setText("Test mail one");  
          Multipart multipart = new MimeMultipart();  
          multipart.addBodyPart(messageBodyPart);  
       // Part two is attachment  
          messageBodyPart = new MimeBodyPart();  
          DataSource source =   
            new FileDataSource(fileAttachment);  
          messageBodyPart.setDataHandler(  
            new DataHandler(source));  
          messageBodyPart.setFileName(fileAttachment);  
          multipart.addBodyPart(messageBodyPart);  
      // Put parts in message  
          msg.setContent(multipart);  


          msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(SMTP_TO_ADDRESS));  

          msg.setSubject(subject);  
         // msg.setContent(content, "text/plain");  

          Transport.send(msg);  
          System.out.println("success....................................");  
          }  
          catch(Exception e){  
              e.printStackTrace();        
          }

来源:http://www.coderanch.com/t/586537/java/java/sample-code-java-mail-api

答案 2 :(得分:0)

您应该创建MimeMultipartMimeBodyPartFileDataSourceDataHandler,如下所示:

Multipart multipart = new MimeMultipart();
messageBodyPart = new MimeBodyPart();
String filePath = "<file system path>";
String fileName = "display name"

DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);

现在,在MimeMessage上使用setContent方法。