简体   繁体   中英

Send mail function in java doesnt send .zip as the attachment

This send mail function is working for all types of files like .html,.txt as attachment. But when i try to send a .zip folder as attachment , it terminates.

public void sendmail(){
       System.out.println("started function");
        String from = "amritharajeevan.77@gmail.com";
            // Sender's email ID needs to be mentioned
            String to = "amritha2.rajeevan@aricent.com";

            final String username = "amritharajeevan.77";//change accordingly
            final String password = "vivekrajeevan";//change accordingly

            // Assuming you are sending email through gmail.com
            String host = "smtp.gmail.com";

            Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", "25");
            Session session = Session.getInstance(props,
               new javax.mail.Authenticator() {
                  protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                     return new javax.mail.PasswordAuthentication(username, password);
                  }
               });

            try {
               Message message = new MimeMessage(session);
               message.setFrom(new InternetAddress(from));
               message.setRecipients(Message.RecipientType.TO,
                  InternetAddress.parse(to));
               message.setSubject("Server Error");
               BodyPart messageBodyPart = new MimeBodyPart();
               messageBodyPart.setText("Server"+" " +servererror+ " " +" is unreachable. Please check the logs.");
               Multipart multipart = new MimeMultipart();
               multipart.addBodyPart(messageBodyPart);
               messageBodyPart = new MimeBodyPart();
               String filename = "/Users/aricent/Desktop/amritha/seetest/May6.zip";
               DataSource source = new FileDataSource(filename);
               messageBodyPart.setDataHandler(new DataHandler(source));
               messageBodyPart.setFileName(filename);
               multipart.addBodyPart(messageBodyPart);
               message.setContent(multipart);
               System.out.println("sent start function");
               Transport.send(message);

               System.out.println("Sent message successfully....");

            } catch (MessagingException e) {
               throw new RuntimeException(e);
            }
         }

So i dont know what to do? please tell me what part of my code should i change. I tried for .txt and .html files. It works fine.

Checkout the Apache Commons Email project, which will clean up a lot of this code and make attachments easy.

    // Create the attachment
    EmailAttachment attachment = new EmailAttachment();
    attachment.setPath(zipFile);
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription("Zip File");
    attachment.setName("myfiles.zip");

    MultiPartEmail email = new MultiPartEmail();
    email.attach(attachment);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM