Advertisement
Guest User

Untitled

a guest
Jun 4th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package javaapplication2;
  7.  
  8.  
  9. import javax.mail.Message;
  10. import javax.mail.MessagingException;
  11. import javax.mail.Session;
  12. import javax.mail.Transport;
  13. import javax.mail.internet.InternetAddress;
  14. import javax.mail.internet.MimeMessage;
  15. import javax.swing.*;
  16. import java.awt.*;
  17. import java.awt.event.ActionEvent;
  18. import java.awt.event.ActionListener;
  19. import java.util.Properties;
  20.  
  21. public class SendEmailClient extends JFrame {
  22. JTextField fromField = new JTextField();
  23. JTextField toField = new JTextField();
  24. JTextField subjectField = new JTextField();
  25. JComboBox mailServer = new JComboBox();
  26. JTextField usernameField = new JTextField();
  27. JPasswordField passwordField = new JPasswordField();
  28. JTextArea contentTextArea = new JTextArea();
  29.  
  30. public SendEmailClient() {
  31. InitializeUI();
  32. }
  33.  
  34. private void InitializeUI() {
  35. setTitle("Send E-mail Client");
  36. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  37. setSize(new Dimension(400, 280));
  38.  
  39. getContentPane().setLayout(new BorderLayout());
  40.  
  41. //
  42. // Header Panel
  43. //
  44. JPanel headerPanel = new JPanel();
  45. headerPanel.setLayout(new GridLayout(6, 2));
  46. headerPanel.add(new JLabel("From:"));
  47. headerPanel.add(fromField);
  48.  
  49. headerPanel.add(new JLabel("To:"));
  50. headerPanel.add(toField);
  51.  
  52. headerPanel.add(new JLabel("Subject:"));
  53. headerPanel.add(subjectField);
  54.  
  55. headerPanel.add(new JLabel("STMP Server:"));
  56. headerPanel.add(mailServer);
  57. mailServer.addItem("smtp.gmail.com");
  58.  
  59. headerPanel.add(new JLabel("Username:"));
  60. headerPanel.add(usernameField);
  61.  
  62. headerPanel.add(new JLabel("Password:"));
  63. headerPanel.add(passwordField);
  64.  
  65. //
  66. // Body Panel
  67. //
  68. JPanel bodyPanel = new JPanel();
  69. bodyPanel.setLayout(new BorderLayout());
  70. bodyPanel.add(new JLabel("Message:"), BorderLayout.NORTH);
  71. bodyPanel.add(contentTextArea, BorderLayout.CENTER);
  72.  
  73. //
  74. //
  75. //
  76. JPanel footerPanel = new JPanel();
  77. footerPanel.setLayout(new BorderLayout());
  78. JButton sendMailButton = new JButton("Send E-mail");
  79. sendMailButton.addActionListener(new SendEmailActionListener());
  80.  
  81. footerPanel.add(sendMailButton, BorderLayout.SOUTH);
  82.  
  83. getContentPane().add(headerPanel, BorderLayout.NORTH);
  84. getContentPane().add(bodyPanel, BorderLayout.CENTER);
  85. getContentPane().add(footerPanel, BorderLayout.SOUTH);
  86. }
  87.  
  88. public static void main(String[] args) {
  89. SwingUtilities.invokeLater(new Runnable() {
  90. @Override
  91. public void run() {
  92. SendEmailClient client = new SendEmailClient();
  93. client.setVisible(true);
  94. }
  95. });
  96. }
  97.  
  98. class SendEmailActionListener implements ActionListener {
  99. SendEmailActionListener() {
  100. }
  101.  
  102. @Override
  103. public void actionPerformed(ActionEvent e) {
  104. Properties props = new Properties();
  105. props.put("mail.smtp.host", mailServer.getSelectedItem());
  106. props.put("mail.transport.protocol", "smtp");
  107. props.put("mail.smtp.starttls.enable","true");
  108.  
  109. Session session = Session.getDefaultInstance(props);
  110.  
  111. try {
  112. InternetAddress fromAddress = new InternetAddress(fromField.getText());
  113. InternetAddress toAddress = new InternetAddress(toField.getText());
  114.  
  115. Message message = new MimeMessage(session);
  116. message.setFrom(fromAddress);
  117. message.setRecipient(Message.RecipientType.TO, toAddress);
  118. message.setSubject(subjectField.getText());
  119. message.setText(contentTextArea.getText());
  120.  
  121. Transport.send(message, usernameField.getText(),
  122. new String(passwordField.getPassword()));
  123. } catch (MessagingException ex) {
  124. ex.printStackTrace();
  125. }
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement