Global.helma.Mail (smtp)
Sourcecode in /home/hannes/helma/modules/helma/Mail.js:
22:   helma.Mail = function(smtp) {
23:       var OK          =  0;
24:       var SUBJECT     = 10;
25:       var TEXT        = 11;
26:       var MIMEPART    = 12;
27:       var TO          = 20;
28:       var CC          = 21;
29:       var BCC         = 22;
30:       var FROM        = 23;
31:      var REPLYTO     = 24;
32:      var SEND        = 30;
33:      var MAILPKG     = Packages.javax.mail;
34:  
35:      var self = this;
36:      var errStr = "Error in helma.Mail";
37:  
38:      var System              = java.lang.System;
39:      var Properties          = java.util.Properties;
40:      var IOException         = java.io.IOException;
41:      var Wrapper             = Packages.org.mozilla.javascript.Wrapper;
42:      var FileDataSource      = Packages.javax.activation.FileDataSource;
43:      var DataHandler         = Packages.javax.activation.DataHandler;
44:  
45:      var Address             = MAILPKG.Address;
46:      var Message             = MAILPKG.Message;
47:      var Multipart           = MAILPKG.Multipart;
48:      var Session             = MAILPKG.Session;
49:      var Transport           = MAILPKG.Transport;
50:      var InternetAddress     = MAILPKG.internet.InternetAddress;
51:      var AddressException    = MAILPKG.internet.AddressException;
52:      var MimePart            = MAILPKG.internet.MimePart;
53:      var MimeBodyPart        = MAILPKG.internet.MimeBodyPart;
54:      var MimeMessage         = MAILPKG.internet.MimeMessage;
55:      var MimeUtility         = MAILPKG.internet.MimeUtility;
56:      var MimeMultipart       = MAILPKG.internet.MimeMultipart;
57:      var MimePartDataSource  = MAILPKG.internet.MimePartDataSource;
58:  
59:      var buffer, multipart;
60:  
61:      var props = new Properties();
62:      System.setProperty(
63:          "mail.mime.charset",
64:          System.getProperty("mail.charset", "ISO-8859-15")
65:      );
66:  
67:      var host = smtp || getProperty("smtp");
68:      if (host != null) {
69:          props.put("mail.smtp.host", host);
70:      }
71:  
72:      var session = Session.getInstance(props);
73:      var message = new MimeMessage(session);
74:  
75:      var setStatus = function(status) {
76:          if (self.status === OK) {
77:              self.status = status;
78:          }
79:          return;
80:      };
81:  
82:      var getStatus = function() {
83:          return self.status;
84:      };
85:  
86:      var addRecipient = function(addstr, name, type) {
87:          if (addstr.indexOf("@") < 0) {
88:              throw new AddressException();
89:          }
90:          if (name != null) {
91:              var address = new InternetAddress(addstr, 
92:                                MimeUtility.encodeWord(name.toString()));
93:          } else {
94:              var address = new InternetAddress(addstr);
95:          }
96:          message.addRecipient(type, address);
97:          return;
98:      };
99:  
100:      this.status = OK;
101:   
102:      this.setFrom = function(addstr, name) {
103:          try {
104:              if (addstr.indexOf("@") < 0) {
105:                  throw new AddressException();
106:              }
107:              if (name != null) {
108:                  var address = new InternetAddress(addstr, 
109:                                    MimeUtility.encodeWord(name.toString()));
110:              } else {
111:                  var address = new InternetAddress(addstr);
112:              }
113:              message.setFrom(address);
114:          } catch (mx) {
115:              res.debug(errStr + ".setFrom(): " + mx);
116:              setStatus(FROM);
117:          }
118:          return;
119:      };
120:  
121:     this.setReplyTo = function(addstr) {
122:         try {
123:             if (addstr.indexOf("@") < 0) {
124:                 throw new AddressException();
125:             }
126:             var address = [new InternetAddress(addstr)];
127:             message.setReplyTo(address);
128:         } catch (mx) {
129:             res.debug(errStr + ".setReplyTo(): " + mx);
130:             setStatus(REPLYTO);
131:         }
132:         return;
133:     }
134: 
135:     this.setTo = function(addstr, name) {
136:         try {
137:             addRecipient(addstr, name, Message.RecipientType.TO);
138:         } catch (mx) {
139:             res.debug(errStr + ".setTo(): " + mx);
140:             setStatus(TO);
141:         }
142:         return;
143:     };
144: 
145:     this.addTo = function(addstr, name) {
146:         try {
147:             addRecipient(addstr, name, Message.RecipientType.TO);
148:         } catch (mx) {
149:             res.debug(errStr + ".addTo(): " + mx);
150:             setStatus(TO);
151:         }
152:         return;
153:     }
154: 
155:     this.addCC = function(addstr, name) {
156:         try {
157:             addRecipient(addstr, name, Message.RecipientType.CC);
158:         } catch (mx) {
159:             res.debug(errStr + ".addCC(): " + mx);
160:             setStatus(CC);
161:         }
162:         return;
163:     }
164: 
165:     this.addBCC = function(addstr, name) {
166:         try {
167:             addRecipient(addstr, name, Message.RecipientType.BCC);
168:         } catch (mx) {
169:             res.debug(errStr + ".addBCC(): " + mx);
170:             setStatus(BCC);
171:         }
172:         return;
173:     }
174: 
175:     this.setSubject = function(subject) {
176:         if (!subject) {
177:             return;
178:         }
179:         try {
180:             message.setSubject(MimeUtility.encodeWord(subject.toString()));
181:         } catch (mx) {
182:             res.debug(errStr + ".setSubject(): " + mx);
183:             setStatus(SUBJECT);
184:         }
185:         return;
186:     };
187: 
188:     this.setText = function(text) {
189:         if (text) {
190:             buffer = new java.lang.StringBuffer(text);
191:         }
192:         return;
193:     };
194: 
195:     this.addText = function(text) {
196:         if (buffer == null) {
197:             buffer = new java.lang.StringBuffer(text);
198:         } else {
199:             buffer.append(text);
200:         }
201:         return;
202:     };
203: 
204:     this.addPart = function(obj, filename) {
205:         try {
206:             if (obj == null) {
207:                 throw new IOException(
208:                     errStr + ".addPart: method called with wrong number of arguments."
209:                 );
210:             }
211:             if (multipart == null) {
212:                 multipart = new MimeMultipart();
213:             }
214:             if (obj instanceof Wrapper) {
215:                 obj = obj.unwrap();
216:             }
217: 
218:             var part = new MimeBodyPart();
219:             if (typeof obj == "string") {
220:                 part.setContent(obj.toString(), "text/plain");
221:             } else if (obj instanceof File) {
222:                 // FIXME: the following line did not work under windows:
223:                 //var source = new FileDataSource(obj);
224:                 var source = new FileDataSource(obj.getPath());
225:                 part.setDataHandler(new DataHandler(source));
226:             } else if (obj instanceof MimePart) {
227:                 var source = new MimePartDataSource(obj);
228:                 part.setDataHandler(new DataHandler(source));
229:             }
230:             if (filename != null) {
231:                 try {
232:                     part.setFileName(filename.toString());
233:                 } catch (x) {}
234:             } else if (obj instanceof File) {
235:                 try {
236:                     part.setFileName(obj.getName());
237:                 } catch (x) {}
238:             }
239:             multipart.addBodyPart(part);
240:         } catch (mx) {
241:             res.debug(errStr + ".addPart(): " + mx);
242:             setStatus(MIMEPART);
243:         }
244:         return;
245:     }
246: 
247:     this.send = function() {
248:         if (this.status > OK) {
249:             res.debug(errStr + ".send(): Status is " + this.status);
250:             return;
251:         }
252:         try {
253:             if (buffer != null) {
254:                 if (multipart != null) {
255:                     var part = new MimeBodyPart();
256:                     part.setContent(buffer.toString(), "text/plain");
257:                     multipart.addBodyPart(part, 0);
258:                     message.setContent(multipart);
259:                 } else {
260:                     message.setText(buffer.toString());
261:                 }
262:             } else if (multipart != null) {
263:                 message.setContent(multipart);
264:             } else {
265:                 message.setText("");
266:             }
267:             message.setSentDate(new Date());
268:             Transport.send(message);
269:         } catch (mx) {
270:             res.debug(errStr + ".send(): " + mx);
271:             setStatus(SEND);
272:         }
273:         return;
274:     };
275: 
276:     for (var i in this)
277:         this.dontEnum(i);
278: 
279:     return this;
280: }