import javax.mail.*; import javax.mail.internet.*; import java.util.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; import sun.net.smtp.*; import java.io.*; import javax.swing.event.*; import javax.swing.border.*; public class MailSkickare extends JFrame{ private JButton send = new JButton("Send"); private JLabel froml = new JLabel("From: "); private JLabel tolabel = new JLabel("To: "); private JLabel hostl = new JLabel("Smtp Server: "); private JLabel subjectl = new JLabel("Subject: "); private JTextField fromf = new JTextField(40); private JTextField tofield = new JTextField(40); private JTextField hostf = new JTextField(40); private JTextField subjectf = new JTextField(40); private JTextArea message = new JTextArea(30, 20); private JScrollPane jsp = new JScrollPane(message); public MailSkickare(){ super("SMTP Client"); //Grafik med alla möjliga lyssnare läggs i en gridlayout till slut getContentPane().setLayout(new BorderLayout()); JPanel labels = new JPanel(); labels.setLayout(new GridLayout(4, 1)); hostl.setForeground(Color.black); labels.add(hostl); JPanel fields=new JPanel(); fields.setLayout(new GridLayout(4, 1)); String hostprop = System.getProperty("mail.host", ""); hostf.setText(hostprop); hostf.addFocusListener(new FocusAdapter(){ public void focusGained(FocusEvent e){ hostl.setForeground(new Color(98,156,245)); } }); hostf.addFocusListener(new FocusAdapter(){ public void focusLost(FocusEvent e){ hostl.setForeground(Color.black); } }); labels.add(tolabel); fields.add(hostf); //hostf.setText("mail.dsv.su.se"); tolabel.setForeground(Color.black); String fromprop = System.getProperty("mail.from", ""); fromf.setText(fromprop); tofield.addFocusListener(new FocusAdapter(){ public void focusGained(FocusEvent e){ tolabel.setForeground(new Color(98,156,245)); } }); tofield.addFocusListener(new FocusAdapter(){ public void focusLost(FocusEvent e){ tolabel.setForeground(Color.black); } }); fields.add(tofield); froml.setForeground(Color.black); labels.add(froml); fields.add(fromf); subjectl.setForeground(Color.black); labels.add(subjectl); subjectf.addFocusListener(new FocusAdapter(){ public void focusGained(FocusEvent e){ subjectl.setForeground(new Color(98,156,245)); } }); subjectf.addFocusListener(new FocusAdapter(){ public void focusLost(FocusEvent e){ subjectl.setForeground(Color.black); } }); fields.add(subjectf); Box north = Box.createHorizontalBox(); north.add(labels); north.add(fields); //font i textfältet!! message.setFont(new Font("SansSerif",Font.BOLD,14)); jsp.setBorder(new TitledBorder(new EtchedBorder(),"Mail Message")); JPanel p4=new JPanel(new FlowLayout()); send.setMnemonic('e'); p4.add(send); //anropa en ActionPerformed class:: send.addActionListener(new SendAction()); //lägg till komponenterna i "containern" getContentPane().add(north,BorderLayout.NORTH); getContentPane().add(jsp,BorderLayout.CENTER); getContentPane().add(p4,BorderLayout.SOUTH); setSize(600,500); setResizable(false); setVisible(true); //lägg allt i en rad!! addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } // metod för att sända meddelandet public void Send(){ String host = hostf.getText().trim(); String from = fromf.getText().trim(); String to = tofield.getText().trim(); String subject = subjectf.getText().trim(); try { Properties props = new Properties(); props.put("mail.smtp.host", host); Session mailConnection = Session.getInstance(props, null); final Message msg = new MimeMessage(mailConnection); //Address to = new InternetAddress(to); //Address from = new InternetAddress(from); msg.setContent(message.getText(), "text/plain" ); msg.setFrom(new InternetAddress(from)); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); msg.setSubject(subject); Runnable r = new Runnable() { public void run() { try{ Transport.send(msg); //skicka själv meddelandet } catch (Exception e) { e.printStackTrace(); //snygg utskrift vid fel :) } } }; Thread t = new Thread(r); t.start(); message.setText(""); System.out.println("Meddelandet skickat"); } catch (Exception e) { e.printStackTrace(); } } class SendAction implements ActionListener{ public void actionPerformed(ActionEvent e){ Send(); } } public static void main(String[] args){ MailSkickare mails = new MailSkickare(); mails.show(); } }