Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Writing Simple Email Clients in Java Email Using Sockets Email using the JavaMail API."— 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 1.Download the JavaMail API Implementation Version 1.3ea 2.Download the JavaBeans Activation Framework 1.0.2ea 3.Unzip both files. 4.Add the following files to your classpath: –mail.jar (JavaMail) –activation.jar (JAF file) Visit http://java.sun.com/products/javamail


Download ppt "Writing Simple Email Clients in Java Email Using Sockets Email using the JavaMail API."

Similar presentations


Ads by Google