Java SMTP Component
The SMTP component provides an easy to use API for communicating
with SMTP servers and sending e-mail. Written using 100% Java and
delivered as a Java Bean makes the SMTP component usable in a wide
variety of IDE and platform configurations. Download
Secure iNet Factory Evaluation
Features
- File attachment support - The SMTP
component provides a simple API for adding one or more attachments
to a e-mail message. Using this API one can easily add one or
more attachments from a file or URL.
- Bulk mail support - For targeted mail campaigns built in
Bcc: (blind carbon copy) support and the ability to define recipients
from the contents of a file provide an easy API for delivering
bulk e-mail
- HTML message support - For e-mail clients capable of rendering
HTML, this feature allows one to send rich-text e-mail to recipients
in the form of a web page.
- E-mail. address validation - The SMTP component supports
all valid e-mail address formats specified in RFC 822. SMTP
and EmailAddress components offer syntax validation of address,
prior to sending e-mail message. This capability reduces network
traffic from bounced e-mail messages and provides a way of systematically
identifying invalid e-mail addresses.
- User authentication - Methods to support username/password
authentication for SMTP servers which require authentication
to prevent unauthorized SMTP relay.
- Message priority support - For e-mail clients which support
message priorities, the SMTP component allows prioritization
of an e-mail message. A message with "highest" priority
would potentially give the message higher visibility in a recipients
mailbox.
- Header management - Easily define SMTP e-mail header values
for submission with a e-mail message. An example might be to
set the X-Mailer header typically used to define the client
software responsible for generating the mail message.
Code Example
The following code example demonstrates how to send an e-mail
along with an attachment using the SMTP component.
/*
* @(#)SmtpAttachmentExample.java
*
* Copyright (c) 2001-2002 JScape
* 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information of
* JScape. ("Confidential Information"). You shall not disclose such
* Confidential Information and shall use it only in accordance with
* the terms of the license agreement you entered into with JScape.
*/
import com.jscape.inet.smtp.*;
import com.jscape.inet.mime.*;
import com.jscape.inet.email.*;
import java.io.*;
public class SmtpAttachmentExample extends SmtpAdapter {
public void sendMessage(String hostname, String to, String from, String subject, String body, String attachment) throws SmtpException,
IOException, MimeException {
// create new Smtp instance
Smtp smtp = new Smtp(hostname);
// enable debugging
smtp.setDebug(true);
// register this class to capture SMTP related events
smtp.addSmtpListener(this);
// connect to SMTP server
smtp.connect();
// create email message
EmailMessage email = new EmailMessage();
email.setTo(to);
email.setFrom(from);
email.setSubject(subject);
email.setBody(body);
// add attachment to email message
email.addAttachment(new Attachment(new File(attachment)));
// send email message
smtp.send(email);
// disconnect from SMTP server
smtp.disconnect();
}
// capture connect event
public void connected(SmtpConnectedEvent evt) {
System.out.println("Connected to SMTP server: " + evt.getHostname());
}
// capture disconnect event
public void disconnected(SmtpDisconnectedEvent evt) {
System.out.println("Disconnected from SMTP server: " + evt.getHostname());
}
public static void main(String[] args) {
String hostname;
String to;
String from;
String subject;
String body;
String attach;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter SMTP hostname (e.g. mail.domain.com): ");
hostname = reader.readLine();
System.out.print("Enter To address (e.g. recipient@domain.com): ");
to = reader.readLine();
System.out.print("Enter From address (e.g. sender@domain.com): ");
from = reader.readLine();
subject = "iNet Factory Attachment Example";
body = "see attached image";
attach = "image.gif";
SmtpAttachmentExample example = new SmtpAttachmentExample();
example.sendMessage(hostname,to,from,subject,body,attach);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
|
|