Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Image field Path

810582Aug 25 2015 — edited Aug 28 2015

Hi,

My jdev version is 12.1.3.

I've created a class file NewMail in viewcontroller view package.

During commit operation i'm initiating an e-mail from the view as per the code  below .

         DataSource fds = new FileDataSource(

            "Picture.png");

If i give the complete path for picture.png, then i'm able to get the mail with image.

ADF throws null pointer exception if i dont give the full path, how to generalise as per ADF incase if i decide to put this picture.png inside webcontent folder.

package view;

import java.io.File;

import java.io.InputStream;

import java.util.Properties;

import javax.activation.DataHandler;

import javax.activation.DataSource;

import javax.activation.FileDataSource;

import javax.mail.BodyPart;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

import javax.servlet.ServletRequest;

import javax.servlet.http.HttpServletRequest;

import oracle.jbo.domain.ClobDomain;

public class NewMail {

    public static void sendenhancement_mail(final String user ) {

       Runnable r = new Runnable() {

               public void run() {

                     

                          try{

      String to = "xxxxx@xxx.com";

      String from = "xxxxx@xxx.com";

      final String username = "xxxxx@xxx.com";

      final String password = "rrrr";

      Properties props = new Properties();

       props.put("mail.smtp.host", "rrrrr");

       props.put("mail.smtp.socketFactory.port", "333");

       props.put("mail.smtp.socketFactory.class",

                       "javax.net.ssl.SSLSocketFactory");

       props.put("mail.smtp.auth", "true");

       props.put("mail.smtp.port", "333");

      Session session = Session.getInstance(props,

         new javax.mail.Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {

               return new PasswordAuthentication(username, password);

            }

         });

      try {

         Message message = new MimeMessage(session);

         message.setFrom(new InternetAddress(from));

         message.setRecipients(Message.RecipientType.TO,

            InternetAddress.parse(to));

         message.setSubject("Hi");

         MimeMultipart multipart = new MimeMultipart("related");

         BodyPart messageBodyPart = new MimeBodyPart();

         String htmlText ="Hello"+"<img src=\"cid:image\">"+"<BR><B>Note: This is an automated E-mail.</B>";

         messageBodyPart.setContent(htmlText, "text/html");

         multipart.addBodyPart(messageBodyPart);

         messageBodyPart = new MimeBodyPart();

          System.out.println("Working Directory = " +

                        System.getProperty("user.dir"));

         DataSource fds = new FileDataSource(

            "Picture.png");

         messageBodyPart.setDataHandler(new DataHandler(fds));

         messageBodyPart.setHeader("Content-ID", "<image>");

         multipart.addBodyPart(messageBodyPart);

         message.setContent(multipart);

         Transport.send(message);

         System.out.println("Sent message successfully....");

      } catch (MessagingException e) {

         throw new RuntimeException(e);

      }

       }

       catch (Exception e) {

                                          System.out.println("Exception occured "+e);

      

                                      }

      

       }

       };

      

       Thread t = new Thread(r);

       t.start();

   }

}

This post has been answered by kdario on Aug 28 2015
Jump to Answer

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Sep 25 2015
Added on Aug 25 2015
5 comments
742 views