[Schmitzm-commits] r1311 - in trunk: src/schmitzm/mail src_junit/schmitzm src_junit/schmitzm/mail
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Sun Nov 28 21:17:51 CET 2010
Author: alfonx
Date: 2010-11-28 21:17:50 +0100 (Sun, 28 Nov 2010)
New Revision: 1311
Added:
trunk/src/schmitzm/mail/EmailThread.java
trunk/src/schmitzm/mail/Mailer.java
trunk/src_junit/schmitzm/mail/
trunk/src_junit/schmitzm/mail/MailUtilTest.java
Modified:
trunk/src/schmitzm/mail/MailUtil.java
Log:
Working on SMTP mail stuff
Added: trunk/src/schmitzm/mail/EmailThread.java
===================================================================
--- trunk/src/schmitzm/mail/EmailThread.java 2010-11-28 19:36:23 UTC (rev 1310)
+++ trunk/src/schmitzm/mail/EmailThread.java 2010-11-28 20:17:50 UTC (rev 1311)
@@ -0,0 +1,24 @@
+package schmitzm.mail;
+
+import org.apache.log4j.Logger;
+
+public class EmailThread extends Thread {
+
+ static final Logger log = Logger.getLogger(EmailThread.class);
+
+ public EmailThread(final Mailer mailer, final String email,
+ final String subject, final String body) {
+ super(new Runnable() {
+
+ @Override
+ public void run() {
+ try {
+ mailer.sendMail(email, subject, body);
+ } catch (Exception e) {
+ log.error("", e);
+ }
+ }
+
+ });
+ }
+}
Property changes on: trunk/src/schmitzm/mail/EmailThread.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id URL
Name: svn:eol-style
+ native
Modified: trunk/src/schmitzm/mail/MailUtil.java
===================================================================
--- trunk/src/schmitzm/mail/MailUtil.java 2010-11-28 19:36:23 UTC (rev 1310)
+++ trunk/src/schmitzm/mail/MailUtil.java 2010-11-28 20:17:50 UTC (rev 1311)
@@ -50,6 +50,70 @@
private static final int MAX_DESKTOP_MAIL_BODY_SIZE = 1500;
/**
+ * Use a {@link Mailer} instance if you youse this method often.
+ *
+ * @param recipientsAddress
+ * @param subject
+ * @param text
+ * @param smtpUser
+ * @param smtpCredential
+ * @param smtpAuth
+ * @param smtpHost
+ * @param mailSenderAddress
+ * @throws AddressException
+ * @throws MessagingException
+ */
+ public static void sendSmtpMail(final String recipientsAddress,
+ final String subject, final String text, String smtpUser,
+ String smtpCredential, String smtpAuth, String smtpHost,
+ String mailSenderAddress) throws AddressException,
+ MessagingException {
+ final String senderAddress = mailSenderAddress;
+ log.debug("try: Sende Mail via " + smtpHost + " an "
+ + recipientsAddress + " als " + senderAddress
+ + " with Betreff = " + subject);
+
+ // It is expected that the client supplies values for the properties
+ // listed in Appendix A of the JavaMail spec
+ // (particularly mail.store.protocol, mail.transport.protocol,
+ // mail.host, mail.user, and mail.from
+ Properties mailProps = new Properties();
+ mailProps.setProperty("mail.smtp.user", smtpUser);
+ mailProps.setProperty("mail.smtp.credential", smtpCredential);
+ mailProps.setProperty("mail.sender.address", mailSenderAddress);
+ mailProps.setProperty("mail.smtp.host", smtpHost);
+ mailProps.setProperty("mail.smtp.auth", smtpAuth);
+ // mailProps.setProperty("mail.smtp.starttls.enable",
+ // getCfg("mail.smtp.starttls.enable"));
+ // mailProps.setProperty("mail.smtp.ssl.protocols",
+ // getCfg("mail.smtp.ssl.protocols"));
+
+ final Session session = Session.getInstance(mailProps,
+ new SimpleStringAuthenticator(smtpUser, smtpCredential));
+
+ // Eine neue Message erzeugen
+ final Message msg = new MimeMessage(session);
+
+ // Hier werden die Absender- und Empfängeradressen gesetzt
+ msg.setFrom(new InternetAddress(senderAddress));
+ msg.setRecipients(Message.RecipientType.TO,
+ InternetAddress.parse(recipientsAddress, false));
+
+ // Der Betreff und Body der Message werden gesetzt
+ msg.setSubject(subject);
+
+ // msg.setContent(text, "text/plain");
+ msg.setText(text);
+
+ // Hier lassen sich HEADER-Informationen hinzufügen
+ // msg.setHeader("Test", "Test");
+ msg.setSentDate(new Date());
+
+ // Zum Schluss wird die Mail natürlich noch verschickt
+ Transport.send(msg);
+ }
+
+ /**
* Opens the local mail client with a new mail.
*
* @param mailDestAddr
Added: trunk/src/schmitzm/mail/Mailer.java
===================================================================
--- trunk/src/schmitzm/mail/Mailer.java 2010-11-28 19:36:23 UTC (rev 1310)
+++ trunk/src/schmitzm/mail/Mailer.java 2010-11-28 20:17:50 UTC (rev 1311)
@@ -0,0 +1,31 @@
+package schmitzm.mail;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.AddressException;
+
+/**
+ * A Mailer-Instance has all parameters set which are needed to send a mail.
+ */
+public class Mailer {
+ final String smtpUser;
+ final String smtpCredential;
+ final String smtpAuth;
+ final String smtpHost;
+ final String mailSenderAddress;
+
+ public Mailer(String smtpUser, String smtpCredential, String smtpAuth,
+ String smtpHost, String mailSenderAddress) {
+ this.smtpUser = smtpUser;
+ this.smtpCredential = smtpCredential;
+ this.smtpAuth = smtpAuth;
+ this.smtpHost = smtpHost;
+ this.mailSenderAddress = mailSenderAddress;
+ }
+
+ public void sendMail(final String recipientsAddress, final String subject,
+ final String text) throws AddressException, MessagingException {
+ MailUtil.sendSmtpMail(recipientsAddress, subject, text, smtpUser,
+ smtpCredential, smtpAuth, smtpHost, mailSenderAddress);
+ }
+
+}
Property changes on: trunk/src/schmitzm/mail/Mailer.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id URL
Name: svn:eol-style
+ native
Added: trunk/src_junit/schmitzm/mail/MailUtilTest.java
===================================================================
--- trunk/src_junit/schmitzm/mail/MailUtilTest.java 2010-11-28 19:36:23 UTC (rev 1310)
+++ trunk/src_junit/schmitzm/mail/MailUtilTest.java 2010-11-28 20:17:50 UTC (rev 1311)
@@ -0,0 +1,52 @@
+package schmitzm.mail;
+
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
+public class MailUtilTest extends schmitzm.junit.TestingClass {
+
+ @Test
+ public void testSendSmtpMail_BugReport() {
+ String recipientsAddress = "bugreport at wikisquare.de";
+ String subject = "junit MailUtilTest";
+ String text = "MailUtilTest works";
+ String smtpUser = "bugreport";
+ String smtpCredential = "";
+ String smtpAuth = "";
+ String smtpHost = "www.wikisquare.de";
+ String mailSenderAddress = "bugreport at wikisquare.de";
+ try {
+ MailUtil.sendSmtpMail(recipientsAddress, subject, text, smtpUser,
+ smtpCredential, smtpAuth, smtpHost, mailSenderAddress);
+ } catch (Exception e) {
+ log.info("failed to send a test-mail to bugreport at wikisquare.de", e);
+ fail(e.getMessage());
+ }
+ }
+
+ @Test
+ public void testSendSmtpMail_ToOtherEmail() {
+ String recipientsAddress = "christian.altenhofen at sidion.de";
+ String subject = "junit MailUtilTest";
+ String text = "Sending a test from bugreport at wikisquare.de to an external mail-address (christian.altenhofen at sidion.de) must fail!. Secure the mail server!";
+ String smtpUser = "bugreport";
+ String smtpCredential = "";
+ String smtpAuth = "";
+ String smtpHost = "www.wikisquare.de";
+ String mailSenderAddress = "christian.altenhofen at sidion.de";
+ try {
+ MailUtil.sendSmtpMail(recipientsAddress, subject, text, smtpUser,
+ smtpCredential, smtpAuth, smtpHost, mailSenderAddress);
+ fail("Sending a test from bugreport at wikisquare.de to christian.altenhofen at sidion.de must fail!. Secure the mail server!");
+ } catch (Exception e) {
+ }
+
+ try {
+ MailUtil.sendSmtpMail(recipientsAddress, subject, text, smtpUser,
+ smtpCredential, smtpAuth, smtpHost, mailSenderAddress);
+ fail("Sending a test from bugreport at wikisquare.de as christian.altenhofen at sidion.de to christian.altenhofen at sidion.de must fail!. Secure the mail server!");
+ } catch (Exception e) {
+ }
+ }
+}
Property changes on: trunk/src_junit/schmitzm/mail/MailUtilTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id URL
Name: svn:eol-style
+ native
More information about the Schmitzm-commits
mailing list