0

I am using this code (given below) to send image through email from my android application, the email is received but it does not have the image. Please tell what is issue in my code?

Received Mail: email body

enter image description here

Code:

package com.example.appdeveloper.appname;

import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;

import java.io.File;
import java.util.Properties;


import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;

import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



public class EmailHandler extends AsyncTask<Void, Void, Boolean> {

    private static String to = "reciever@example.com";
    private static String from = "sender@example.com";
    private static String subject = "Subject";
    private static String sender = "Android App";
    private static String mail;
    private static String username = "sender";
    private static String password = "password";


    EmailHandler(Context context) {
        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/test.png";
        mail ="<!DOCTYPE html><html><body><img src="+path+"></body></html>";
    }


    @Override
    protected Boolean doInBackground(Void... nothing) {
        try {
            Authenticator auth = new EmailAutherticator();
            Properties properties = new Properties();
            properties.setProperty("mail.smtp.auth", "true");
            properties.setProperty("mail.smtp.starttls.enable", "true");
            properties.setProperty("mail.smtp.host", "smtp.gmail.com");
            properties.setProperty("mail.smtp.port", "587");
            properties.setProperty("mail.smtp.user", username);
            properties.setProperty("mail.smtp.password", password);
            Session session = Session.getDefaultInstance(properties,auth);

            MimeMessage message = new MimeMessage(session);
            message.setSubject(subject);
            message.setContent(mail, "text/html; charset=utf-8");
            Address address = new InternetAddress(from,sender);
            message.setFrom(address);

            InternetAddress ad[] = new InternetAddress[2];
            ad[0] = new InternetAddress(to);
            ad[1] = new InternetAddress(from);
            message.addRecipients( Message.RecipientType.TO, ad );
            Transport.send(message);

            return true;
        }
        catch(Exception exp) {
            exp.printStackTrace();
            return false;
        }
    }

}



class EmailAutherticator extends Authenticator {

    private String username = "sender";
    private String password = "password";

    public EmailAutherticator() {
        super();
    }

    public EmailAutherticator(String user,String pwd){
        super();
        username = user;
        password = pwd;
    }

    public PasswordAuthentication getPasswordAuthentication(){
        return new PasswordAuthentication(username,password);
    }

}
Renats Stozkovs
  • 2,549
  • 10
  • 22
  • 26
Salu
  • 53
  • 7
  • Your HTML is going to look something like this: ` ` If you copy that HTML to a file, and attempt to view it in your desktop Web browser, it will not work. The `src` attribute has no scheme, and so it will be interpreted relative to wherever you are displaying the HTML file from, and your desktop machine does not have an image at that path. – CommonsWare Feb 27 '17 at 16:47
  • @CommonsWare So what should I do to send image successfully? – Salu Feb 27 '17 at 16:53
  • Send the image as an attachment. Or, upload the image to a Web server, then use the `https` URL from the Web server in your HTML email message body. Simply referencing a filesystem path in an email body does not transfer the image. – CommonsWare Feb 27 '17 at 16:55
  • @CommonsWare I want to send email without user interaction. – Salu Feb 27 '17 at 18:18
  • There is nothing in either of my comments that requires user interaction. – CommonsWare Feb 27 '17 at 18:20

1 Answers1

0

So your Email comes across with body like this (kinda):

<!DOCTYPE html><html><body><img src="\myPhone\DCIM\image.png"></body></html>

Your email won't be able to get to that location unless you read email on your phone.

EDIT 1

You can attach extra data to your intent by using Intent.EXTRA_STREAM as outlined in Android documentation here

Renats Stozkovs
  • 2,549
  • 10
  • 22
  • 26