Srinivas V
Srinivas V

Reputation: 32

How to send a file greater than 25 MB using Java Mail API

I am designing an application which sends Email with attachments using Gmail's smtp host. But when the file is larger than 25 MB, then I get an error saying that "552-5.2.3 Your message exceeded Google's message size limits. Please visit https://support.google.com/mail/?p=MaxSizeError to view our size guidelines. 188sm2692677pfg.11 -gsmtp"

    final String username = "[email protected]";
    final String password = "password";

    Properties prop = new Properties();
    prop.put("mail.smtp.host", "smtp.gmail.com");
    prop.put("mail.smtp.port", "587");
    prop.put("mail.smtp.auth", "true");
    prop.put("mail.smtp.starttls.enable", "true");

    Session session = Session.getInstance(prop,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(
                Message.RecipientType.TO,
                InternetAddress.parse("receiver address"));
        message.setSubject("This mail is a test mail");
     BodyPart messageBodyPart = new MimeBodyPart();  
     messageBodyPart.setText("Message");


     Multipart multipart = new MimeMultipart();


     multipart.addBodyPart(messageBodyPart);


     messageBodyPart = new MimeBodyPart();
     String filename = <Path>;
     DataSource source = new FileDataSource(filename);
     messageBodyPart.setDataHandler(new DataHandler(source));
     messageBodyPart.setFileName(filename);
     multipart.addBodyPart(messageBodyPart);

     message.setContent(multipart);

        Transport.send(message);


    } catch (MessagingException e) {
        JOptionPane.showMessageDialog(null, e.getMessage());
    }

Is there any way of sending files greater than gmail's limit of 25 MB? Can I change the file size upload limit from account settings or can I do something like any file will be uploaded as a drive link?

Upvotes: 1

Views: 3391

Answers (2)

Roger Gustavsson
Roger Gustavsson

Reputation: 1709

The user friendly way is probably to upload the file somewhere and add a link to the file in the mail.

It's also possible to split the file in several smaller parts and send each part in its own mail. The recipient then needs to join the files together again.

Zip-archivers can usually split large files into several zip-files that can then be join together again.

There's also raw splitting and joining. I haven't come across split commands built in to operating system distributions as standard. But your program could split the file in any way you desire.

Joining the files is then quite easy under Windows or Unix (Linux and others) operating systems. In Windows you go to the command prompt and use "copy": copy file1+file2+file3 finalfile In Unix you use "cat": cat file1 file2 file3 > finalfile

Upvotes: 2

luk2302
luk2302

Reputation: 57124

No. That is hard limit. Gmail itself states:

If your file is greater than 25 MB, Gmail automatically adds a Google Drive link in the email instead of including it as an attachment

That is what I would recommend for you as well, upload the file somewhere and paste a link in the mail. Options may be: Google Drive, Mega, Dropbox, S3, ...

Other than that there is nothing you can do.

Upvotes: 1

Related Questions