[Schmitzm-commits] r2110 - in trunk/schmitzm-core/src: main/java/de/schmitzm/net/mail main/resources/de/schmitzm/swing/resource/icons/small test/java/de/schmitzm/net/mail

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Mon Oct 15 14:36:04 CEST 2012


Author: mojays
Date: 2012-10-15 14:36:04 +0200 (Mon, 15 Oct 2012)
New Revision: 2110

Added:
   trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/icons/small/attach.png
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java
   trunk/schmitzm-core/src/test/java/de/schmitzm/net/mail/MailUtilTest.java
Log:
MailUtil: method to send HTML mail with attachments
New icon

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-10-14 21:31:50 UTC (rev 2109)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java	2012-10-15 12:36:04 UTC (rev 2110)
@@ -2,6 +2,7 @@
 
 import java.awt.Desktop;
 import java.awt.GraphicsEnvironment;
+import java.io.File;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Date;
@@ -10,13 +11,20 @@
 import java.util.Set;
 import java.util.regex.Pattern;
 
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.activation.FileDataSource;
+import javax.mail.BodyPart;
 import javax.mail.Message;
 import javax.mail.MessagingException;
+import javax.mail.Multipart;
 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.JOptionPane;
 
 import org.apache.commons.lang.StringUtils;
@@ -123,7 +131,70 @@
 		// Zum Schluss wird die Mail natürlich noch verschickt
 		Transport.send(msg);
 	}
+	
+	/**
+	 * Send HTML mail by SMTP server.
+	 */
+	public static void sendSmtpHtmlMail(final String recipientsAddress,
+        final String subject, final String text, String smtpUser,
+        String smtpCredential, String smtpAuth, String smtpHost,
+        String mailSenderAddress, File... attachments) throws AddressException,
+        MessagingException {
+	  
+      Properties mailProps = System.getProperties();
 
+      // Setup mail server
+      mailProps.setProperty("mail.smtp.host", smtpHost);
+//      mailProps.setProperty("mail.user", smtpUser);
+//      mailProps.setProperty("mail.password", smtpCredential);
+      mailProps.setProperty("mail.smtp.auth", smtpAuth);
+
+      // Get the default Session object.
+      Session session = Session.getDefaultInstance(mailProps,
+                                                   new SimpleStringAuthenticator(smtpUser, smtpCredential));
+     // Create a default MimeMessage object.
+      MimeMessage message = new MimeMessage(session);
+      // Set From: and To: header field of the header.
+      message.setFrom(new InternetAddress(mailSenderAddress));
+      message.addRecipient(Message.RecipientType.TO,
+                           new InternetAddress(recipientsAddress));
+      // Set Subject: header field
+      message.setSubject(subject);
+
+      // Send the actual HTML message, as big as you like
+      if ( attachments.length == 0 )
+        message.setContent(text,"text/html" );
+      else {
+        // Create a multipart message
+        Multipart multipart = new MimeMultipart("related");
+
+        // Create the message part 
+        BodyPart messageBodyPart = new MimeBodyPart();
+        // Fill the message
+        messageBodyPart.setText(text);
+        messageBodyPart.setContent(text,"text/html");
+        // Set text message part
+        multipart.addBodyPart(messageBodyPart);
+
+        // Part two is attachment
+        for (File f : attachments) {
+          messageBodyPart = new MimeBodyPart();
+          String filename = f.getName();
+          DataSource source = new FileDataSource(f);
+          messageBodyPart.setDataHandler(new DataHandler(source));
+          messageBodyPart.setHeader("Content-ID","<"+filename+">");
+          messageBodyPart.setFileName(filename);
+          multipart.addBodyPart(messageBodyPart);
+        }
+
+        // Send the complete message parts
+        message.setContent(multipart);      
+      }
+      
+      // Send message
+      Transport.send(message);
+	}
+
     /**
      * Opens the local mail client with a new mail.
      * 

Added: trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/icons/small/attach.png
===================================================================
(Binary files differ)


Property changes on: trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/icons/small/attach.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

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-10-14 21:31:50 UTC (rev 2109)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/net/mail/MailUtilTest.java	2012-10-15 12:36:04 UTC (rev 2110)
@@ -1,12 +1,18 @@
 package de.schmitzm.net.mail;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
+import java.io.File;
+import java.net.URL;
 import java.util.regex.Pattern;
 
 import org.junit.Ignore;
 import org.junit.Test;
 
+import de.schmitzm.io.IOUtil;
+import de.schmitzm.swing.SwingUtil;
 import de.schmitzm.testing.TestingClass;
 
 public class MailUtilTest extends TestingClass {
@@ -67,4 +73,29 @@
 		} catch (Exception e) {
 		}
 	}
+
+    @Test
+    @Ignore
+    public void testSendSmtpHtmlMail() throws Throwable {
+        String recipientsAddress = "mojays at web.de";
+        String subject = "junit MailUtilTest";
+        String text = "<html>\n"
+                    + "<body>\n"
+                    + "<h1>Header1</h1>\n"
+                    + "<b><i>Dies ist ein fetter, kursiver Text</i></b><br>\n"
+                    + "<img src=\"cid:add.png\">\n"
+                    + "</body></html>\n";
+        String smtpUser = "martin at mojays.de";
+        String smtpCredential = "@drastE@";
+        String smtpAuth = "true";
+        String smtpHost = "smtp.1und1.de";
+        String mailSenderAddress = "Martin at mojays.de";
+        URL  resource1 = SwingUtil.class.getResource("resource/icons/small/add.png");
+        File attachment1 = IOUtil.urlToFile(resource1);
+        File attachment2 = new File("D:/Sonstiges/maibaumverkauf_2012.pdf");
+        MailUtil.sendSmtpHtmlMail(recipientsAddress, subject, text, smtpUser,
+                                  smtpCredential, smtpAuth, smtpHost, mailSenderAddress,
+                                  attachment1,attachment2);
+
+    }
 }
\ No newline at end of file



More information about the Schmitzm-commits mailing list