Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Friday, December 31, 2021

Read the content of a PDF file using Java API

import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.pdmodel.PDDocument; 
PDdocument pdDocument = PDdocument.load(new File("your filepath) )); 
PDFTextStripper stripper = new PDFTextStripper () ; 
String fileContent = stripper.getText(pdDocument) ; 
System.out.println(text) ;

Tuesday, February 2, 2021

Java - Send email using java program ( javax.mail API)

Java Mail API


public static  sendMail() {  

//sending email using java code using javax.mail api

        Properties props = new Properties();

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

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

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

        final String user = "EmailUsername";

        final String password = "Password";

        String to = "EmailToAddress";

        String from = "EmailFromAddress";

        Session session = Session.getInstance(props,

                new javax.mail.Authenticator() {

@Override

                    protected PasswordAuthentication getPasswordAuthentication() {

                        return new PasswordAuthentication(user, password);

                    }

                });

        try {

            MimeMessage message = new MimeMessage(session);

            message.setFrom(new InternetAddress(user));

            message.addRecipient(Message.RecipientType.TO,to);


            message.setSubject("Mail Subject any text");

            MimeBodyPart messageBodyPart1 = new MimeBodyPart();

            messageBodyPart1.setContent("mail content in html fromat or text", "text/html");

MimeBodyPart messageBodyPart2 = new MimeBodyPart();

            DataSource source1= new FileDataSource(new File("attachment path if required"));

            messageBodyPart2.setDataHandler(new DataHandler(source1));

            messageBodyPart2.setFileName(doctitle);    

            Multipart multipart = new MimeMultipart();

            multipart.addBodyPart(messageBodyPart2);

            multipart.addBodyPart(messageBodyPart1); 

            message.setContent(multipart);

            Transport.send(message);

        } catch (MessagingException ex) {

            ex.printStackTrace();

        }     

 }