# 1
I've managed to find some code that almost does what I need (silent print).
Below is the whole thing, it checks for mail every 3 minutes, when a new item arrives it sounds an audio alert and it displays the message content. With the inclusion of the Print2DPrinterJob class, it will now happily also prints "abc" on my default printer, without asking for confirmation of printer/settings (ie. silent).
I would be very grateful is someone could show me how instead of "abc" it prints the String "contents" (the email message content).
I know... I am guilty of patching together pieces of other peoples examples/tutorials without understanding how it works. I promise (!) I will study and learn to write my own in future, but for now I really need to get this working, so please assist if you can. thanks in advance.
import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.sound.sampled.*;
public class Soop extends JFrame{
AudioFormat audioFormat;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
final JButton playBtn = new JButton("Play");
final JButton updateBtn = new JButton("Update");
final JTextField audioFileLocation = new JTextField("C:/My Sounds/newemail.wav");
final JTextField pollValue = new JTextField("3");
public static void main(String args[]){
new Soop();
}
public Soop () {
try{
String audioFile = audioFileLocation.getText();
String freqmin = pollValue.getText();
int freq = (Integer.valueOf(freqmin)) * 60000 ; // check for mail in freq milliseconds
playBtn.setEnabled(true);
//Instantiate and register action listener
// on the Play button.
playBtn.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
playBtn.setEnabled(false);
final String audioFile = audioFileLocation.getText();
if (audioFileExists(audioFile))
playAudio();//Play the file
else
playBtn.setEnabled(true); // prepare to play another file
}//end actionPerformed
}//end ActionListener
);//end addActionListener()
getContentPane().add(playBtn,"West");
getContentPane().add(updateBtn,"East");
getContentPane().add(audioFileLocation,"North");
getContentPane().add(pollValue,"South");
setTitle("Auto Print Mail");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500,100);
setVisible(true);
for (int cnt=0; ;cnt++) {
// Get system properties
Properties props = new Properties();
// Get session
Session session = Session.getDefaultInstance(props, null);
// Get the store
Store store = session.getStore("pop3");
store.connect("mail.mydomain.com", "me@mydomain.com", "mypassword");
// Get folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
// Get directory
Message message[] = folder.getMessages();
if (message.length == 0) {
System.out.println(cnt+" no new mail \t");
// Close connection
folder.close(false);
}
else {
for (int i=0, n=message.length; i<n; i++) {
System.out.println(i + ": " + message[i].getFrom()[0] + message[i].getContentType() + "\t" + message[i].getSubject());
String content = (message[i]).getContent().toString();
System.out.println(content);
/* Create a print job */
Print2DPrinterJob sp = new Print2DPrinterJob();
}
folder.setFlags(message, new Flags(Flags.Flag.DELETED), true);
// Close connection
folder.close(true);
if (audioFileExists(audioFile))
playAudio();//Play the file
}
store.close();
Thread.sleep(freq); // sleep for freq milliseconds
}
}
catch (Exception e) {
e.printStackTrace();
System.exit(0);
}//end catch
}
public class Print2DPrinterJob implements Printable {
public Print2DPrinterJob() {
/* Construct the print request specification. */
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.PORTRAIT); // or LANDSCAPE
aset.add(new Copies(1));
aset.add(new JobName("New Mail Item", null));
/* Create a print job */
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(this);
try {
pj.print(aset);
}
catch (PrinterException pe) {
System.err.println(pe);
}
}
public int print(Graphics g,PageFormat pf,int pageIndex) {
String thisContent = "abc";
if (pageIndex == 0) {
Graphics2D g2d= (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
g2d.setColor(Color.black);
g2d.drawString(thisContent, 50, 50);
return Printable.PAGE_EXISTS;
}
else {
return Printable.NO_SUCH_PAGE;
}
}
}
private boolean audioFileExists(String filename) {
boolean result = true;
try{
File f = new File(filename);
if (f.exists())
result = true;
else
result = false;
}
catch (Exception e) {
e.printStackTrace();
System.exit(0);
}//end catch
return(result);
}
private void playAudio() {
try{
File soundFile = new File(audioFileLocation.getText());
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
audioFormat = audioInputStream.getFormat();
//System.out.println(audioFormat);
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class,audioFormat);
sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo);
//Create a thread to play back the data and
// start it running until the end of file
new PlayThread().start();
}catch (Exception e) {
e.printStackTrace();
System.exit(0);
}//end catch
}//end playAudio
class PlayThread extends Thread{
byte tempBuffer[] = new byte[10000];
public void run(){
try{
sourceDataLine.open(audioFormat);
sourceDataLine.start();
int cnt;
//Keep looping until the input read method
// returns -1 for empty stream
while((cnt = audioInputStream.read(tempBuffer,0,tempBuffer.length)) != -1){
if(cnt > 0){
//Write data to the internal buffer of
// the data line where it will be
// delivered to the speaker.
sourceDataLine.write(tempBuffer, 0, cnt);
}//end if
}//end while
//Block and wait for internal buffer of the
// data line to empty.
sourceDataLine.drain();
sourceDataLine.close();
//Prepare to playback another file
playBtn.setEnabled(true);
}catch (Exception e) {
e.printStackTrace();
System.exit(0);
}//end catch
}//end run
}//end inner class PlayThread
//===================================//
}