Attach file from internal storage android java mail api

To attach a file from internal storage using the Java Mail API on Android, you can follow these steps:

  1. First, you need to get the file path from the internal storage. You can use the getFilesDir() method of the Context class to get the path of the internal storage.
File file = new File(context.getFilesDir(), "your_file_name");
  1. Then, you need to create a DataSource object from the file path. This object will be used to attach the file to the email.
DataSource source = new FileDataSource(file.getAbsolutePath());
  1. Next, you need to create a MimeBodyPart object and set the DataSource object as its content.
MimeBodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(new DataHandler(source));
  1. Finally, you can add the MimeBodyPart object to the email message using the Multipart class.
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(attachment);

Here is the complete code:

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.io.IOException;
import java.util.Properties;

public class SendEmail {
    private static final String TAG = "SendEmail";

    public void sendEmail(Context context, String recipient, String subject, String body, String filePath) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "your_smtp_host");
        props.put("mail.smtp.port", "your_smtp_port");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("your_email", "your_password");
            }
        });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("your_email"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
            message.setSubject(subject);
            message.setText(body);

            File file = new File(context.getFilesDir(), filePath);
            DataSource source = new FileDataSource(file.getAbsolutePath());
            MimeBodyPart attachment = new MimeBodyPart();
            attachment.setDataHandler(new DataHandler(source));
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(attachment);
            message.setContent(multipart);

            Transport.send(message);
            Log.d(TAG, "Email sent successfully");
        } catch (MessagingException e) {
            Log.e(TAG, "Error sending email", e);
        }
    }
}

In this code, replace "your_smtp_host", "your_smtp_port", "your_email", and "your_password" with your actual SMTP host, port, email, and password. Also, replace "your_file_name" with the name of the file you want to attach.

Note that this code assumes that the file is located in the internal storage of the app. If the file is located in the external storage, you need to use a different method to get the file path.