KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > util > EMail


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.util;
15
16 import java.sql.*;
17 import java.io.*;
18 import java.net.*;
19 import java.util.*;
20
21 import javax.mail.*;
22 import javax.mail.internet.*;
23 import javax.activation.*;
24
25 import com.sun.mail.smtp.*;
26
27
28 /**
29  * EMail Object.
30  * Resources:
31  * http://java.sun.com/products/javamail/index.html
32  * http://java.sun.com/products/javamail/FAQ.html
33  *
34  * <p>
35  * When I try to send a message, I get javax.mail.SendFailedException:
36  * 550 Unable to relay for my-address
37  * <br>
38  * This is an error reply from your SMTP mail server. It indicates that
39  * your mail server is not configured to allow you to send mail through it.
40  *
41  * @author Jorg Janke
42  * @version $Id: EMail.java,v 1.30 2003/10/11 05:20:32 jjanke Exp $
43  */

44 public final class EMail implements Serializable
45 {
46     /**
47      * Minimum conveniance Constructor for mail from current SMTPHost and User.
48      *
49      * @param ctx Context
50      * @param fromCurrentOrRequest if true get user or request - otherwise user only
51      * @param to Recipient EMail address
52      * @param subject Subject of message
53      * @param message The message
54      */

55     public EMail (Properties ctx, boolean fromCurrentOrRequest,
56         String JavaDoc to, String JavaDoc subject, String JavaDoc message)
57     {
58         this (EMailUtil.getSmtpHost(ctx),
59             fromCurrentOrRequest ? EMailUtil.getEMail(ctx, false) : EMailUtil.getEMail_User(ctx, false),
60             to, subject, message);
61         m_ctx = ctx;
62     } // EMail
63

64     /**
65      * Minumum Constructor.
66      * Need to set subject and message
67      *
68      * @param smtpHost The mail server
69      * @param from Sender's EMail address
70      * @param to Recipient EMail address
71      */

72     public EMail (String JavaDoc smtpHost, String JavaDoc from, String JavaDoc to)
73     {
74     // log.info("(" + smtpHost + ") " + from + " -> " + to);
75
setSmtpHost(smtpHost);
76         setFrom(from);
77         addTo(to);
78     } // EMail
79

80     /**
81      * Full Constructor
82      *
83      * @param smtpHost The mail server
84      * @param from Sender's EMail address
85      * @param to Recipient EMail address
86      * @param subject Subject of message
87      * @param message The message
88      */

89     public EMail (String JavaDoc smtpHost, String JavaDoc from, String JavaDoc to, String JavaDoc subject, String JavaDoc message)
90     {
91     // log.info("(" + smtpHost + ") " + from + " -> " + to + ", Subject=" + subject);
92
setSmtpHost(smtpHost);
93         setFrom(from);
94         addTo(to);
95         setSubject (subject);
96         setMessageText (message);
97         m_valid = isValid (true);
98     } // EMail
99

100     /** Client SMTP CTX key */
101     public static final String JavaDoc CTX_SMTP = "#Client_SMTP";
102     /** User EMail CTX key */
103     public static final String JavaDoc CTX_EMAIL = "#User_EMail";
104     public static final String JavaDoc CTX_EMAIL_USER = "#User_EMailUser";
105     public static final String JavaDoc CTX_EMAIL_USERPW = "#User_EMailUserPw";
106     /** Request EMail CTX key */
107     public static final String JavaDoc CTX_REQUEST_EMAIL = "#Request_EMail";
108     public static final String JavaDoc CTX_REQUEST_EMAIL_USER = "#Request_EMailUser";
109     public static final String JavaDoc CTX_REQUEST_EMAIL_USERPW = "#Request_EMailUserPw";
110
111     /** From Address */
112     private InternetAddress m_from;
113     /** To Address */
114     private ArrayList m_to;
115     /** CC Addresses */
116     private ArrayList m_cc;
117     /** BCC Addresses */
118     private ArrayList m_bcc;
119     /** Reply To Address */
120     private InternetAddress m_replyTo;
121     /** Mail Subject */
122     private String JavaDoc m_subject;
123     /** Mail Plain Message */
124     private String JavaDoc m_messageText;
125     /** Mail HTML Message */
126     private String JavaDoc m_messageHTML;
127     /** Mail SMTP Server */
128     private String JavaDoc m_smtpHost;
129     /** Attachments */
130     private ArrayList m_attachments;
131     /** UserName and Password */
132     private EMailAuthenticator m_auth = null;
133     /** Message */
134     private SMTPMessage m_msg = null;
135     /** Comtext - may be null */
136     private Properties m_ctx;
137
138     /** Info Valid */
139     private boolean m_valid = false;
140
141     /** Mail Sent OK Status */
142     public static final String JavaDoc SENT_OK = "OK";
143
144     /** Logger */
145     protected Logger log = Logger.getCLogger (getClass());
146     /** Logger */
147     protected static Logger s_log = Logger.getCLogger (EMail.class);
148
149     /**
150      * Send Mail direct
151      * @return OK or error message
152      */

153     public String JavaDoc send ()
154     {
155         log.info("send (" + m_smtpHost + ") " + m_from + " -> " + m_to);
156         //
157
if (!isValid(true))
158             return "Invalid Data";
159         //
160
Properties props = System.getProperties();
161         props.put("mail.store.protocol", "smtp");
162         props.put("mail.transport.protocol", "smtp");
163         props.put("mail.host", m_smtpHost);
164         //
165
setEMailUser();
166         if (m_auth != null)
167             props.put("mail.smtp.auth","true");
168         Session session = Session.getDefaultInstance(props, m_auth);
169         session.setDebug(Log.isTraceLevel(10));
170
171         try
172         {
173         // m_msg = new MimeMessage(session);
174
m_msg = new SMTPMessage(session);
175             // Addresses
176
m_msg.setFrom(m_from);
177             InternetAddress[] rec = getTos();
178             if (rec.length == 1)
179                 m_msg.setRecipient (Message.RecipientType.TO, rec[0]);
180             else
181                 m_msg.setRecipients (Message.RecipientType.TO, rec);
182             rec = getCcs();
183             if (rec != null && rec.length > 0)
184                 m_msg.setRecipients (Message.RecipientType.CC, rec);
185             rec = getBccs();
186             if (rec != null && rec.length > 0)
187                 m_msg.setRecipients (Message.RecipientType.BCC, rec);
188             if (m_replyTo != null)
189                 m_msg.setReplyTo(new Address[] {m_replyTo});
190             //
191
m_msg.setSentDate(new java.util.Date JavaDoc());
192             m_msg.setHeader("Comments", "CompiereMail");
193         // m_msg.setDescription("Description");
194
// SMTP specifics
195
m_msg.setAllow8bitMIME(true);
196             // Send notification on Failure & Success - no way to set envid in Java yet
197
m_msg.setNotifyOptions (SMTPMessage.NOTIFY_FAILURE | SMTPMessage.NOTIFY_SUCCESS);
198             // Bounce only header
199
m_msg.setReturnOption (SMTPMessage.RETURN_HDRS);
200         // m_msg.setHeader("X-Mailer", "msgsend");
201
//
202
setContent();
203             m_msg.saveChanges();
204         // log.debug("send - message =" + m_msg);
205
//
206
// Transport.send(msg);
207
Transport t = session.getTransport("smtp");
208         // log.debug("send - transport=" + t);
209
t.connect();
210         // t.connect(m_smtpHost, user, password);
211
// log.debug("send - transport connected");
212
Transport.send(m_msg);
213         // t.sendMessage(msg, msg.getAllRecipients());
214
log.debug("send - success - MessageID=" + m_msg.getMessageID());
215         }
216         catch (MessagingException me)
217         {
218             Exception JavaDoc ex = me;
219             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("send(ME)");
220             boolean printed = false;
221             do
222             {
223                 if (ex instanceof SendFailedException)
224                 {
225                     SendFailedException sfex = (SendFailedException)ex;
226                     Address[] invalid = sfex.getInvalidAddresses();
227                     if (!printed)
228                     {
229                         if (invalid != null && invalid.length > 0)
230                         {
231                             sb.append (" - Invalid:");
232                             for (int i = 0; i < invalid.length; i++)
233                                 sb.append (" ").append (invalid[i]);
234
235                         }
236                         Address[] validUnsent = sfex.getValidUnsentAddresses ();
237                         if (validUnsent != null && validUnsent.length > 0)
238                         {
239                             sb.append (" - ValidUnsent:");
240                             for (int i = 0; i < validUnsent.length; i++)
241                                 sb.append (" ").append (validUnsent[i]);
242                         }
243                         Address[] validSent = sfex.getValidSentAddresses ();
244                         if (validSent != null && validSent.length > 0)
245                         {
246                             sb.append (" - ValidSent:");
247                             for (int i = 0; i < validSent.length; i++)
248                                 sb.append (" ").append (validSent[i]);
249                         }
250                         printed = true;
251                     }
252                     if (sfex.getNextException() == null)
253                         sb.append(" ").append(sfex.getLocalizedMessage());
254                 }
255                 else if (ex instanceof AuthenticationFailedException)
256                 {
257                     sb.append(" - Invalid Username/Password - " + m_auth);
258                 }
259                 else
260                 {
261                     String JavaDoc msg = ex.getLocalizedMessage();
262                     if (msg == null)
263                         msg = ex.toString();
264                     sb.append(" ").append(msg);
265                 }
266                 if (ex instanceof MessagingException)
267                     ex = ((MessagingException)ex).getNextException();
268                 else
269                     ex = null;
270             } while (ex != null);
271             log.error(sb.toString(), me);
272             return sb.toString();
273         }
274         catch (Exception JavaDoc e)
275         {
276             log.error("send", e);
277             return "EMail.send: " + e.getLocalizedMessage();
278         }
279         //
280
if (Log.isTraceLevel(9))
281             dumpMessage();
282         return SENT_OK;
283     } // send
284

285     /**
286      * Dump Message Info
287      */

288     private void dumpMessage()
289     {
290         if (m_msg == null)
291             return;
292         try
293         {
294             Enumeration e = m_msg.getAllHeaderLines ();
295             while (e.hasMoreElements ())
296                 log.debug("- " + e.nextElement ());
297         }
298         catch (MessagingException ex)
299         {
300             log.error("dumpMessage", ex);
301         }
302     } // dumpMessage
303

304     /**
305      * Get the message directly
306      * @return mail message
307      */

308     protected MimeMessage getMimeMessage()
309     {
310         return m_msg;
311     } // getMessage
312

313     /**
314      * Get Message ID or null
315      * @return Message ID e.g. <20030130004739.15377.qmail@web13506.mail.yahoo.com>
316      * <25699763.1043887247538.JavaMail.jjanke@main>
317      */

318     public String JavaDoc getMessageID()
319     {
320         try
321         {
322             if (m_msg != null)
323                 return m_msg.getMessageID ();
324         }
325         catch (MessagingException ex)
326         {
327             log.error("getMessageID", ex);
328         }
329         return null;
330     } // getMessageID
331

332     /** Getter/Setter ********************************************************/
333
334     /**
335      * Create Authentificator for User
336      * @param username user name
337      * @param password user password
338      */

339     public void setEMailUser (String JavaDoc username, String JavaDoc password)
340     {
341         if (username == null || password == null)
342             log.warn("setEMailUser ignored - " + username + "/" + password);
343         else
344         {
345         // log.debug ("setEMailUser: " + username + "/" + password);
346
m_auth = new EMailAuthenticator (username, password);
347         }
348     } // setEmailUser
349

350     /**
351      * Try to set Authentication
352      */

353     private void setEMailUser ()
354     {
355         // already set
356
if (m_auth != null)
357             return;
358         //
359
String JavaDoc from = m_from.getAddress();
360         Properties ctx = m_ctx == null ? Env.getCtx() : m_ctx;
361         //
362
String JavaDoc email = Env.getContext(ctx, CTX_EMAIL);
363         String JavaDoc usr = Env.getContext(ctx, CTX_EMAIL_USER);
364         String JavaDoc pwd = Env.getContext(ctx, CTX_EMAIL_USERPW);
365         if (from.equals(email) && usr.length() > 0 && pwd.length() > 0)
366         {
367             setEMailUser (usr, pwd);
368             return;
369         }
370         email = Env.getContext(ctx, CTX_REQUEST_EMAIL);
371         usr = Env.getContext(ctx, CTX_REQUEST_EMAIL_USER);
372         pwd = Env.getContext(ctx, CTX_REQUEST_EMAIL_USERPW);
373         if (from.equals(email) && usr.length() > 0 && pwd.length() > 0)
374             setEMailUser (usr, pwd);
375     } // setEMailUser
376

377
378     /**
379      * Get Sender
380      * @return Sender's internet address
381      */

382     public InternetAddress getFrom()
383     {
384         return m_from;
385     } // getFrom
386

387     /**
388      * Set Sender
389      * @param newFrom Sender's email address
390      */

391     public void setFrom(String JavaDoc newFrom)
392     {
393         if (newFrom == null)
394         {
395             m_valid = false;
396             return;
397         }
398         try
399         {
400             m_from = new InternetAddress (newFrom, true);
401         }
402         catch (Exception JavaDoc e)
403         {
404             log.error("setFrom", e);
405             m_valid = false;
406         }
407     } // setFrom
408

409     /**
410      * Add To Recipient
411      * @param newTo Recipient's email address
412      * @returns true if valid
413      */

414     public boolean addTo (String JavaDoc newTo)
415     {
416         if (newTo == null || newTo.length() == 0)
417         {
418             m_valid = false;
419             return false;
420         }
421         InternetAddress ia = null;
422         try
423         {
424             ia = new InternetAddress (newTo, true);
425         }
426         catch (Exception JavaDoc e)
427         {
428             log.error("addTo - " + e.toString());
429             m_valid = false;
430             return false;
431         }
432         if (m_to == null)
433             m_to = new ArrayList();
434         m_to.add(ia);
435         return true;
436     } // addTo
437

438     /**
439      * Get Recipient
440      * @return Recipient's internet address
441      */

442     public InternetAddress getTo()
443     {
444         if (m_to == null || m_to.size() == 0)
445             return null;
446         InternetAddress ia = (InternetAddress)m_to.get(0);
447         return ia;
448     } // getTo
449

450     /**
451      * Get TO Recipients
452      * @return Recipient's internet address
453      */

454     public InternetAddress[] getTos()
455     {
456         if (m_to == null || m_to.size() == 0)
457             return null;
458         InternetAddress[] ias = new InternetAddress[m_to.size()];
459         m_to.toArray(ias);
460         return ias;
461     } // getTos
462

463     /**
464      * Add CC Recipient
465      * @param newCc EMail cc Recipient
466      * @returns true if valid
467      */

468     public boolean addCc (String JavaDoc newCc)
469     {
470         if (newCc == null || newCc.length() == 0)
471             return false;
472         InternetAddress ia = null;
473         try
474         {
475             ia = new InternetAddress (newCc, true);
476         }
477         catch (Exception JavaDoc e)
478         {
479             log.error("addCc", e);
480             return false;
481         }
482         if (m_cc == null)
483             m_cc = new ArrayList();
484         m_cc.add (ia);
485         return true;
486     } // addCc
487

488     /**
489      * Get CC Recipients
490      * @return Recipient's internet address
491      */

492     public InternetAddress[] getCcs()
493     {
494         if (m_cc == null || m_cc.size() == 0)
495             return null;
496         InternetAddress[] ias = new InternetAddress[m_cc.size()];
497         m_cc.toArray(ias);
498         return ias;
499     } // getCcs
500

501     /**
502      * Add BCC Recipient
503      * @param newBcc EMail cc Recipient
504      * @returns true if valid
505      */

506     public boolean addBcc (String JavaDoc newBcc)
507     {
508         if (newBcc == null || newBcc.length() == 0)
509             return false;
510         InternetAddress ia = null;
511         try
512         {
513             ia = new InternetAddress (newBcc, true);
514         }
515         catch (Exception JavaDoc e)
516         {
517             log.error("addBcc", e);
518             return false;
519         }
520         if (m_bcc == null)
521             m_bcc = new ArrayList();
522         m_bcc.add (ia);
523         return true;
524     } // addBcc
525

526     /**
527      * Get BCC Recipients
528      * @return Recipient's internet address
529      */

530     public InternetAddress[] getBccs()
531     {
532         if (m_bcc == null || m_bcc.size() == 0)
533             return null;
534         InternetAddress[] ias = new InternetAddress[m_bcc.size()];
535         m_bcc.toArray(ias);
536         return ias;
537     } // getBccs
538

539     /**
540      * Set Reply to Address
541      * @param newTo email address
542      * @returns true if valid
543      */

544     public boolean setReplyTo (String JavaDoc newTo)
545     {
546         if (newTo == null || newTo.length() == 0)
547             return false;
548         InternetAddress ia = null;
549         try
550         {
551             ia = new InternetAddress (newTo, true);
552         }
553         catch (Exception JavaDoc e)
554         {
555             log.error("setReplyTo", e);
556             return false;
557         }
558         m_replyTo = ia;
559         return true;
560     } // setReplyTo
561

562     /**
563      * Get Reply To
564      * @return Reoly To internet address
565      */

566     public InternetAddress getReplyTo()
567     {
568         return m_replyTo;
569     } // getReplyTo
570

571     /*************************************************************************/
572
573     /**
574      * Set Subject
575      * @param newSubject Subject
576      */

577     public void setSubject(String JavaDoc newSubject)
578     {
579         if (newSubject == null || newSubject.length() == 0)
580             m_valid = false;
581         else
582             m_subject = newSubject;
583     } // setSubject
584

585     /**
586      * Get Subject
587      * @return subject
588      */

589     public String JavaDoc getSubject()
590     {
591         return m_subject;
592     } // getSubject
593

594     /**
595      * Set Message
596      * @param newMessage message
597      */

598     public void setMessageText (String JavaDoc newMessage)
599     {
600         if (newMessage == null || newMessage.length() == 0)
601             m_valid = false;
602         else
603         {
604             m_messageText = newMessage;
605             if (!m_messageText.endsWith("\n"))
606                 m_messageText += "\n";
607         }
608     } // setMessage
609

610     /**
611      * Get MIME String Message - line ending with CRLF.
612      * @return message
613      */

614     public String JavaDoc getMessageCRLF()
615     {
616         if (m_messageText == null)
617             return "";
618         char[] chars = m_messageText.toCharArray();
619         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
620         for (int i = 0; i < chars.length; i++)
621         {
622             char c = chars[i];
623             if (c == '\n')
624             {
625                 int previous = i-1;
626                 if (previous >= 0 && chars[previous] == '\r')
627                     sb.append(c);
628                 else
629                     sb.append("\r\n");
630             }
631             else
632                 sb.append(c);
633         }
634     // log.debug("IN " + m_messageText);
635
// log.debug("OUT " + sb);
636
return sb.toString();
637     } // getMessageCRLF
638

639     /**
640      * Set HTML Message
641      * @param html message
642      */

643     public void setMessageHTML (String JavaDoc html)
644     {
645         if (html == null || html.length() == 0)
646             m_valid = false;
647         else
648         {
649             m_messageHTML = html;
650             if (!m_messageHTML.endsWith("\n"))
651                 m_messageHTML += "\n";
652         }
653     } // setMessageHTML
654

655     /**
656      * Set HTML Message
657      * @param subject subject repeated in message as H2
658      * @param message message
659      */

660     public void setMessageHTML (String JavaDoc subject, String JavaDoc message)
661     {
662         m_subject = subject;
663         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<HTML>\n")
664             .append("<HEAD>\n")
665             .append("<TITLE>\n")
666             .append(subject + "\n")
667             .append("</TITLE>\n")
668             .append("</HEAD>\n");
669         sb.append("<BODY>\n")
670             .append("<H2>" + subject + "</H2>" + "\n")
671             .append(message)
672             .append("\n")
673             .append("</BODY>\n");
674         sb.append("</HTML>\n");
675         m_messageHTML = sb.toString();
676     } // setMessageHTML
677

678     /**
679      * Get HTML Message
680      * @return message
681      */

682     public String JavaDoc getMessageHTML()
683     {
684         return m_messageHTML;
685     } // getMessageHTML
686

687     /**
688      * Add file Attachment
689      * @param file file to attach
690      */

691     public void addAttachment (File file)
692     {
693         if (file == null)
694             return;
695         if (m_attachments == null)
696             m_attachments = new ArrayList();
697         m_attachments.add(file);
698     } // addAttachment
699

700     /**
701      * Add url based file Attachment
702      * @param url url content to attach
703      */

704     public void addAttachment (URL url)
705     {
706         if (url == null)
707             return;
708         if (m_attachments == null)
709             m_attachments = new ArrayList();
710         m_attachments.add(url);
711     } // addAttachment
712

713     /**
714      * Add attachment.
715      * (converted to ByteArrayDataSource)
716      * @param data data
717      * @param type MIME type
718      * @param name name of attachment
719      */

720     public void addAttachment (byte[] data, String JavaDoc type, String JavaDoc name)
721     {
722         ByteArrayDataSource byteArray = new ByteArrayDataSource (data, type).setName(name);
723         addAttachment (byteArray);
724     } // addAttachment
725

726     /**
727      * Add arbitary Attachment
728      * @param dataSource content to attach
729      */

730     public void addAttachment (DataSource dataSource)
731     {
732         if (dataSource == null)
733             return;
734         if (m_attachments == null)
735             m_attachments = new ArrayList();
736         m_attachments.add(dataSource);
737     } // addAttachment
738

739     /**
740      * Set the message content
741      * @throws MessagingException
742      * @throws IOException
743      */

744     private void setContent ()
745         throws MessagingException, IOException
746     {
747         m_msg.setSubject (getSubject ());
748
749         // Simple Message
750
if (m_attachments == null || m_attachments.size() == 0)
751         {
752             if (m_messageHTML == null || m_messageHTML.length () == 0)
753                 m_msg.setContent (getMessageCRLF(), "text/plain");
754             else
755                 m_msg.setDataHandler (new DataHandler
756                     (new ByteArrayDataSource (m_messageHTML, "text/html")));
757             //
758
log.debug("setContent (simple) " + getSubject());
759         }
760         else // Multi part message ***************************************
761
{
762             // First Part - Message
763
MimeBodyPart mbp_1 = new MimeBodyPart();
764             mbp_1.setText("");
765             if (m_messageHTML == null || m_messageHTML.length () == 0)
766                 mbp_1.setContent (getMessageCRLF(), "text/plain");
767             else
768                 mbp_1.setDataHandler (new DataHandler
769                     (new ByteArrayDataSource (m_messageHTML, "text/html")));
770
771             // Create Multipart and its parts to it
772
Multipart mp = new MimeMultipart();
773             mp.addBodyPart(mbp_1);
774             log.debug("setContent (multi) " + getSubject() + " - " + mbp_1);
775
776             // for all attachments
777
for (int i = 0; i < m_attachments.size(); i++)
778             {
779                 Object JavaDoc attachment = m_attachments.get(i);
780                 DataSource ds = null;
781                 if (attachment instanceof File)
782                 {
783                     File file = (File)attachment;
784                     if (file.exists())
785                         ds = new FileDataSource (file);
786                     else
787                     {
788                         log.error("setContent - File does not exist: " + file);
789                         continue;
790                     }
791                 }
792                 else if (attachment instanceof URL)
793                 {
794                     URL url = (URL)attachment;
795                     ds = new URLDataSource (url);
796                 }
797                 else if (attachment instanceof DataSource)
798                     ds = (DataSource)attachment;
799                 else
800                 {
801                     log.error("setContent - Attachement type unknown: " + attachment);
802                     continue;
803                 }
804                 // Attachment Part
805
MimeBodyPart mbp_2 = new MimeBodyPart();
806                 mbp_2.setDataHandler(new DataHandler(ds));
807                 mbp_2.setFileName(ds.getName());
808                 log.debug("setContent - Added Attachment " + ds.getName() + " - " + mbp_2);
809                 mp.addBodyPart(mbp_2);
810             }
811
812             // Add to Message
813
m_msg.setContent(mp);
814         } // multi=part
815
} // setContent
816

817     /*************************************************************************/
818
819     /**
820      * Set SMTP Host or address
821      * @param newSmtpHost Mail server
822      */

823     public void setSmtpHost(String JavaDoc newSmtpHost)
824     {
825         if (newSmtpHost == null || newSmtpHost.length() == 0)
826             m_valid = false;
827         else
828             m_smtpHost = newSmtpHost;
829     } // setSMTPHost
830

831     /**
832      * Get Mail Server name or address
833      * @return mail server
834      */

835     public String JavaDoc getSmtpHost()
836     {
837         return m_smtpHost;
838     } // getSmtpHosr
839

840     /**
841      * Is Info valid to send EMail
842      * @return true if email is valid and can be sent
843      */

844     public boolean isValid()
845     {
846         return m_valid;
847     } // isValid
848

849     /**
850      * Re-Check Info if valid to send EMail
851      * @param recheck if true, re-evaluate email
852      * @return true if email is valid and can be sent
853      */

854     public boolean isValid (boolean recheck)
855     {
856         // mandatory info
857
if (m_from == null || m_from.getAddress().length() == 0)
858         {
859             log.warn("isValid - From is invalid=" + m_from);
860             return false;
861         }
862         InternetAddress ia = getTo();
863         if (ia == null || ia.getAddress().length() == 0)
864         {
865             log.warn("isValid - To is invalid=" + m_to);
866             return false;
867         }
868         if (m_smtpHost == null || m_smtpHost.length() == 0)
869         {
870             log.warn("isValid - SMTP Host is invalid" + m_smtpHost);
871             return false;
872         }
873         if (m_subject == null || m_subject.length() == 0)
874         {
875             log.warn("isValid - Subject is invalid=" + m_subject);
876             return false;
877         }
878         return true;
879     } // isValid
880

881
882     /*************************************************************************/
883
884     /**
885      * Test
886      * java -cp CTools.jar;CClient.jar org.compiere.util.EMail main info@compiere.org jjanke@compiere.org "My Subject" "My Message"
887      * @param args Array of arguments
888      */

889     public static void main (String JavaDoc[] args)
890     {
891         org.compiere.Compiere.startupClient ();
892         Log.setTraceLevel(9);
893         /** Test **/
894         EMail emailTest = new EMail("main", "jjanke@compiere.org", "jjanke@compiere.org", "TestSubject", "TestMessage");
895     // EMail emailTest = new EMail("main", "jjanke@compiere.org", "jjanke@yahoo.com");
896
// emailTest.addTo("jjanke@acm.org");
897
// emailTest.addCc("jjanke@yahoo.com");
898
// emailTest.setMessageHTML("My Subject1", "My Message1");
899
// emailTest.addAttachment(new File("C:\\Compiere2\\RUN_Compiere2.sh"));
900
emailTest.setEMailUser("info", "test");
901         emailTest.send();
902         System.exit(0);
903         /** Test */
904
905         if (args.length != 5)
906         {
907             System.out.println("Parameters: smtpHost from to subject message");
908             System.out.println("Example: java org.compiere.util.EMail mail.acme.com joe@acme.com sue@acme.com HiThere CheersJoe");
909             System.exit(1);
910         }
911         EMail email = new EMail(args[0], args[1], args[2], args[3], args[4]);
912         email.send();
913     } // main
914

915 } // EMail
916
Popular Tags