diff options
Diffstat (limited to 'tdejava/koala/examples/simplemail')
-rw-r--r-- | tdejava/koala/examples/simplemail/MailHelper.java | 220 | ||||
-rw-r--r-- | tdejava/koala/examples/simplemail/README | 29 | ||||
-rw-r--r-- | tdejava/koala/examples/simplemail/SimpleMailFrm.java | 240 | ||||
-rw-r--r-- | tdejava/koala/examples/simplemail/SimpleMailer.java | 39 |
4 files changed, 528 insertions, 0 deletions
diff --git a/tdejava/koala/examples/simplemail/MailHelper.java b/tdejava/koala/examples/simplemail/MailHelper.java new file mode 100644 index 00000000..1d33a21c --- /dev/null +++ b/tdejava/koala/examples/simplemail/MailHelper.java @@ -0,0 +1,220 @@ +//package com.werpu.simplemail; + + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Properties; + +import javax.mail.Address; +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +/** + * + * @author Werner Punz werpu@gmx.at + * @version 1.0 + * This is a basic Mailer class built upon the JavaMail API + * it only does SMTP. + */ +public class MailHelper +{ + + private String message = ""; + + private String recipient = ""; + + private String server = ""; + + private String password = ""; + + private String sender = ""; + + private String subject = ""; + + private String username = ""; + + private LinkedList ccAddresses = new LinkedList(); + + /** Creates a new instance of LC_Mailer */ + public MailHelper() + { + } + + /** Getter for property message. + * @return Value of property message. + */ + public String getMessage() + { + return message; + } + + /** Setter for property message. + * @param message New value of property message. + */ + public void setMessage(String message) + { + this.message = message; + } + + /** Getter for property password. + * @return Value of property password. + */ + public String getPassword() + { + return password; + } + + /** Setter for property password. + * @param password New value of property password. + */ + public void setPassword(String password) + { + this.password = password; + } + + /** Getter for property recipient. + * @return Value of property recipient. + */ + public String getRecipient() + { + return recipient; + } + + /** Setter for property recipient. + * @param recipient New value of property recipient. + */ + public void setRecipient(String recipient) + { + this.recipient = recipient; + } + + /** Getter for property sender. + * @return Value of property sender. + */ + public String getSender() + { + return sender; + } + + /** Setter for property sender. + * @param sender New value of property sender. + */ + public void setSender(String sender) + { + this.sender = sender; + } + + /** Getter for property server. + * @return Value of property server. + */ + public String getServer() + { + return server; + } + + /** Setter for property server. + * @param server New value of property server. + */ + public void setServer(String server) + { + this.server = server; + } + + /** Getter for property subject. + * @return Value of property subject. + */ + public String getSubject() + { + return subject; + } + + /** Setter for property subject. + * @param subject New value of property subject. + */ + public void setSubject(String subject) + { + this.subject = subject; + } + + /** + * Method setUsername. + * @param username + */ + public void setUsername(String username) + { + this.username = username; + } + + /** + * Method addCCAddress. + * @param ccAddresses + * Adds a single CC Adress to the current CC Addresses + */ + public void addCCAddress(String ccAddresses) + { + if (!ccAddresses.equalsIgnoreCase("null")) + this.ccAddresses.add(ccAddresses); + } + + /** + * Method addCCAddress. + * @param ccAddresses + * adds the ccAddresses to the current messaging parameters + */ + public void addCCAddress(Iterator ccAddresses) + { + + while (ccAddresses.hasNext()) + addCCAddress((String) ccAddresses.next()); + } + + /** + * Method send. + * @throws MessagingException + * sends out the mail with the set messaging parameters + */ + public void send() throws MessagingException + { + Properties props = new Properties(); + Session session = Session.getDefaultInstance(props, null); + + + Address to = new InternetAddress(getRecipient()); + Address from = new InternetAddress(getSender()); + + //only one from address + Address[] froms = new Address[1]; + froms[0] = from; + + MimeMessage message = new MimeMessage(session); + message.setText(getMessage()); + message.setSubject(getSubject()); + message.addRecipient(Message.RecipientType.TO, to); + + //Add CCs to the recipient list + Iterator i = ccAddresses.iterator(); + while (i.hasNext()) + { + to = new InternetAddress((String) i.next()); + message.addRecipient(Message.RecipientType.BCC, to); + } + + message.addFrom(froms); + message.saveChanges(); + + + //set smtp + Transport myTransport = session.getTransport("smtp"); + + //send mail + myTransport.connect(getServer(), username.trim(), password.trim()); + + myTransport.sendMessage(message,message.getAllRecipients()); + myTransport.close(); + } + +} diff --git a/tdejava/koala/examples/simplemail/README b/tdejava/koala/examples/simplemail/README new file mode 100644 index 00000000..d1c1cd13 --- /dev/null +++ b/tdejava/koala/examples/simplemail/README @@ -0,0 +1,29 @@ +Werner Punz writes: + +A simple send the email app I hacked together today in 2 hours, feel + free to add it to the repository... + + This time I used the QT designer and the provided sed script to convert + the files into near-java files and then started from there. + +First you need JDK 1.2+ since a LinkedList ist used. + Secondly you have to have the javamail and activation jar files in the + classpath otherwise it won?t work! Those are not part of the standard JDK + distro, and haven? t made it into 1.4 yet, but nevertheless are great APIs + to access email and nntp systems easily (actually activation isn? t but + javamail depends on it). + + Both can be obtained from the Sun java website. + + Here are the links to these api's taken from one of my readme's + + 1. Download the e-mail api's from sun. You will need two packages. + + http://java.sun.com/products/javamail/ + http://java.sun.com/products/javabeans/glasgow/jaf.html + + Ah yes you can add multiple BCCs simply by separating them with a ',' or a + ';'. + + Thats all have much fun with it :-). + diff --git a/tdejava/koala/examples/simplemail/SimpleMailFrm.java b/tdejava/koala/examples/simplemail/SimpleMailFrm.java new file mode 100644 index 00000000..bbbe3d0a --- /dev/null +++ b/tdejava/koala/examples/simplemail/SimpleMailFrm.java @@ -0,0 +1,240 @@ +// package com.werpu.simplemail; + + +/** + * SimpleMailFrm + * This is the main window from of the simple mail application + * @author Werner Punz werpu@gmx.at + * This class was generated by Qt Designer and then converted to Java and + * redesigned to fit into the KDE Framework + */ + +import org.trinitydesktop.qt.*; +import org.trinitydesktop.koala.*; +import java.util.StringTokenizer; +import java.util.LinkedList; + + + +class SimpleMailFrm extends TDEMainWindow { + + + TQGroupBox groupBox1; + KLineEdit txtServer; + KLineEdit txtUserName; + KPasswordEdit txtPassword; + TQLabel textLabel1; + TQLabel textLabel2; + TQLabel textLabel3; + KLineEdit txtFrom; + KLineEdit txtTo; + KLineEdit txtBCC; + TQLabel textLabel4; + TQLabel textLabel6; + TQLabel textLabel5; + KPushButton btSend; + KPushButton btCancel; + TQMultiLineEdit txtMessage; + + + TQGridLayout form1Layout; + TQGridLayout groupBox1Layout; + + TDEApplication parentApp = null; + + public SimpleMailFrm(TDEApplication kApp) + + { + super( null, null,0); + parentApp = kApp; + + + resize( 582, 486 ); + setCaption( trUtf8( "Simple Mailer" ) ); + + TQGroupBox centralBox = new TQGroupBox((TQWidget) this,""); + + form1Layout = new TQGridLayout(centralBox, 1, 1, 11, 6, "form1Layout"); + + groupBox1 = new TQGroupBox( centralBox, "groupBox1" ); + groupBox1.setTitle( trUtf8( "Mailserver data" ) ); + groupBox1.setColumnLayout(0, Qt.Vertical ); + groupBox1.layout().setSpacing( 6 ); + groupBox1.layout().setMargin( 11 ); + groupBox1Layout = new TQGridLayout( groupBox1.layout() ); + groupBox1Layout.setAlignment( Qt.AlignTop ); + + txtServer = new KLineEdit( groupBox1, "txtServer" ); + + groupBox1Layout.addWidget( txtServer, 0, 1 ); + + txtUserName = new KLineEdit( groupBox1, "txtUserName" ); + + groupBox1Layout.addWidget( txtUserName, 1, 1 ); + + txtPassword = new KPasswordEdit( groupBox1, "txtPassword" ); + + groupBox1Layout.addWidget( txtPassword, 2, 1 ); + + textLabel1 = new TQLabel( groupBox1, "textLabel1" ); + textLabel1.setText( trUtf8( "Server" ) ); + + groupBox1Layout.addWidget( textLabel1, 0, 0 ); + + textLabel2 = new TQLabel( groupBox1, "textLabel2" ); + textLabel2.setText( trUtf8( "Username" ) ); + + groupBox1Layout.addWidget( textLabel2, 1, 0 ); + + textLabel3 = new TQLabel( groupBox1, "textLabel3" ); + textLabel3.setText( trUtf8( "Password" ) ); + + groupBox1Layout.addWidget( textLabel3, 2, 0 ); + + txtFrom = new KLineEdit( groupBox1, "txtFrom" ); + + groupBox1Layout.addWidget( txtFrom, 0, 3 ); + + txtTo = new KLineEdit( groupBox1, "txtTo" ); + + groupBox1Layout.addWidget( txtTo, 1, 3 ); + + txtBCC = new KLineEdit( groupBox1, "txtBCC" ); + + groupBox1Layout.addWidget( txtBCC, 2, 3 ); + + textLabel4 = new TQLabel( groupBox1, "textLabel4" ); + textLabel4.setText( trUtf8( "From" ) ); + + groupBox1Layout.addWidget( textLabel4, 0, 2 ); + + textLabel6 = new TQLabel( groupBox1, "textLabel6" ); + textLabel6.setText( trUtf8( "BCC" ) ); + + groupBox1Layout.addWidget( textLabel6, 2, 2 ); + + textLabel5 = new TQLabel( groupBox1, "textLabel5" ); + textLabel5.setText( trUtf8( "To" ) ); + + groupBox1Layout.addWidget( textLabel5, 1, 2 ); + + form1Layout.addMultiCellWidget( groupBox1, 0, 0, 0, 1 ); + + btSend = new KPushButton( centralBox, "btSend" ); + btSend.setText( trUtf8( "Send" ) ); + + form1Layout.addWidget( btSend, 2, 0 ); + + btCancel = new KPushButton( centralBox, "btCancel" ); + btCancel.setText( trUtf8( "Cancel" ) ); + + form1Layout.addWidget( btCancel, 2, 1 ); + + txtMessage = new TQMultiLineEdit(centralBox, "txtMessage" ); + + form1Layout.addMultiCellWidget( txtMessage, 1, 1, 0, 1 ); + + setCentralWidget(centralBox); + // tab order + setTabOrder( txtServer, txtUserName ); + setTabOrder( txtUserName, txtPassword ); + setTabOrder( txtPassword, txtFrom ); + setTabOrder( txtFrom, txtTo ); + setTabOrder( txtTo, txtBCC ); + setTabOrder( txtBCC, txtMessage ); + setTabOrder( txtMessage, btSend ); + setTabOrder( btSend, btCancel ); + + setEventHandlers(); + } + + //---------------------------------------------------------- + // Getter Methods to access the data outside of the + // current class + //---------------------------------------------------------- + + public String getServer() { + return txtServer.text(); + } + + public String getUserName() { + return txtUserName.text(); + } + + public String getPassword() { + return txtPassword.text(); + } + + public String getFrom() { + return txtFrom.text(); + } + + public String getTo() { + return txtTo.text(); + } + + public String getBCC() { + return txtBCC.text(); + } + + public String getMessage() { + return txtMessage.text(); + } + + void setEventHandlers() { + connect( btCancel, SIGNAL("clicked()"), parentApp, SLOT("quit()")); + connect( btSend,SIGNAL("clicked()"),this,SLOT("sendMail()")); + } + + + //-------------------------------------------- + // Slots + //------------------------------------------- + public void sendMail() { + String server = getServer(); + String userName = getUserName(); + String password = getPassword(); + String from = getFrom(); + String to = getTo(); + String message = getMessage(); + LinkedList bccs = getBCCs(); + + try { + MailHelper mailer = new MailHelper(); + mailer.setMessage(message); + mailer.setSubject("Simple Mail"); + mailer.setSender(from); + mailer.setRecipient(to); + mailer.addCCAddress(bccs.iterator()); + mailer.setServer(server); + mailer.setUsername(userName); + mailer.setPassword(password); + mailer.send(); + } + catch (Exception ex) { + KMessageBox.error(this , ex.getMessage()); + return; + } + KMessageBox.information(this,"Mail was successfully sent!"); + } + + //------------------------------------- + //Helpers + //------------------------------------- + + /** + * Split the BCCs into single entries if nedded + */ + LinkedList getBCCs() { + LinkedList retVal = new LinkedList(); + + String bcc = getBCC(); + StringTokenizer splitter = new StringTokenizer(bcc,",;"); + + while(splitter.hasMoreTokens()) + retVal.add(splitter.nextToken()); + + return retVal; + } +} + diff --git a/tdejava/koala/examples/simplemail/SimpleMailer.java b/tdejava/koala/examples/simplemail/SimpleMailer.java new file mode 100644 index 00000000..a31b88b0 --- /dev/null +++ b/tdejava/koala/examples/simplemail/SimpleMailer.java @@ -0,0 +1,39 @@ +//package com.werpu.simplemail; + +import org.trinitydesktop.qt.*; +import org.trinitydesktop.koala.*; + + +/** + * + * @author Werner Punz werpu@gmx.at + * This is a simple mailer to demonstrate the connection + * of KDE Java with java packages. + * It demonstrates the connection between KDEJava and the javamail API + * to send mails from a KDE Application box via the SMTP protocol + * Note:The javamail and activation jar files have to be in your classpath! + * + */ +public class SimpleMailer extends TDEApplication{ + static { + qtjava.initialize(); + tdejava.initialize(); + } + + public SimpleMailer() { + super(); + SimpleMailFrm mainWidget = new SimpleMailFrm(this); + this.setMainWidget(mainWidget); + mainWidget.show(); + this.exec(); + } + + /** + * Entry point for the program + */ + public static void main(String [] argv) { + TDECmdLineArgs.init(argv, "simplemailer", "SimpleMailer", + "Simple Mailing Application", "0.1"); + SimpleMailer mailClnt = new SimpleMailer(); + } +} |