Sending email attachments using Java and SMTP


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.

Example

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 }

  1. 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.
  2. Line 24. Create a new Smtp instance passing the SMTP (email) server hostname as its argument.
  3. 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.
  4. Line 33. Establish a connection with the SMTP server.
  5. Line 36. Create a new EmailMessage instance to contain the contents of the email message.
  6. Lines 39-48. Address the message and set the message subject and body.
  7. 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.
  8. Line 54. Send email message to SMTP server.
  9. Line 57. Disconnect from SMTP server.
  10. Line 70. main() method to run this example, prompt user for necessary inputs and invoke SmtpAttachmentExample#sendMessage method.

Comments

Popular posts from this blog

Integrating ZXing QR Code reader in iPhone / iOS applications

Multiple line of text in UIPickerView

Connect Samsung devices to Kies on Mac