Sending email attachments using Java
This article will demonstrate how to send an email message that
includes attachments using the components provided in Secure iNet Factory.
To see what else Secure iNet Factory has to offer Download
a FREE 30 day Secure iNet Factory Evaluation.
Using the Secure iNet Factory components adding file attachments to
your outbound email messages is a very simple task. The process
for adding file attachments to your email message is described
in the code example provided below.
01 /*
02 * @(#)SmtpAttachmentExample.java
03 *
04 * Copyright (c) 2001-2003 JSCAPE
05 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
06 * All rights reserved.
07 *
08 * This software is the confidential and proprietary information of
09 * JSCAPE. ("Confidential Information"). You shall not disclose such
10 * Confidential Information and shall use it only in accordance with
11 * the terms of the license agreement you entered into with JSCAPE.
12 */
13
14 import com.jscape.inet.smtp.*;
15 import com.jscape.inet.mime.*;
16 import com.jscape.inet.email.*;
17 import java.io.*;
18
19 public class SmtpAttachmentExample extends SmtpAdapter {
20 public void sendMessage(String hostname, String to, String from, String subject, String body, String attachment) throws SmtpException,
21 IOException, MimeException {
22
23 // create new Smtp instance
24 Smtp smtp = new Smtp(hostname);
25
26 // enable debugging ... all output is sent to System.out
27 smtp.setDebug(true);
28
29 // capture SMTP related events
30 smtp.addSmtpListener(this);
31
32 // connect to mail server
33 smtp.connect();
34
35 // create new email message
36 EmailMessage email = new EmailMessage();
37
38 // set recipient address
39 email.setTo(to);
40
41 // set from address
42 email.setFrom(from);
43
44 // set subject
45 email.setSubject(subject);
46
47 // set body
48 email.setBody(body);
49
50 // add file attachment to message
51 email.addAttachment(new Attachment(new File(attachment)));
52
53 // send email message
54 smtp.send(email);
55
56 // disconnect from SMTP server
57 smtp.disconnect();
58 }
59
60 // capture connect event
61 public void connected(SmtpConnectedEvent evt) {
62 System.out.println("Connected to Smtp server: " + evt.getHostname());
63 }
64
65 // capture disconnect event
66 public void disconnected(SmtpDisconnectedEvent evt) {
67 System.out.println("Disconnected from Smtp server: " + evt.getHostname());
68 }
69
70 public static void main(String[] args) {
71 String hostname;
72 String to;
73 String from;
74 String subject;
75 String body;
76 String attach;
77 try {
78 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
79 System.out.print("Enter Smtp hostname (e.g. mail.domain.com): ");
80 hostname = reader.readLine();
81 System.out.print("Enter To address (e.g. recipient@domain.com): ");
82 to = reader.readLine();
83 System.out.print("Enter From address (e.g. sender@domain.com): ");
84 from = reader.readLine();
85 subject = "iNet Factory Attachment Example";
86 body = "see attached image";
87 attach = "image.gif";
88 SmtpAttachmentExample example = new SmtpAttachmentExample();
89 example.sendMessage(hostname,to,from,subject,body,attach);
90 }
91 catch(Exception e) {
92 e.printStackTrace();
93 }
94 }
95 }
- Lines 14-17. Add necessary import statements. In this example
the com.jscape.inet.smtp.Smtp, com.jscape.inet.email.EmailMessage
and com.jscape.inet.mime.Attachment classes are used.
- Line 24. Create a new Smtp instance passing the SMTP (email)
server hostname as its argument.
- Line 30. Register this class to capture SMTP related events.
The SmtpConnectedEvent and SmtpDisconnectedEvent are captured
in the SmtpAttachmentExample#connected and SmtpAttachmentExample#disconnected
methods respectively.
- Line 33. Establish a connection with the SMTP server.
- Line 36. Create a new EmailMessage instance to contain the
contents of the email message.
- Lines 39-48. Address the message and set the message subject
and body.
- Line 51. Add a file attachment to the email message using
the path of the attachment variable passed in the SmtpAttachmentExample#sendMessage
method. To add multiple attachments you would invoke the EmailMessage#addAttachment
method for each attachment you wish to send.
- Line 54. Send email message to SMTP server.
- Line 57. Disconnect from SMTP server.
- Line 70. main() method to run this example, prompt user
for necessary inputs and invoke SmtpAttachmentExample#sendMessage
method.
In this article you learned how to send an email message containing
one or more attachments. The SMTP and MIME components in Secure iNet
Factory make this easy removing the complexities of the SMTP and
MIME protocols. To see what else Secure iNet Factory has to offer Download
a FREE 30 day Secure iNet Factory Evaluation.
|