1

Some body please tell me about how to attach available pdf file to mail. I am use mail.jar and activation.jar for sending a mail. I can send mail but I dont know how to send pdf file with attachment. so please suggest me about that thank you.

I just try

           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);

But still it not get data, it empty attached.

4

3 回答 3

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

于 2013-08-06T08:48:52.653 回答
0

您应该创建MimeMultipart、和MimeBodyPart,如下所示:FileDataSourceDataHandler

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方法。

于 2013-08-06T08:41:19.430 回答
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

于 2013-08-06T08:37:44.083 回答