Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:43:05+00:00 2026-05-29T17:43:05+00:00

Hi I want to send attachment with mail using javaMailAPI. my code is: public

  • 0

Hi I want to send attachment with mail using javaMailAPI. my code is:

public class GmailSender {

    private static String HOST = "smtp.gmail.com";
    private static String USER = "######";
    private static String PASSWORD = "######";
    private static String PORT = "465";
    private static String FROM = "######";
    private static String TO = "######";

    private static String STARTTLS = "true";
    private static String AUTH = "true";
    private static String DEBUG = "true";
    private static String SOCKET_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private static String SUBJECT = "Testing JavaMail API";
    private static String TEXT = "This is a test message from my java application. Just ignore it";

    public static void main(String[] args){
        GmailSender.send();
    } 


    public static void makeAttachment(MimeMessage message){

        try{

            Multipart mp = new MimeMultipart();

            //start Code for Attachment

            String fileName = "c:/tmp/f.txt";
            File f = new File(fileName);


            //read file and make byte array
            FileInputStream fi = new FileInputStream(f);
            byte[] attachmentData = new byte[8000000];

            // Create the message part 
            BodyPart messageBodyPart = new MimeBodyPart();
            // Fill the message
            messageBodyPart.setText("hi");

            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);

            // Part two is attachment
            messageBodyPart = new MimeBodyPart();
            String filename = "c:/tmp/f.txt";
            DataSource source = new FileDataSource(filename);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(filename);
            multipart.addBodyPart(messageBodyPart);

            // Put parts in message
            message.setContent(multipart);



        }catch(Exception e){
            System.out.println(">>Exception "+e);
        }       

    }
    public static synchronized void send() {
        //Use Properties object to set environment properties
        Properties props = new Properties();

        props.put("mail.smtp.host", HOST);
        props.put("mail.smtp.port", PORT);
        props.put("mail.smtp.user", USER);

        props.put("mail.smtp.auth", AUTH);
        props.put("mail.smtp.starttls.enable", STARTTLS);
        props.put("mail.smtp.debug", DEBUG);

        props.put("mail.smtp.socketFactory.port", PORT);
        props.put("mail.smtp.socketFactory.class", SOCKET_FACTORY);
        props.put("mail.smtp.socketFactory.fallback", "false");

        try {

            //Obtain the default mail session
            Session session = Session.getDefaultInstance(props, null);
            session.setDebug(true);

            //Construct the mail message
            MimeMessage message = new MimeMessage(session);
            message.setText(TEXT);
            message.setSubject(SUBJECT);
            message.setFrom(new InternetAddress(FROM));
            message.addRecipient(RecipientType.TO, new InternetAddress(TO));

            makeAttachment(message);

            message.saveChanges();

            //Use Transport to deliver the message
            Transport transport = session.getTransport("smtp");
            transport.connect(HOST, USER, PASSWORD);          

            transport.sendMessage(message, message.getAllRecipients());

            transport.close();

        } catch (Exception e) {
                System.out.println(">>GmailSender "+e);

        }
    }    
}

I am getting this exception:

    javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 
    boundary="----=_Part_1_21944831.1329056547036"
    at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:877)
    at javax.activation.DataHandler.writeTo(DataHandler.java:302)
    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
    at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:636)
    at GmailSender.send(GmailSender.java:117)
    at GmailSender.main(GmailSender.java:34)
>>GmailSender javax.mail.MessagingException: IOException while sending message;
  nested exception is:
    javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 
    boundary="----=_Part_1_21944831.1329056547036"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:43:17+00:00Added an answer on May 29, 2026 at 5:43 pm

    Your code sample works for me when I run it, which points to a difference in environment. I googled and found this post, which I believe answers your question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using System.Net.Mail to send an email message and want to make sure it
I am trying to create a PHP function to send an e-mail using SMTP
I need to send mail using Gmail SMTP, but it's showing Failure sending mail.
I want to send booking information through mail in an attachment to add in
I am using this function to send mails via gmail. private bool uploadToGmail(string username,
I want to send an encrypted and signed mail without using any third-party API.
I want to record a .caf file then send it as a mail attachment
For testing purposes I want send mail to my localhost user account rather than
I want to send email with an image attachment but the image must not
I use this class to send mails trough a gmail account: public class GmailAccount

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.