1 / 11

Writing Simple Email Clients in Java

Writing Simple Email Clients in Java. Email Using Sockets Email using the JavaMail API. Email Using Sockets (Linden Chapter 17). // See page 571 of Linden's Just Java Fifth Edition import java.io.*; import java.net.*; public class SimpleEmailClient {

bern
Download Presentation

Writing Simple Email Clients in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Writing Simple Email Clients in Java • Email Using Sockets • Email using the JavaMail API

  2. Email Using Sockets (Linden Chapter 17) // See page 571 of Linden's Just Java Fifth Edition import java.io.*; import java.net.*; public class SimpleEmailClient { public static void main(String args[]) throws IOException { Socket sock; DataInputStream dis; PrintStream ps;

  3. // open a socket to an email server on port 25 sock = new Socket("cyrus.andrew.cmu.edu",25); dis = new DataInputStream(sock.getInputStream()); ps = new PrintStream(sock.getOutputStream()); // talk email ps.println("mail from: mm6@andrew.cmu.edu"); System.out.println(dis.readLine()); String Addressee = "mm6@andrew.cmu.edu"; ps.println("rcpt to: " + Addressee); System.out.println(dis.readLine());

  4. ps.println("data"); System.out.println(dis.readLine()); ps.println("A message sent by a Java client using sockets"); ps.println("."); System.out.println(dis.readLine()); ps.flush(); sock.close(); } }

  5. Output Echos from Cyrus D:\McCarthy\www\95-713\JavaMail>java SimpleEmailClient 220-mail-fe2.andrew.cmu.edu ESMTP Sendmail 8.12.3.Beta2/8.12.3. Beta2 220-Mis-identifing the sender of mail is an abuse of computing facilites. 220 ESMTP spoken here 250 2.1.0 mm6@andrew.cmu.edu... Sender ok

  6. Using JavaMail // A simple example using JavaMail import java.io.*; import java.net.InetAddress; import java.util.Properties; import java.util.Date; import javax.mail.*; import javax.mail.internet.*; public class Mailer { String to, from, msg;

  7. public Mailer(String to, String from, String msg) { this.to = to; this.from = from; this.msg = msg; } public void send() { Session s = createSession(); try { Message mess = createMessage(s, msg); Transport.send(mess); } catch(MessagingException e) { System.out.println("Messaging Exception: "+e); }

  8. catch(Exception e ) { System.out.println("Exception thrown" + e); } } private Session createSession() { Properties p = System.getProperties(); p.setProperty("mail.transport.protocol", "smtp"); p.setProperty("mail.smtp.host","cyrus.andrew.cmu.edu"); Session sess = Session.getDefaultInstance(p); return sess; }

  9. private Message createMessage(Session sess, String msg)throws MessagingException{ Message mess = new MimeMessage(sess); mess.setFrom(new InternetAddress(from)); mess.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); mess.setSubject("Warning"); mess.setText(msg); mess.setSentDate(new Date()); return mess; }

  10. public static void main(String a[]) { Mailer mailman = new Mailer("mm6@andrew.cmu.edu", "mccarthy@cs.pitt.edu", "It's hot"); mailman.send(); } }

  11. Installation Instructions Visit http://java.sun.com/products/javamail • Download the JavaMail API Implementation Version 1.3ea • Download the JavaBeans Activation Framework 1.0.2ea • Unzip both files. • Add the following files to your classpath: • mail.jar (JavaMail) • activation.jar (JAF file)

More Related