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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:52:09+00:00 2026-05-23T23:52:09+00:00

i have a java code in that i must send a attachment , it

  • 0

i have a java code in that i must send a attachment, it may be .doc,.db or .file. So i use the following code, the message was delivered successfully and particular attachment file was not send and also received.

My Code is:

import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendEmail {

    private String from = "example1@gmail.com";
    private String to;
    private String subject;
    private String text;
    String filename = "hardWare_Dtls.file";
    String host = "smtp.gmail.com";

    public SendEmail(String from, String to, String subject, String text,String filename) {
        // System.out.println("From Adress inside constr"+from);
        this.from = from;
        this.to = to;
        this.subject = subject;
        this.text = text;
        this.filename=filename;
    }

    public void send() {

        Properties props = System.getProperties();
        System.out.println("Email Options SendEmail");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", "123456");
        props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo
                                            // mail
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        Session mailSession = Session.getDefaultInstance(props, null);
        Message simpleMessage = new MimeMessage(mailSession);

        InternetAddress fromAddress = null;
        InternetAddress toAddress = null;
        try {
            fromAddress = new InternetAddress(from);
            toAddress = new InternetAddress(to);

        } catch (AddressException e) {
            // TODO Auto-generated catch block
            // e.printStackTrace();
            System.out.println("Address Exception" + e.getMessage());
        }

        try {
            System.out.println("From Address" + fromAddress);
            simpleMessage.setFrom(fromAddress);
            simpleMessage.setRecipient(RecipientType.TO, toAddress);
            System.out.println("To Address" + toAddress);
            simpleMessage.setSubject(subject);
            simpleMessage.setText(text);
            simpleMessage.setSentDate(new Date());

             MimeBodyPart attachmentPart = new MimeBodyPart();
                FileDataSource fileDataSource = new FileDataSource(filename) {
                    @Override
                    public String getContentType() {
                        return "application/octet-stream";
                    }
                };
                attachmentPart.setDataHandler(new DataHandler(fileDataSource));
                attachmentPart.setFileName(filename);

                Multipart multipart = new MimeMultipart();
              //  multipart.addBodyPart(messagePart);
                multipart.addBodyPart(attachmentPart);


            // simpleMessage.setText(attachment);

            Transport transport = mailSession.getTransport("smtps");
            transport.connect("smtp.gmail.com", "example1@gmail.com",
                    "123456");
            simpleMessage.saveChanges();
            transport.sendMessage(simpleMessage,
                    simpleMessage.getAllRecipients());
             Transport.send(simpleMessage);

            transport.close();
            // Transport.send(simpleMessage);
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            // e.printStackTrace();
            System.out.println("Messagine Exception" + e.getMessage());
        }
    }

}

in this code what mistake i had. If any other code for sending a message with attachment using Javacode, kindly send me. thanks in Advance.

I use this code in my Main Class:

            String from = "example@gmail.com";
        String to = "example@yahoo.com";
        String subject = "Sample Text Message";
        String message = "Sample Msg with File attachment";
        String filename="hardWare_Dtls.file";

        SendEmail sendMail = new SendEmail(from, to, subject, message,filename);
        sendMail.send();
  • 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-23T23:52:10+00:00Added an answer on May 23, 2026 at 11:52 pm

    You have pretty much all the pieces but you should use a MimeMessage (instead of just Message), and set its content to be your Multipart. I think you actually tried to do that but your code wouldn’t compile so you commented out the line:

     // simpleMessage.setText(attachment);
    

    Instead of that do this:

    ((MimeMessage) simpleMessage).setContent(multipart);
    

    And test again. It should work.

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

Sidebar

Related Questions

In my Java code I have function that gets file from the client in
I have a method that contains the following (Java) code: doSomeThings(); doSomeOtherThings(); doSomeThings() creates
I have heard that Java must use a JIT to be fast. This makes
I have some Java code that uses curly braces in two ways // Curly
I have some Java code that is throwing out of memory exceptions after running
I have some Java code that connects to an Oracle database using DriverManager.getConnection(). It
I have a java script code that works fine when run through a browser,
I have existing java code and need to create Design Document based on that.
I have a standalone Java app that has some licensing code that I want
Here is the piece of code that I have used for Java 5.0 TreeSet<Integer>

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.