[Yanel-dev] Patch für MailUtil

Balz Schreier balz.schreier at gmail.com
Wed Sep 8 10:53:35 CEST 2010


Skipped content of type multipart/alternative-------------- next part -----=
---------
Index: MailUtil.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- MailUtil.java	(revision 53045)
+++ MailUtil.java	(working copy)
@@ -1,5 +1,9 @@
 package org.wyona.yanel.core.util;
 =

+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.util.Properties;
+
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.Session;
@@ -93,4 +97,82 @@
         // Send the message
         Transport.send(msg);
     }
+    =

+    /**
+     * @param replyTo
+     *            email address (if null, then no reply-to will be set)
+     * @param charset
+     *            Charset, e.g. utf-8
+     * @param mimeSubType
+     *            Mime sub-type, e.g. "html" or "plain"
+     * @param fromEmailAdress e.g. abc at gmail.com
+     * @param fromName e.g. "Alfred Jung" (then the receiver of the mail w=
ill see this name instead of the address itself, looks better if supported)
+     */
+    public static void sendAuthenticated(String smtpHost, int smtpPort, St=
ring fromEmailAddress, String fromName, String replyTo, String to,
+            String subject, String content, String charset, String mimeSub=
Type, String username, String password)
+            throws AddressException, MessagingException {
+        if (username =3D=3D null) {
+            username =3D "username";
+        }
+        if (password =3D=3D null) {
+            password =3D "password";
+        }
+        if (charset =3D=3D null) {
+            charset =3D Charset.defaultCharset().name();
+        }
+        if (mimeSubType =3D=3D null) {
+            mimeSubType =3D "plain";
+        }
+        // Create a mail session
+        Session session =3D null;
+        Properties props =3D new Properties();
+        if (smtpHost !=3D null && smtpPort >=3D 0) {
+            props.put("mail.smtp.host", smtpHost);
+            props.put("mail.smtp.port", "" + smtpPort);
+            session =3D Session.getInstance(props, null);
+            log.warn("Use specific mail session: " + session.getProperty("=
mail.smtp.host") + ":"
+                    + session.getProperty("mail.smtp.port"));
+        } else {
+            props.put("mail.smtp.host", "mail.foo.bar"); // Dummy value
+            props.put("mail.smtp.port", "37"); // Dummy value
+            session =3D Session.getDefaultInstance(props, null);
+            log.warn("Use default mail session: " + session.getProperty("m=
ail.smtp.host") + ":"
+                    + session.getProperty("mail.smtp.port"));
+        }
+
+        // Construct the message
+        MimeMessage msg =3D new MimeMessage(session);
+        if (fromName !=3D null && !("".equals(fromName))) {
+            try {
+                msg.setFrom(new InternetAddress(fromEmailAddress, fromName=
));
+                log.debug("Using fromName =3D "+fromName+" / fromEmail =3D=
 "+fromEmailAddress);
+            } catch (UnsupportedEncodingException e) {
+                msg.setFrom(new InternetAddress(fromEmailAddress));
+                log.debug("Using fromEmail only =3D "+fromEmailAddress);
+            }
+        } else {
+            msg.setFrom(new InternetAddress(fromEmailAddress));
+            log.debug("Using fromEmail only =3D "+fromEmailAddress);
+        }
+        if (replyTo !=3D null && !("".equals(replyTo))) {
+            InternetAddress[] replyToAddresses =3D new InternetAddress[1];
+            replyToAddresses[0] =3D new InternetAddress(replyTo);
+            msg.setReplyTo(replyToAddresses);
+        }
+        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to)=
);
+        msg.setSubject(subject);
+        msg.setText(content, charset, mimeSubType);
+
+        String protocol =3D "smtp";
+        props.put("mail." + protocol + ".auth", "true");
+
+        Transport t =3D session.getTransport(protocol);
+        log.debug("+++ send mail: before connect");
+        t.connect(username, password);
+        log.debug("+++ send mail: connect ok, now sending...");
+        t.sendMessage(msg, msg.getAllRecipients());
+        log.debug("+++ send mail OK");
+
+    }
+
 }


More information about the Yanel-development mailing list