Send a file as an attachment in java Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Send a file as an attachment in java without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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 = "";
    private String to;
    private String subject;
    private String text;
    String filename = "hardWare_Dtls.file";
    String host = "";

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

    public void send() {

        Properties props = System.getProperties();
        System.out.println("Email Options SendEmail");
        props.put("", 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.setRecipient(RecipientType.TO, toAddress);
            System.out.println("To Address" + toAddress);
            simpleMessage.setSentDate(new Date());

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

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

            // simpleMessage.setText(attachment);

            Transport transport = mailSession.getTransport("smtps");
            transport.connect("", "",

            // 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 = "";
        String to = "";
        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);


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.

We are here to answer your question about Send a file as an attachment in java - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji