Новичок
Профиль
Группа: Участник
Сообщений: 1
Регистрация: 1.8.2008
Репутация: нет Всего: нет
|
Вообщем пытаюсь сделать простенький мэйл клиент для отправки почты...программно вроде все получается, а вот с интерфейсом у меня туго. Есть 2 класса, первый для интерфейса, 2 для отправки сообщений. Сделал простенький интерфейс, есть поля для ввода адресса и тд. Никак не могу склеить текст из полей ввода со 2 классом. Помогите. Код | import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.mail.MessagingException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class GoogleTest extends JFrame{ GoogleMessage message; private JLabel JLabel1 = new JLabel(); private JLabel JLabel2 = new JLabel(); private JLabel JLabel3 = new JLabel(); private JLabel JLabel4 = new JLabel(); private static JTextField _from = new JTextField(); private JTextField _to = new JTextField(); private JTextField _subject = new JTextField(); private JTextField _smtp = new JTextField(); private JTextArea ta = new JTextArea(50,50); private JScrollPane _scrollPane2 = new JScrollPane(ta); private static JButton Send = null; private static JButton Cancel = null; private JScrollPane _scrollPane = new JScrollPane(); private JList _output = new JList(); public GoogleTest() { setTitle("SendMail Example"); getContentPane().setLayout(null); setSize(736, 350); //labels JLabel1.setText("From:"); this.add(JLabel1); JLabel1.setBounds(12, 12, 36, 12); JLabel2.setText("To:"); this.add(JLabel2); JLabel2.setBounds(12, 48, 36, 12); JLabel3.setText("Subject:"); this.add(JLabel3); JLabel3.setBounds(12, 84, 48, 12); JLabel4.setText("SMTP Server:"); this.add(JLabel4); JLabel4.setBounds(12, 120, 84, 12); //textfields this.add(_from); _from.setBounds(96, 12, 300, 24); this.add(_to); _to.setBounds(96, 48, 300, 24); this.add(_subject); _subject.setBounds(96, 84, 300, 24); this.add(_smtp); _smtp.setBounds(96, 120, 300, 24); this.add(_scrollPane2); _scrollPane2.setBounds(12, 156, 384, 108); ta.setText("Enter your message here."); ta.setBounds(0, 0, 381, 105); Send = new JButton("Send"); Send.addActionListener( // on cree une classe anonyme implementant ActionListener new ActionListener() { public void actionPerformed(ActionEvent e) { // System.out.println("send"); try { message.sendMessage(); } catch (MessagingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } ); this.add(Send); Send.setBounds(60, 276, 132, 24); Cancel = new JButton("Cancel"); Cancel.addActionListener( // on cree une classe anonyme implementant ActionListener new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } } ); this.add(Cancel); Cancel.setBounds(216, 276, 120, 24); this.add(_scrollPane); _scrollPane.setBounds(408, 12, 312, 288); this.add(_output); _output.setBounds(408, 12, 309, 285); setVisible(true); } public static void main(String args[])throws MessagingException{ // Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); new GoogleTest(); } }
|
Код | import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class GoogleMessage { GoogleTest test; private static final String SMTP_HOST_NAME = "smtp.gmail.com"; private static final String SMTP_PORT = "465"; private static final String emailMsgTxt = "Test Message Contents"; private static final String emailSubjectTxt = "A test from gmail"; private static final String emailFromAddress = "*******@gmail.com"; private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; private static final String[] sendTo = { "*******@yandex.ru", "******@gmail.com"}; public GoogleMessage(String recipients[], String subject, String message, String from) throws MessagingException { //public static void sendSSLMessage(String recipients[], String subject, // String message, String from) throws MessagingException { Properties props = new Properties(); props.put("mail.smtp.host", SMTP_HOST_NAME); props.put("mail.smtp.auth", "true"); props.put("mail.debug", "true"); props.put("mail.smtp.port", SMTP_PORT); props.put("mail.smtp.socketFactory.port", SMTP_PORT); props.put("mail.smtp.socketFactory.class", SSL_FACTORY); props.put("mail.smtp.socketFactory.fallback", "false"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("*******", "*******"); } }); Message msg = new MimeMessage(session); InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { addressTo[i] = new InternetAddress(recipients[i]); } msg.setRecipients(Message.RecipientType.TO, addressTo); // Setting the Subject and Content Type msg.setSubject(subject); msg.setContent(message, "text/plain"); Transport.send(msg); } public static void sendMessage() throws MessagingException{ new GoogleMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress); System.out.println("Sucessfully Sent mail to All Users"); } }
|
|