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 }