160 likes | 451 Views
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 {
E N D
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 { public static void main(String args[]) throws IOException { Socket sock; DataInputStream dis; PrintStream ps;
// 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());
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(); } }
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
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;
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); }
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; }
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; }
public static void main(String a[]) { Mailer mailman = new Mailer("mm6@andrew.cmu.edu", "mccarthy@cs.pitt.edu", "It's hot"); mailman.send(); } }
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)