Forum Stats

  • 3,855,337 Users
  • 2,264,499 Discussions
  • 7,905,972 Comments

Discussions

Image field Path

810582
810582 Member Posts: 63
edited Aug 28, 2015 7:48AM in JDeveloper and ADF

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 = "[email protected]";


      String from = "[email protected]";
      final String username = "[email protected]";
      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(); 
   }
}
Tagged:
810582

Best Answer

  • kdario
    kdario Member Posts: 3,533 Silver Crown
    edited Aug 28, 2015 5:30AM Answer ✓

    If that files are part of your web app, then you can retrieve them with something like:

    InputStream is = this.getClass().getClassLoader().getResourceAsStream("/RELATIVE-PATH/yourFile");

    or

    FacesContext fc = FacesContext.getCurrentInstance();

    ServletContext sc= (ServletContext)fc.getExternalContext().getContext();

    String path = sc.getRealPath("/RELATIVE-PATH/yourFile")

    (you need to enable "Enable getRealPath() results for archived web apps" )

    RELATIVE-PATH is relative to context root(something like "/images")

    Dario

    810582

Answers

This discussion has been closed.