Android sent mail with image

To send an email with an image attachment from an Android app, you can use the JavaMail API or a third-party library like SendGrid or Firebase Cloud Messaging. Here's an example of how to do it using JavaMail API:

Step 1: Add the JavaMail API to your project

Add the following dependencies to your Android project's build.gradle file:

dependencies {
    implementation 'com.sun.mail:android-mail:1.6.2'
    implementation 'com.sun.mail:android-transport:1.6.2'
}

Step 2: Create a JavaMail session

Create a JavaMail session object:

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");
    }
});

Step 3: Create a MimeMessage object

Create a MimeMessage object to represent the email:

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("your-email"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient-email"));
message.setSubject("Email with image attachment");

Step 4: Add the image attachment

Add the image attachment to the email:

File file = new File("/path/to/image.jpg");
DataSource source = new FileDataSource(file);
message.setDataHandler(new DataHandler(source));
message.setFileName(file.getName());

Step 5: Send the email

Send the email using the JavaMail API:

Transport.send(message);

Here's the complete code:

import android.os.Bundle;
import android.util.Log;

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

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create a JavaMail session
        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");
            }
        });

        // Create a MimeMessage object
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("your-email"));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient-email"));
        message.setSubject("Email with image attachment");

        // Add the image attachment
        File file = new File("/path/to/image.jpg");
        DataSource source = new FileDataSource(file);
        message.setDataHandler(new DataHandler(source));
        message.setFileName(file.getName());

        // Send the email
        Transport.send(message);

        Log.d(TAG, "Email sent successfully!");
    }
}

Make sure to replace the placeholders (your-smtp-host, your-smtp-port, your-email, your-password, and recipient-email) with your actual email settings and recipient's email address.

Also, note that this is just a basic example, and you may need to add additional error handling and configuration depending on your specific use case.