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 ..
    Thx
     
  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?


    Code:
    you have need three lib  of java mail 

        
    1.activation.jar
        
    2.additionnal.jar
        
    3.mail.jar




        
    public class AutomaticEmailActivity extends Activity {

            @Override 
            
    public void onCreate(Bundle savedInstanceState) { 
                super.onCreate(savedInstanceState); 
                setContentView(R.layout.main); 
                
    if (android.os.Build.VERSION.SDK_INT > 9) {
                    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                    StrictMode.setThreadPolicy(policy);
                
    }



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

                System.out.println("SSLEmail Start");
                Properties props = new Properties();
                props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
                props.put("mail.smtp.socketFactory.port", "465"); //SSL Port
                props.put("mail.smtp.socketFactory.class",
                        "javax.net.ssl.SSLSocketFactory"); //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(R.id.send_email);
                send_email.setOnClickListener(new OnClickListener() {

                    @Override
                    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){
                try{
                    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("no_reply@journaldev.com", "NoReply-JD"));

                    msg.setReplyTo(InternetAddress.parse("no_reply@journaldev.com", 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
                    multipart.addBodyPart(messageBodyPart);


                    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>");
                    multipart.addBodyPart(messageBodyPart);  
                    msg.setContent(multipart);


                    // Send message
                    Transport.send(msg);
                    System.out.println("EMail Sent Successfully with image!!");
                }catch (MessagingException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
     
  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.
     
Loading...
  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