Created
March 19, 2009 13:24
-
-
Save dizzi/81812 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cz.citco.u1; | |
import java.awt.GridLayout; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Properties; | |
import javax.mail.Message; | |
import javax.mail.MessagingException; | |
import javax.mail.Session; | |
import javax.mail.Transport; | |
import javax.mail.internet.AddressException; | |
import javax.mail.internet.InternetAddress; | |
import javax.mail.internet.MimeBodyPart; | |
import javax.mail.internet.MimeMessage; | |
import javax.mail.internet.MimeMultipart; | |
import javax.swing.JButton; | |
import javax.swing.JDialog; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JOptionPane; | |
import javax.swing.JPanel; | |
import javax.swing.JTextField; | |
import org.apache.commons.configuration.ConfigurationException; | |
import org.apache.commons.configuration.PropertiesConfiguration; | |
/** | |
* @author D.Mesko | |
* Simple email application | |
*/ | |
public class EmailApp extends JFrame{ | |
private static final long serialVersionUID = 366439665535206984L; | |
private JTextField smtp, recipient, subject, attachment, text; | |
private PropertiesConfiguration config; | |
private static String configName = "config.properties"; | |
public EmailApp() { | |
config = loadConfig(configName); | |
if(config==null) | |
System.exit(-1); | |
this.setSize(400, 200); | |
this.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE); | |
this.setResizable(false); | |
this.getContentPane().add(initGUIElements()); | |
smtp.setText(config.getString("smtp", "")); | |
this.setVisible(true); | |
} | |
/** | |
* Function which adds GUI elements | |
* @return JPanel with added elements | |
*/ | |
private JPanel initGUIElements() { | |
JPanel panel = new JPanel(new GridLayout(0,2,5,5)); | |
panel.add(new JLabel("SMTP server:")); | |
this.smtp = new JTextField(); | |
panel.add(this.smtp); | |
panel.add(new JLabel("Recipient:")); | |
this.recipient = new JTextField(); | |
panel.add(this.recipient); | |
panel.add(new JLabel("Subject:")); | |
this.subject = new JTextField(); | |
panel.add(this.subject); | |
panel.add(new JLabel("Attachment:")); | |
this.attachment = new JTextField(); | |
panel.add(this.attachment); | |
panel.add(new JLabel("Text:")); | |
this.text = new JTextField(); | |
panel.add(this.text); | |
panel.add(new JLabel("")); | |
JButton send = new JButton("Send"); | |
send.addMouseListener(new MouseAdapter(){ | |
@Override | |
public void mouseClicked(MouseEvent e) { | |
super.mouseClicked(e); | |
sendEmail(); | |
} | |
}); | |
panel.add(send); | |
return panel; | |
} | |
/** | |
* Function saving actual config to file | |
*/ | |
private void saveConfig(){ | |
try { | |
this.config.save(config.getFile()); | |
} catch (ConfigurationException e) { | |
System.out.println("Can't save config file: "+e.getMessage()); | |
} | |
} | |
/** | |
* Function loading config from file | |
*/ | |
private PropertiesConfiguration loadConfig(String configName){ | |
try { | |
PropertiesConfiguration config = new PropertiesConfiguration(configName); | |
return config; | |
} catch (ConfigurationException e) { | |
errorMessage("Can't load config file: "+e.getMessage()+"\nProgram will be terminated"); | |
} | |
return null; | |
} | |
/** | |
* Function which creates and send emails | |
*/ | |
public void sendEmail(){ | |
try { | |
// check if required fields arent empty, and set system and message properties | |
Properties props = System.getProperties(); | |
if(smtp.getText().equals("")){ | |
errorMessage("You have set SMTP server"); | |
return; | |
} | |
props.put("mail.smtp.host", smtp.getText()); | |
Session session = Session.getInstance(props, null); | |
Message msg = new MimeMessage(session); | |
if(recipient.getText().equals("")){ | |
errorMessage("You have set recipient address"); | |
return; | |
} | |
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient.getText(), false)); | |
msg.setFrom(new InternetAddress("[email protected]")); | |
msg.setSubject(subject.getText()); | |
// try to open entered file | |
File file = new File(attachment.getText()); | |
// if attachment field is not empty and file doesnt exist, complain | |
if(!attachment.getText().equals("")&&!file.exists()){ | |
errorMessage("Entered file doesn't exist, try again"); | |
return; | |
} | |
// if file exist, make multipart email | |
if (file.exists()) { | |
MimeBodyPart mbp1 = new MimeBodyPart(); | |
mbp1.setText(text.getText()); | |
MimeBodyPart mbp2 = new MimeBodyPart(); | |
mbp2.attachFile(file); | |
MimeMultipart mp = new MimeMultipart(); | |
mp.addBodyPart(mbp1); | |
mp.addBodyPart(mbp2); | |
msg.setContent(mp); | |
} else { | |
// there is no attachment, send simple message | |
msg.setText(text.getText()); | |
} | |
Transport.send(msg); | |
} catch (AddressException e) { | |
errorMessage("Address error: "+e.getMessage()); | |
return; | |
} catch (MessagingException e) { | |
errorMessage("Messaging error: "+e.getMessage()); | |
return; | |
} catch (IOException e){ | |
errorMessage("File IO error: "+e.getMessage()); | |
return; | |
} | |
errorMessage("All ok, program will be terminated"); | |
// change config value and save | |
this.config.setProperty("smtp", this.smtp.getText()); | |
saveConfig(); | |
System.exit(0); | |
} | |
/** | |
* Wrapper for dialog box | |
* @param text - text to show in dialog | |
*/ | |
private void errorMessage(String text){ | |
JOptionPane.showMessageDialog(null, text); | |
} | |
public static void main(String[] args) { | |
new EmailApp(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment