sJava smtp attachments, java email, sending email with java
Home Search Knowledge Base Support

Support

Click here for access to live sales support.

For technical support please submit a ticket to the Help Desk.

 

Java and .NET Help

iPod shuffle offer

For a limited time get the newly re-designed iPod shuffle free with qualifying purchase.

Click for details.


News

JSCAPE Secure FTP Server 6.4 Released
01/18/2010 03:58 PM

JSCAPE Secure FTP Server 6.3 Released
12/14/2009 05:45 PM

Secure iNet Factory 8.3.1 Released
12/04/2009 04:26 PM

SSH Factory 3.5 Released
11/10/2009 01:46 PM

Secure iNet Factory 8.3 Released
11/10/2009 01:43 PM

Secure FTP Factory 8.3 Released
11/10/2009 01:37 PM

more...


Tutorials

Enabling Phone Authentication
04/08/2009 11:24 AM

Detecting and Handling Brute Force Password Attacks
01/29/2009 09:44 AM

Creating a Domain
12/15/2008 11:33 AM

Public key authentication with SFTP
10/02/2008 07:59 AM

Formatting MimeMessages using .NET
09/14/2008 04:31 PM

Communicating with an IMAP4 server in .NET
09/14/2008 03:54 PM

more...


Articles

Streamlining web uploads with ZIP archives
12/14/2009 10:11 AM

Using regular expressions in complex trigger conditions
09/08/2009 07:42 AM

Using custom forms to automate business processes
07/03/2009 08:51 AM

Public key authentication with SFTP
10/02/2008 07:59 AM

Best Practices for Configuring Your FTP Server
06/03/2008 04:47 PM

What is the difference between passive and active FTP?
05/20/2008 09:27 AM

more...


Feedback

Request a feature or component

Request a Java or .NET component


 

Sending email attachments using Java

Overview

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.

Sending attachments

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 attachmentthrows 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.

Summary

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.