0% found this document useful (0 votes)
29 views

Mail - Java: Package Import Public Class

The Mail.java document defines a Mail class that creates a graphical user interface for composing and sending emails. It contains fields for the from, password, to, subject, and message text. It initializes the frame and adds labels, text fields, buttons for sending, clearing, and canceling the email. The EmailSender class contains the code for actually sending emails by connecting to an SMTP server and sending the message using the Java Mail API.

Uploaded by

RajMore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Mail - Java: Package Import Public Class

The Mail.java document defines a Mail class that creates a graphical user interface for composing and sending emails. It contains fields for the from, password, to, subject, and message text. It initializes the frame and adds labels, text fields, buttons for sending, clearing, and canceling the email. The EmailSender class contains the code for actually sending emails by connecting to an SMTP server and sending the message using the Java Mail API.

Uploaded by

RajMore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Mail.

java
package app;
import java.awt.EventQueue;
public class Mail
{
private
private
private
private
private

JFrame frmMail;
JTextField txtfrom;
JPasswordField pfpassword;
JTextField txtto;
JTextField txtsub;

public static void main(String[] args)


{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
Mail window = new Mail();
window.frmMail.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}

public Mail()
{
initialize();
}

private void initialize()


{
frmMail = new JFrame();
frmMail.setTitle("Mail");
frmMail.setBounds(100, 100, 392, 354);
frmMail.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpringLayout springLayout = new SpringLayout();
frmMail.getContentPane().setLayout(springLayout);
JLabel lblNewLabel = new JLabel("From");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel, 10,
SpringLayout.NORTH, frmMail.getContentPane());

springLayout.putConstraint(SpringLayout.WEST, lblNewLabel, 10,


SpringLayout.WEST, frmMail.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, lblNewLabel, 32,
SpringLayout.NORTH, frmMail.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, lblNewLabel, 62,
SpringLayout.WEST, frmMail.getContentPane());
frmMail.getContentPane().add(lblNewLabel);
txtfrom = new JTextField();
springLayout.putConstraint(SpringLayout.NORTH, txtfrom, 3,
SpringLayout.NORTH, lblNewLabel);
springLayout.putConstraint(SpringLayout.WEST, txtfrom, 59,
SpringLayout.EAST, lblNewLabel);
springLayout.putConstraint(SpringLayout.EAST, txtfrom, 299,
SpringLayout.EAST, lblNewLabel);
frmMail.getContentPane().add(txtfrom);
txtfrom.setColumns(10);
JLabel lblPassword = new JLabel("Password");
lblPassword.setFont(new Font("Tahoma", Font.BOLD, 14));
springLayout.putConstraint(SpringLayout.NORTH, lblPassword, 17,
SpringLayout.SOUTH, lblNewLabel);
springLayout.putConstraint(SpringLayout.WEST, lblPassword, 10,
SpringLayout.WEST, frmMail.getContentPane());
frmMail.getContentPane().add(lblPassword);
pfpassword = new JPasswordField();
springLayout.putConstraint(SpringLayout.NORTH, pfpassword, 13,
SpringLayout.SOUTH, txtfrom);
springLayout.putConstraint(SpringLayout.WEST, pfpassword, 0,
SpringLayout.WEST, txtfrom);
springLayout.putConstraint(SpringLayout.EAST, pfpassword, 0,
SpringLayout.EAST, txtfrom);
frmMail.getContentPane().add(pfpassword);
JLabel lblTo = new JLabel("To");
springLayout.putConstraint(SpringLayout.WEST, lblTo, 0,
SpringLayout.WEST, lblNewLabel);
lblTo.setFont(new Font("Tahoma", Font.BOLD, 14));
frmMail.getContentPane().add(lblTo);
txtto = new JTextField();
springLayout.putConstraint(SpringLayout.NORTH, lblTo, 0,
SpringLayout.NORTH, txtto);
springLayout.putConstraint(SpringLayout.NORTH, txtto, 16,
SpringLayout.SOUTH, pfpassword);
springLayout.putConstraint(SpringLayout.WEST, txtto, 0,
SpringLayout.WEST, txtfrom);
springLayout.putConstraint(SpringLayout.EAST, txtto, 0,
SpringLayout.EAST, txtfrom);
frmMail.getContentPane().add(txtto);
txtto.setColumns(10);
JLabel lblSubject = new JLabel("Subject");
springLayout.putConstraint(SpringLayout.NORTH, lblSubject, 20,
SpringLayout.SOUTH, lblTo);
springLayout.putConstraint(SpringLayout.WEST, lblSubject, 0,
SpringLayout.WEST, lblNewLabel);
lblSubject.setFont(new Font("Tahoma", Font.BOLD, 14));

frmMail.getContentPane().add(lblSubject);
txtsub = new JTextField();
springLayout.putConstraint(SpringLayout.NORTH, txtsub, 14,
SpringLayout.SOUTH, txtto);
springLayout.putConstraint(SpringLayout.WEST, txtsub, 0,
SpringLayout.WEST, txtfrom);
springLayout.putConstraint(SpringLayout.EAST, txtsub, 0,
SpringLayout.EAST, txtfrom);
frmMail.getContentPane().add(txtsub);
txtsub.setColumns(10);
JLabel lblMessage = new JLabel("Message");
lblMessage.setFont(new Font("Tahoma", Font.BOLD, 14));
springLayout.putConstraint(SpringLayout.NORTH, lblMessage, 19,
SpringLayout.SOUTH, lblSubject);
springLayout.putConstraint(SpringLayout.WEST, lblMessage, 0,
SpringLayout.WEST, lblNewLabel);
frmMail.getContentPane().add(lblMessage);
final JTextArea tamsg = new JTextArea();
springLayout.putConstraint(SpringLayout.NORTH, tamsg, 0,
SpringLayout.NORTH, lblMessage);
springLayout.putConstraint(SpringLayout.WEST, tamsg, 0,
SpringLayout.WEST, txtfrom);
springLayout.putConstraint(SpringLayout.SOUTH, tamsg, 103,
SpringLayout.SOUTH, txtsub);
springLayout.putConstraint(SpringLayout.EAST, tamsg, 0,
SpringLayout.EAST, txtfrom);
frmMail.getContentPane().add(tamsg);
JButton btnsend = new JButton("Send");
springLayout.putConstraint(SpringLayout.NORTH, btnsend, 92,
SpringLayout.SOUTH, lblMessage);
springLayout.putConstraint(SpringLayout.WEST, btnsend, 21,
SpringLayout.WEST, frmMail.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, btnsend, -22,
SpringLayout.SOUTH, frmMail.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, btnsend, -258,
SpringLayout.EAST, frmMail.getContentPane());
btnsend.addActionListener(new ActionListener(){
@SuppressWarnings("deprecation")
@Override
public void actionPerformed(ActionEvent e)
{
String[] to = {txtto.getText()};
if(EmailSender.sendMail(txtfrom.getText(),
pfpassword.getText(), tamsg.getText(), to,txtsub.getText()))
{
JOptionPane.showMessageDialog(null, "Mail sent
Successfully.");
}
else
{
System.out.println("Sending Failed.");
}
txtfrom.setText("");

pfpassword.setText("");
txtto.setText("");
txtsub.setText("");
tamsg.setText("");
txtfrom.requestFocus();
}
});
btnsend.setFont(new Font("Tahoma", Font.BOLD, 14));
frmMail.getContentPane().add(btnsend);
JButton btncancel = new JButton("Cancel");
springLayout.putConstraint(SpringLayout.NORTH, btncancel, 0,
SpringLayout.NORTH, btnsend);
springLayout.putConstraint(SpringLayout.SOUTH, btncancel, 0,
SpringLayout.SOUTH, btnsend);
springLayout.putConstraint(SpringLayout.EAST, btncancel, 0,
SpringLayout.EAST, txtfrom);
btncancel.setFont(new Font("Tahoma", Font.BOLD, 14));
frmMail.getContentPane().add(btncancel);
JButton btnclr = new JButton("Clear");
btnclr.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
txtfrom.setText("");
pfpassword.setText("");
txtto.setText("");
txtsub.setText("");
tamsg.setText("");
txtfrom.requestFocus();
}
});
springLayout.putConstraint(SpringLayout.WEST, btncancel, 26,
SpringLayout.EAST, btnclr);
springLayout.putConstraint(SpringLayout.NORTH, btnclr, 0,
SpringLayout.NORTH, btnsend);
springLayout.putConstraint(SpringLayout.WEST, btnclr, 26,
SpringLayout.EAST, btnsend);
springLayout.putConstraint(SpringLayout.SOUTH, btnclr, 0,
SpringLayout.SOUTH, btnsend);
springLayout.putConstraint(SpringLayout.EAST, btnclr, 117,
SpringLayout.WEST, tamsg);
btnclr.setFont(new Font("Tahoma", Font.BOLD, 14));
frmMail.getContentPane().add(btnclr);
}
}

EmailSender.java
package app;
import
import
import
import
import
import

java.util.Properties;
javax.mail.Message;
javax.mail.MessagingException;
javax.mail.Session;
javax.mail.Transport;
javax.mail.internet.*;

public class EmailSender


{
public static boolean sendMail(String from,String password,String
message,String to[],String subject)
{
String host= "smtp.gmail.com";
Properties props= System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", 587);
props.put("mail.smtp.auth", "true");
Session session= Session.getDefaultInstance(props,null);
MimeMessage mimeMessage=new MimeMessage(session);
try
{
mimeMessage.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
for(int i=0;i<to.length;i++)
{
toAddress[i]= new InternetAddress(to[i]);
}
for(int i=0;i<toAddress.length;i++)
{
mimeMessage.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
mimeMessage.setSubject(subject);
mimeMessage.setText(message);
Transport transport= session.getTransport("smtp");
transport.connect(host,from,password);
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
transport.close();
return true;
}
catch(MessagingException me)
{
me.printStackTrace();
}
return false;
}
}

You might also like