On Tue, Jul 13, 1999 at 03:54:49PM +0900, Won, Taewoong wrote:
> My servlet need to send e-mail message via remote SMTP servler. The
> environment is Solaris 2.6, ServletExec 2.0, Netscape Enterprise
> Server with Sun JDK 1.2 VM. At first I looked into JavaMail API, but
> it is too complicated for our simple needs. Are there any other simple
> SMTP-repackage available that I can use with minimum modification?
I'm sure you've gotten a dozen suggestions, but I'm going to
put a word in in favor of JavaMail. It's _not_ too complicated. I'm
beginning to think the examples given in the documentation are
horrible, though.
I've included a couple of methods that use JavaMail to send
mail. One sends a simple, text-only message with 18 lines of code. The
other sends text _and_ attachments in 35 lines.
package com.kloognome.Mailer;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import java.util.Vector;
import java.io.*;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
public class Mailer {
public static void sendMail(String smtp_host, String from,
String subject, String recip_file, String message_file)
throws IOException, AddressException,
MessagingException {
Properties props = new Properties();
props.put("smtp.host", smtp_host);
Session session = Session.getDefaultInstance(props, null);
InternetAddress[] addr = new InternetAddress(to);
msg.addRecipients(Message.RecipientType.TO, addr);
InternetAddress from_addr = new InternetAddress(from);
msg.setFrom(from_addr);
Message msg = new MimeMessage(session);
msg.setSubject(subject);
String content = readFile(message_file);
msg.setContent(content, "text/plain");
Transport.send(msg);
}
public static void sendWithAttachments(String smtp_host, String from,
String subject, String smtp_host, String to,
String message, Vector attach)
throws IOException, AddressException,
MessagingException {
Properties props = new Properties();
props.put("smtp.host", smtp_host);
Session session = Session.getDefaultInstance(props, null);
InternetAddress addr = new InternetAddress(to)
msg.addRecipients(Message.RecipientType.TO, to);
InternetAddress from_addr = new InternetAddress(from);
msg.setFrom(from_addr);
Message msg = new MimeMessage(session);
msg.setSubject(subject);
MimeMultipart mp = new MimeMultipart();
MimeBodyPart text = new MimeBodyPart();
text.setDisposition(Part.INLINE);
text.setContent(message, "text/plain");
mp.addBodyPart(text);
for (int i = 0; i < attach.size(); i++) {
MimeBodyPart file_part = new MimeBodyPart();
File file = (File) attach.elementAt(i);
FileDataSource fds = new FileDataSource(file);
DataHandler dh = new DataHandler(fds);
file_part.setFileName(file.getName());
file_part.setDisposition(Part.ATTACHMENT);
file_part.setDescription("Attached file: " + file.getName());
file_part.setDataHandler(dh);
mp.addBodyPart(file_part);
}
msg.setContent(mp);
Transport.send(msg);
}
} // Mailer
--
Robert Crawford [EMAIL PROTECTED]
http://www.iac.net/~crawford
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html