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 8587853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:40:12+00:00 2026-06-11T22:40:12+00:00

I am trying to send an email with an image attachment from Java. I

  • 0

I am trying to send an email with an image attachment from Java. I am using the following piece of code:

String to = "jverstry@gmail.com";
String from = "ffff@ooop.com";

// Which server is sending the email?
String host = "localhost";

// Setting sending mail server
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);

// Providing email and password access to mail server
properties.setProperty("mail.user", "xxx");
properties.setProperty("mail.password", "yyy");

// Retrieving the mail session
Session session = Session.getDefaultInstance(properties);

// Create a default MimeMessage
MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(from));
message.addRecipient(
    Message.RecipientType.TO, new InternetAddress(to));

message.setSubject("This an email test !!!");

// Create a multipart message
Multipart mp = new MimeMultipart();

// Body text
BodyPart messageBP = new MimeBodyPart();
messageBP.setText("Some message body !!!");
mp.addBodyPart(messageBP);

// Attachment
BodyPart messageBP2 = new MimeBodyPart();

String image = "/MyImage.jpg";
InputStream stream = EmailWithAttachment.class
    .getResourceAsStream(image);
DataSource source = new ByteArrayDataSource(stream, "image/*");

messageBP2.setDataHandler(new DataHandler(source));
messageBP2.setHeader("Content-ID", "My Image");
mp.addBodyPart(messageBP2);

message.setContent(mp);

// Sending the message
Transport.send(message);

The email arrives in my mailbox, but when I open it, the attachment is not available. What could cause this issue? I have checked the .jar and it contains the image.

  • 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-06-11T22:40:13+00:00Added an answer on June 11, 2026 at 10:40 pm

    Ok, I got it. I should not pass the input stream, but a byte array and set a more precise MIME type. I modified my code as following and it works:

    DataSource source = new ByteArrayDataSource(
        IOUtils.toByteArray(is), "image/jpeg");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to send email using gmail from my mvc application. I am
I am trying to send email using c# following is my code. try {
I am trying to send email through gmail using PHPMailer_V5.1. Getting the following error,
I am trying to send email using Exchange 2007 from a console app using
Trying to get Play! Framework to send an email with an attachment. Code below
Im trying to send a HTML email and I have this piece of code:
I am trying to send Email using telnet and smtp.mail.yahoo.com server. when I try
i am trying to send email using jquery and php. my html code is
I'm trying to send email with some images attached in django. Code used is
I am trying to send email using AJAX and PHP but no success:( My

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.