Monday, May 9, 2022

Send mail using Java Mail

                  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.Multipart;

        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;


              public static void main(String[] args) {

      String to = "to mail address";

      String from = "from mail address";

      String host = "mail host";

      Properties properties = System.getProperties();

      properties.setProperty("mail.smtp.host"host);

      Session session = Session.getDefaultInstance(properties);


      try {

         MimeMessage message = new MimeMessage(session);

         message.setFrom(new InternetAddress(from));

         message.addRecipient(Message.RecipientType.TOnew                                 InternetAddress(to));

         message.setSubject("This is the Subject Line!");

         

         BodyPart messageBodyPart = new MimeBodyPart();

         

         messageBodyPart.setText("This is message body");

         

         Multipart multipart = new MimeMultipart();

         

         multipart.addBodyPart(messageBodyPart);

         

         messageBodyPart = new MimeBodyPart();

         String filename = "filePath";

         DataSource source = new FileDataSource(filename);

         messageBodyPart.setDataHandler(new DataHandler(source));

         messageBodyPart.setFileName(filename);

         multipart.addBodyPart(messageBodyPart);

         

         message.setContent(multipart);

         

         Transport.send(message);

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

      catch (MessagingException mex) {

         mex.printStackTrace();

      }

   }

Swap two variables in Java

 

  • With temp variable
                public void swap(int a,int b)
                    {
                        int temp=a;
                        a=b;
                        b=temp;
                    }


  • Without temp variable
                public void swap(int a,int b)
                    {
                        a=a+b;
                        b=a-b;
                        a=a-b;
                    }

Switch case in Java

  Problem statement: Return the capital of a state based on input state


        public String getCapital(String state){

            switch(state)

            {

                case "Tamilnadu":

                            return "Chennai";

                 case "Karnataka":

                            return "Bangalore";

                case "Maharastra":

                            return "Mumbai";

                case "Kerala":

                            return "Trivandrum";

                }

                return null;

          }


Input: Tamilnadu

Output: Chennai


Wednesday, August 31, 2016

Convert Blob to string

public String BlobToString(Blob input) throws Exception{
            byte[] bdata = input.getBytes(1, (int)input.length());
        String data1 = new String(bdata);
        return data1;
    }

How to get big size clob content from table

select column1,DBMS_LOB.SUBSTR(clobColumn,4000,1) as clobColumn 
               from table

Convert Sql clob to string in java

    String query = "";
            ResultSet output = stmt.executeQuery(query);
            String clobStr ="";
            while(output.next()){
                StringBuffer strOut = new StringBuffer();
                String aux;
                try {
                    BufferedReader br = new BufferedReader(output.getClob("").
                            getCharacterStream());
                    while ((aux=br.readLine())!=null) {
                        strOut.append(aux);
                        strOut.append(System.getProperty("line.separator"));
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }
                clobStr = strOut.toString().replace(" ", "").trim();
            }

Thursday, May 28, 2015

How to refresh or reload the web pages in webdriver

Using refresh() method:

In Java,

driver.navigate().refresh();
 
Using sendkeys() method:

driver.findElement(By.id(locator)).sendKeys(F5 key);

In Java,
driver.findElement(By.id("gbqfq")).sendKeys(Keys.F5);


Using navigate().to() method:
In Java,
driver.navigate().to(driver.getCurrentUrl()); 


Using get() method:
If we know the URL then we can load the same URL again to reload the page. In Java,
driver.get("https://www.google.com.bd/");

We can also use getCurrentUrl() function to know the current URL of the page. The command would be,
driver.get(driver.getCurrentUrl());


Using sendkeys() method with ASCII code:
We can use ASCII code as argument on sendkeys() method that is equivalent to F5 key command of the keyboard.
driver.findElement(By.id("gbqfq")).sendKeys("\uE035");

Here \uE035 is the ASCII code of F5 key.

Using executeScript() method:
Using this command we can execute any JavaScript for our need. If we execute location.reload() JavaScript function then current page will be reloaded which meets our purpose. 

The command is,
driver.executeScript("location.reload()");