電子メールの送信
%telnet xxx 25
<220
#メール送信開始
>HELLO <domainName>
<250 ...
#送り主と宛先の指定
>MAIL FROM:<mail address>
<250 ..
>RCPT TO::<mail address>
<250 ..
#本文
>DATA..
<354
><message>
>.
<250 ..
#終了
>QUIT
public void sendMail() { try { Socket s = new Socket(jmaleServer.getText(), 25); out = new PrintWriter(s.getOutputStream()); in = new BufferedReader(new InputStreamReader(s.getInputStream())); String hostName = InetAddress.getLocalHost().getHostName(); receive(); send("HELO " + hostName); receive(); send("MAIL FROM: <" + jFromText1.getText() + ">"); receive(); send("RCPT TO: <" + jToText.getText() + ">"); receive(); send("DATA"); receive(); StringTokenizer tokenizer = new StringTokenizer( jMessagePane1.getText(), "\n"); while (tokenizer.hasMoreTokens()) send(tokenizer.nextToken()); send("."); receive(); s.close(); } catch (IOException exception) { jLogArea.append("Error: " + exception); } }
public void send(String s) throws IOException { textAreaComm.append(">"+s); textAreaComm.append("\n"); out.print(s); out.print("\r\n"); out.flush(); }
public void receive() throws IOException { String line = in.readLine(); if (line != null) { textAreaComm.append(line); textAreaComm.append("\n"); } }
package sendmailaplet; import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.*; import java.net.*; import java.io.*; public class Applet1 extends Applet { private BufferedReader in; private PrintWriter out; private boolean isStandalone = false; TextField textFieldFrom = new TextField(); Label label1 = new Label(); TextField textFieldTo = new TextField(); TextField textFieldServer = new TextField(); TextArea textAreaMessage = new TextArea(); TextArea textAreaComm = new TextArea(); Label label2 = new Label(); Label label3 = new Label(); Label label4 = new Label(); Label label5 = new Label(); Button buttonSend = new Button(); TextField textFieldSbj = new TextField(); Label label6 = new Label(); //Construct the applet public Applet1() { } //Initialize the applet public void init() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { textFieldFrom.setText("yt7m-itu@asahi-net.or.jp"); textFieldFrom.setBounds(new Rectangle(96, 18, 239, 18)); this.setLayout(null); label1.setText("from"); label1.setBounds(new Rectangle(36, 23, 61, 18)); textFieldTo.setText("yt7m-itu@asahi-net.or.jp"); textFieldTo.setBounds(new Rectangle(99, 40, 238, 20)); textFieldServer.setText("hm.aitai.ne.jp"); textFieldServer.setBounds(new Rectangle(101, 65, 169, 17)); textAreaMessage.setText("textArea1"); textAreaMessage.setBounds(new Rectangle(27, 132, 342, 92)); textAreaComm.setBounds(new Rectangle(25, 243, 346, 129)); label2.setText("message"); label2.setBounds(new Rectangle(23, 109, 86, 20)); label3.setText("communication"); label3.setBounds(new Rectangle(15, 223, 71, 21)); label4.setText("to"); label4.setBounds(new Rectangle(39, 39, 52, 20)); label5.setText("server"); label5.setBounds(new Rectangle(38, 66, 55, 18)); buttonSend.setLabel("send"); buttonSend.setBounds(new Rectangle(294, 71, 62, 33)); buttonSend.addActionListener(new Applet1_buttonSend_actionAdapter(this)); textFieldSbj.setText("subject"); textFieldSbj.setBounds(new Rectangle(99, 92, 168, 16)); label6.setText("subject"); label6.setBounds(new Rectangle(33, 91, 51, 16)); this.add(label1, null); this.add(textAreaComm, null); this.add(textAreaMessage, null); this.add(label2, null); this.add(label3, null); this.add(label4, null); this.add(textFieldServer, null); this.add(textFieldSbj, null); this.add(buttonSend, null); this.add(textFieldTo, null); this.add(label5, null); this.add(label6, null); this.add(textFieldFrom, null); } void buttonSend_actionPerformed(ActionEvent e) { try { Socket s = new Socket(textFieldServer.getText(), 25); out = new PrintWriter(s.getOutputStream()); in = new BufferedReader(new InputStreamReader(s.getInputStream())); String hostName = InetAddress.getLocalHost().getHostName(); receive(); send("HELO " + hostName); receive(); send("MAIL FROM: <" + textFieldFrom.getText() + ">"); receive(); send("RCPT TO: <" + textFieldTo.getText() + ">"); receive(); send("DATA"); receive(); send("Subject :" + textFieldSbj.getText()); send(""); StringTokenizer tokenizer = new StringTokenizer( textAreaMessage.getText(), "\n"); while (tokenizer.hasMoreTokens()) send(tokenizer.nextToken()); send("."); receive(); send("QUIT"); receive(); s.close(); } catch (IOException exception) { textAreaComm.append("Error: " + exception); } } public void send(String s) throws IOException { textAreaComm.append(">"+s); textAreaComm.append("\n"); out.print(s); out.print("\r\n"); out.flush(); } public void receive() throws IOException { String line = in.readLine(); if (line != null) { textAreaComm.append(line); textAreaComm.append("\n"); } } } class Applet1_buttonSend_actionAdapter implements java.awt.event.ActionListener { Applet1 adaptee; Applet1_buttonSend_actionAdapter(Applet1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.buttonSend_actionPerformed(e); } }
220 mira2.aitai.ne.jp ESMTP Mirapoint 3.3.3-GR; Sun, 6 Feb 2005 15:26:10 +0900 (JST) >HELO makoto 250 mira2.aitai.ne.jp Hello global221-28-032.aitai.ne.jp [221.118.28.32], pleased to meet you >MAIL FROM: <mito@ccad.sccs.chukyo-u.ac.jp> 250 <mito@ccad.sccs.chukyo-u.ac.jp>... Sender ok >RCPT TO: <yt7m-itu@asahi-net.or.jp> 250 <yt7m-itu@asahi-net.or.jp>... Recipient ok >DATA 354 Enter mail, end with "." on a line by itself >Subject :subject3 > >textArea1z >. 250 BYY09422 Message accepted for delivery >QUIT 221 mira2.aitai.ne.jp closing connection