[Schmitzm-commits] r2151 - in trunk/schmitzm-core/src: main/java/de/schmitzm/net/mail test/java/de/schmitzm/net/mail

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri Nov 30 01:24:20 CET 2012


Author: alfonx
Date: 2012-11-30 01:24:20 +0100 (Fri, 30 Nov 2012)
New Revision: 2151

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/Mailer.java
   trunk/schmitzm-core/src/test/java/de/schmitzm/net/mail/MailUtilTest.java
Log:
sendSmtpMail um eine Option html erweitert. ich habe Martin sendHtmlMail gesehen, aber da wird die Auth anders aufgebaut. Eine Wrapper Klasse ist angelegt.Nach aussen keine API changes.

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java	2012-11-28 17:36:29 UTC (rev 2150)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java	2012-11-30 00:24:20 UTC (rev 2151)
@@ -79,6 +79,7 @@
 	 */
 	public static final int MAX_DESKTOP_MAIL_BODY_SIZE = 1500;
 
+
 	/**
 	 * Use a {@link Mailer} instance if you youse this method often.
 	 * 
@@ -96,27 +97,14 @@
 	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,
+			String mailSenderAddress, boolean html) 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"));
+		Properties mailProps = createMailProps(smtpUser, smtpCredential, smtpAuth, smtpHost, mailSenderAddress);
 
 		final Session session = Session.getInstance(mailProps,
 				new SimpleStringAuthenticator(smtpUser, smtpCredential));
@@ -133,7 +121,10 @@
 		msg.setSubject(subject);
 
 		// msg.setContent(text, "text/plain");
-		msg.setText(text);
+		if (html)
+			msg.setContent(text, "text/html; charset=utf-8");
+		else
+			msg.setText(text);
 
 		// Hier lassen sich HEADER-Informationen hinzufügen
 		// msg.setHeader("Test", "Test");
@@ -142,6 +133,36 @@
 		// Zum Schluss wird die Mail natürlich noch verschickt
 		Transport.send(msg);
 	}
+
+	private static Properties createMailProps(String smtpUser, String smtpCredential, String smtpAuth, String smtpHost,
+			String mailSenderAddress ) {
+		Properties mailProps = new Properties();
+		
+		// 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
+
+		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"));
+		return mailProps;
+	}
+
+	/**
+	 * Ruft {@link #sendSmtpMail(String, String, String, String, String, String, String, String, boolean)} mit html=false auf.
+	 */
+	public static void sendSmtpMail(String recipientsAddress, String subject, String text, String smtpUser,
+			String smtpCredential, String smtpAuth, String smtpHost, String mailSenderAddress) throws AddressException, MessagingException {
+		sendSmtpMail(recipientsAddress, subject, text, smtpUser, smtpCredential, smtpAuth, smtpHost, mailSenderAddress, false);
+	}
 	
 	/**
 	 * Send HTML mail by SMTP server.
@@ -462,4 +483,6 @@
       }
       
     }
+    
+
 }

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/Mailer.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/Mailer.java	2012-11-28 17:36:29 UTC (rev 2150)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/Mailer.java	2012-11-30 00:24:20 UTC (rev 2151)
@@ -31,7 +31,7 @@
 			final String text) throws AddressException, MessagingException {
 		try {
 			MailUtil.sendSmtpMail(recipientsAddress, subject, text, smtpUser,
-					smtpCredential, smtpAuth, smtpHost, mailSenderAddress);
+					smtpCredential, smtpAuth, smtpHost, mailSenderAddress, true);
 		} catch (Exception e) {
 			throw new RuntimeException(e);
 		}

Modified: trunk/schmitzm-core/src/test/java/de/schmitzm/net/mail/MailUtilTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/net/mail/MailUtilTest.java	2012-11-28 17:36:29 UTC (rev 2150)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/net/mail/MailUtilTest.java	2012-11-30 00:24:20 UTC (rev 2151)
@@ -31,7 +31,7 @@
 	public void testSendSmtpMail_BugReport() {
 		String recipientsAddress = "bugreport at wikisquare.de";
 		String subject = "junit MailUtilTest";
-		String text = "MailUtilTest works";
+		String text = "MailUtilTest works now";
 		String smtpUser = "bugreport";
 		String smtpCredential = "";
 		String smtpAuth = "";
@@ -48,8 +48,8 @@
 
 	@Test
 	@Ignore
-	// Since the mailserver is now local to hudson, this test is not valid
-	// anymore
+//	// Since the mailserver is now local to hudson, this test is not valid
+//	// anymore
 	public void testSendSmtpMail_ToOtherEmail() {
 		String recipientsAddress = "christian.altenhofen at sidion.de";
 		String subject = "junit MailUtilTest";



More information about the Schmitzm-commits mailing list