Introduction
To send an e-mail using your Java Application is simple enough but to start with, you should have JavaMail API and Java Activation Framework (JAF) installed on your machine. We will see how to include these jar files in the projects. If you are using Netbean or Eclipse, then that is very simple. This is a simple app that may not have many features but it sets the basis for a good Desktop Email Sender App.
Package com.sun.mail.smtp Description
An SMTP protocol provider for the JavaMail API that provides access to an SMTP server. Refer to RFC 821 for more information.
When sending a message, detailed information on each address that fails is available in an SMTPAddressFailedException chained off the top level SendFailedException that is thrown. In addition, if the mail.smtp.reportsuccess
property is set, an SMTPAddressSucceededException will be included in the list for each address that is successful. Note that this will cause a top level SendFailedException to be thrown even though the send was successful.
Using the Code
Before you start the coding, it is important that you should have to do the following things:
- Download and unzip these files, in the newly created top level directories you will find a number of jar files for both the applications. You need to add mail.jar and activation.jar files in your
CLASSPATH
. These two jar files can be downloaded easily. Feel free.
Here are the two links:
- For JAF: http://www.oracle.com/technetwork/java/jaf11-139815.html
- For javaMail: http://www.oracle.com/technetwork/java/javamail/index.html
Code is quite simple. Just parse through it:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel7.setVisible(true);
String host="smtp.gmail.com";
final String user = jTextField1.getText();
final String password = new String(jPasswordField1.getPassword());
if(!user.equals("") && !password.equals(""))
{
String SMTP_PORT = "465";
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String to=jTextField3.getText();
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host",host);
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(user,password);
}
});
try
{
MimeMessage message = new MimeMessage(session);
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(jTextField5.getText());
message.setText(jTextPane1.getText());
Transport.send(message);
JOptionPane.showMessageDialog(null,"message sent successfully...");
jLabel7.setVisible(false);
}
catch (MessagingException e) {e.printStackTrace();}
}
else
{
JOptionPane.showMessageDialog(null,"Dear User! Please Enter Email or Password");
}
}
Points of Interest
There is an interesting thing that I also tried such an app in C#.NET. Making that is also a very simple. If you are planning to make a quality and good HCI following app build a Attachment portion in it. Many websites give a simple attachment code of variety. It may be true that you find another way for building such Desktop Mail Sender but it's also going to help you in that way. :)