Android Question Send html email that include pictures

Discussion in 'Android Questions' started by le_toubib, Feb 24, 2015.

  1. le_toubib

    le_toubib Active Member Licensed User

    Hello there my friends
    I do enjoy learning from this forum everyday.

    I know how to send an html email, but How to send an html email that contains a local image (1.5mb) within the body ..
  2. Erel

    Erel Administrator Staff Member Licensed User

    This question was discussed in the past without a reliable / working solution.

    A simple solution is to send the image as an attachment.
    le_toubib and Peter Simpson like this.
  3. le_toubib

    le_toubib Active Member Licensed User

    Hi erel
    Thanks for replying
    I just found this code on stack
    That supposedly works fine.
    Could that help?
    Is it possible to convert it to b4a?

    you have need three lib  of java mail 


    public class AutomaticEmailActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) { 
    if (android.os.Build.VERSION.SDK_INT > 9) {
                    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

                final String fromEmail = ""; //requires valid gmail id
                final String password = "abc"; // correct password for gmail id
                final String toEmail = ""; // can be any email id 

                System.out.println("SSLEmail Start");
                Properties props = new Properties();
                props.put("", ""); //SMTP Host
                props.put("mail.smtp.socketFactory.port", "465"); //SSL Port
                        ""); //SSL Factory Class
                props.put("mail.smtp.auth", "true"); //Enabling SMTP Authentication
                props.put("mail.smtp.port", "465"); //SMTP Port

                Authenticator auth = new Authenticator() {
                    //override the getPasswordAuthentication method
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(fromEmail, password);

                final Session session = Session.getDefaultInstance(props, auth);
                Button send_email=(Button) findViewById(;
                send_email.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {

                        sendImageEmail(session, toEmail,"SSLEmail Testing Subject with Image", "SSLEmail Testing Body with Image");



            public static void sendImageEmail(Session session, String toEmail, String subject, String body){
                    MimeMessage msg = new MimeMessage(session);
                    msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
                    msg.addHeader("format", "flowed");
                    msg.addHeader("Content-Transfer-Encoding", "8bit");

                    msg.setFrom(new InternetAddress("", "NoReply-JD"));

                    msg.setReplyTo(InternetAddress.parse("", false));

                    msg.setSubject(subject, "UTF-8");

                    msg.setSentDate(new Date());

                    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));

                    MimeMultipart multipart = new MimeMultipart("related");

                    BodyPart messageBodyPart = new MimeBodyPart();
                    String htmlText = "<H1>Hello</H1><img src=\"cid:image\">";
                    messageBodyPart.setContent(htmlText, "text/html");
                    // add it

                    String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
                    String filename = base + "/photo.jpg";

                    messageBodyPart = new MimeBodyPart();
                    DataSource fds = new FileDataSource(filename);

                    messageBodyPart.setDataHandler(new DataHandler(fds));
                    messageBodyPart.setHeader("Content-ID", "<image>");

                    // Send message
                    System.out.println("EMail Sent Successfully with image!!");
                }catch (MessagingException e) {
                } catch (UnsupportedEncodingException e) {
  4. Erel

    Erel Administrator Staff Member Licensed User

    I wouldn't use this code in an application as it sends the message on the main thread.

    Check NetExtra library. I think that it allows you set the headers which is the part missing in SMTP object from the Net library.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice