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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

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

_AZ_

i think i should elaborate that I do expect to receive only one row ( from the select). Anything more (or less) should be deemed an error.

Answer

Well, with the code you have above you *will* get an error: TypeError: 'NoneType' object is not iterable. The reason for that is that fetchone() returns None if there are no rows left to fetch. That error isn't too helpful, though. You will need to do something along these lines:

row = cursor.fetchone()

if row is None:

   raise Exception("Hey, only one row was returned!")

tim, val = row

You will want to replace the Exception message with something a bit more meaningful, of course!

Marked as Answer by _AZ_ · Sep 27 2020
_AZ_

thank you @Anthony . Is there a better approach that i should.could use ( vs fetchone or overall ) ?

You're welcome. That approach works and is reasoanble. If you want to check for too many rows as well, you can do fetchall() which will return an array and check the length of the array instead. If you're worried about getting back too many rows with fetchall() you can also use fetchmany(2) which will tell you if there are 0, 1, or 2 rows available.

1 - 4
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,632 views