Hi!
Ich möchte einen smtp-Client programmieren und häng momentan irgendwie!
Die GUI ist soweit fertig!
Das Programm soll mit dem Server kommunizieren und dabei soll der gesamte Dialog in der TextArea output ausgegeben werden!
Ziel ist es, dass ich mit diesem Email-Client eine Email wegschicken kann!
Es happert momentan eigentlich nur mehr an den Input- und Outputstreams!
Die Ausgabe soll in etwa so aussehen:
1: > 220 mail.example.com SMTP Foo Mailserver
2: < HELO mail.example.org
3: > 250 Ok
4: < MAIL FROM: hans.muster@example.org
5: > 250 Ok
6: < RCPT TO: foo@example.com
7: > 250 Ok
8: < DATA
9: > 354 End data with .
10: < From: hans.muster@example.org
11: < To: foo@example.com
12: < Subject: Testmail
13: <
14: < Testmail
15: < .
16: > 250 Ok
17: < QUIT
18: > 221 Bye
Momentan hängt sich mein Programm immer auf...irgendwas stimmt da nicht!
Weiß momentan nicht, wies weitergeht! :bahnhof:
Vielleicht kann mir jemand helfen bitte! (wenn er Zeit und Lust hat) :bae:
DANKE!
Ich möchte einen smtp-Client programmieren und häng momentan irgendwie!
Die GUI ist soweit fertig!
Das Programm soll mit dem Server kommunizieren und dabei soll der gesamte Dialog in der TextArea output ausgegeben werden!
Ziel ist es, dass ich mit diesem Email-Client eine Email wegschicken kann!
Es happert momentan eigentlich nur mehr an den Input- und Outputstreams!
Die Ausgabe soll in etwa so aussehen:
1: > 220 mail.example.com SMTP Foo Mailserver
2: < HELO mail.example.org
3: > 250 Ok
4: < MAIL FROM: hans.muster@example.org
5: > 250 Ok
6: < RCPT TO: foo@example.com
7: > 250 Ok
8: < DATA
9: > 354 End data with .
10: < From: hans.muster@example.org
11: < To: foo@example.com
12: < Subject: Testmail
13: <
14: < Testmail
15: < .
16: > 250 Ok
17: < QUIT
18: > 221 Bye
Momentan hängt sich mein Programm immer auf...irgendwas stimmt da nicht!
Weiß momentan nicht, wies weitergeht! :bahnhof:
Vielleicht kann mir jemand helfen bitte! (wenn er Zeit und Lust hat) :bae:
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;
class SmtpClient extends JFrame implements ActionListener{
private JPanel oben;
private JPanel unten;
private JPanel a;
private JPanel b;
private JPanel c;
private JLabel labsender;
private JLabel labrec;
private JLabel labtopic;
private JTextField sender;
private JTextField receiver;
private JTextField topic;
private JButton send;
private JTextArea input;
private JTextArea output;
private JScrollPane pane1;
private JScrollPane pane2;
public SmtpClient(String titel){
super(titel);
Container c=getContentPane();
c.setLayout(new GridLayout(2,1,5,5));
c.setSize(750,500);
c.setBackground(Color.black);
oben=new JPanel(new BorderLayout(5,5));
oben.setBackground(Color.black);
a=new JPanel();
a.setBackground(Color.lightGray);
labsender=new JLabel("Sender: ");
sender=new JTextField(15);
labrec=new JLabel("Receiver:");
receiver=new JTextField(15);
labtopic=new JLabel("Topic:");
topic=new JTextField(15);
send=new JButton("Send");
send.addActionListener(this);
a.add(labsender);
a.add(sender);
a.add(labrec);
a.add(receiver);
a.add(labtopic);
a.add(topic);
a.add(send);
b=new JPanel();
b.setBackground(Color.white);
JTextArea input=new JTextArea(9,65);
pane1=new JScrollPane(input);
pane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
b.add(pane1);
c=new JPanel(new FlowLayout(FlowLayout.LEFT));
c.setBackground(Color.lightGray);
JLabel t=new JLabel("Geben Sie bitte im Feld darunter ihre Nachricht ein:");
c.add(t);
unten=new JPanel();
unten.setBackground(Color.white);
output=new JTextArea(13,65);
pane2=new JScrollPane(output);
pane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
unten.add(pane2);
add(oben);
oben.add(a, BorderLayout.NORTH);
oben.add(c, BorderLayout.CENTER);
oben.add(b, BorderLayout.SOUTH);
add(unten);
}
public void actionPerformed(ActionEvent e) {
String senderName=sender.getText();
String receiverName=receiver.getText();
SocketConnection con=new SocketConnection();
if(e.getSource()==send){
String ausgabe;
try {
ausgabe = con.getCommunication(senderName,receiverName);
output.setText(ausgabe);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
SmtpClient client=new SmtpClient("SMTP Mail Client");
client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.setSize(750,500);
client.setLocation(300,300);
client.setVisible(true);
}
}
Code:
import java.io.*;
import java.net.*;
public class SocketConnection {
String hostname="www.email.uni-linz.ac.at";
public String getCommunication(String sender, String receiver) throws IOException{
try{
InetAddress inet=InetAddress.getByName(hostname);
}catch(UnknownHostException ex) {
return "Invalid server name: UnknownHostException " + ex.getMessage();
}
Socket socket;
try{
socket=new Socket(hostname,25);
}catch(IOException ex) {
return "Could not create socket: IOException " + ex.getMessage();
}
PrintWriter out;
BufferedReader in;
try {
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException ex) {
return "Could not open streams: IOException " + ex.getMessage();
}
StringBuffer sb=new StringBuffer();
int cnt=0;
try{
String ausgabe="";
ausgabe=in.readLine();
sb.append(cnt);
sb.append(":");
while(in!=null){
sb.append(ausgabe);
sb.append("\n");
cnt++;
}
}catch(IOException e){
return "Could not read from connection: IOException " + e.getMessage();
}
try {
// close socket (streams are closed automatically)
socket.close();
} catch (IOException e) {
return "Could not close connection: IOException " + e.getMessage();
}
return sb.toString();
}
}