Skip to Main Content

Java Programming

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!

Java - Add Email Subject and Body

frank.anelliaDec 6 2017 — edited Dec 6 2017

Hello,

I have this program I've been working on that monitors the size of Linux mounts on servers.  Here is the code:

import java.io.IOException;

import java.nio.file.FileStore;

import java.nio.file.FileSystems;

import java.util.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;

public class LinuxMounts {

    public static void main(String[] args) throws IOException {

       

        String mailFrom = "java@email";

        String mailTo = "<recipients@email>";

        String mailSubject = "Testing from Java";

        String mailBody = "You passed!";

        String mailHost = "localhost";

        printHeading();

        for (FileStore store : FileSystems.getDefault().getFileStores()) {

            String strPath = store.toString();

            if (strPath.matches("^/(u01|backups).*$")){

                long total = store.getTotalSpace() / 1024 / 1024 / 1024;

                long used = (store.getTotalSpace() - store.getUnallocatedSpace()) / 1024 / 1024 / 1024;

                long avail = store.getUsableSpace() / 1024 / 1024 / 1024;

                float percentage = ((used * 100) / total);

                System.out.format("%-50s %20d %20d %20d %20.2f %n", store, total, used, avail, percentage);

            }

        }

       

        sendEmail(mailFrom, mailTo, mailSubject, mailBody, mailHost);

    }

   

    static void printHeading(){

       

        String heading1 = "Filesystem";

        String heading2 = "Total Size (MB)";

        String heading3 = "Used Space (MB)";

        String heading4 = "Available Space (MB)";

        String heading5 = "Percentage Used (%)";

        System.out.format("%-50s %20s %20s %20s %20s %n", heading1, heading2, heading3, heading4, heading5);

    }

   

    public static void sendEmail(String fromEmail, String toEmail, String subject, String body,String mailHost){

        Properties properties = System.getProperties();

        //Setup mail server

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

        Session session = Session.getDefaultInstance(properties);

        try {

            MimeMessage message = new MimeMessage(session);

            message.setFrom(new InternetAddress(fromEmail));

            message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));

            message.setSubject(subject);

            message.setText(body);

            // Send message

            Transport.send(message);

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

        } catch (MessagingException mex) {

                mex.printStackTrace();

          }

    }

}

The program works.  I would like to send the output of printHeading() and the mount values in the body of the email.  Eventually, I would like to add the server name and some other details to the 'Subject' line.

Any ideas how I can pass those values to the Body and Subject of the email?

Thanks,

Frank

This post has been answered by mNem on Dec 6 2017
Jump to Answer

Comments

Billy Verreynne

SQLCL is a client application. It does not have users and passwords to use.
SQLCL connects to database servers (db instances). The database typically requires a username and password for authentication. SQLCL may prompt for these credentials to supply to the database.
There are no default usernames and passwords to use in today's Oracle database. Most schemas are also locked by default when the database is created.
Ask your DBA for the details of the database and user schema to use.

User_21BG6

i have installed sqlcl-21.3.3.322.1724..but it is asked user name and password in command prompt..please give the solution

Mike Kutz

What database are you trying to connect to?

User_H3J7U

Use /nolog parameter.

thatJeffSmith-Oracle

If you don't know your username and/or password, you need to talk to the person responsible for your database. If you don't know who that is, we can't help you.

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

Post Details

Locked on Jan 3 2018
Added on Dec 6 2017
7 comments
3,643 views