Simple example to send an email (without attachment ) using java application
Syntax :
Example :
- import javax.mail.Session;
- import javax.mail.Message;
- import javax.mail.Transport;
- import javax.mail.MessagingException;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.InternetAddress;
- import java.util.Properties;
- public class Main {
- public static void main(String[] args) {
- String from = "user@some-domain.com";
- String to = "user@some-domain.com";
- String subject = "Hi There...";
- String text = "How are you?";
- Properties properties = new Properties();
- properties.put("mail.smtp.host", "smtp.some-domain.com");
- properties.put("mail.smtp.port", "25");
- Session session = Session.getDefaultInstance(properties, null);
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress(from));
- message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
- message.setSubject(subject);
- message.setText(text);
- Transport.send(message);
- }
- }
Description :
Step 1 - 7 : Importing all files.
Step 9 : Start of main class
Step 16-19 :Properties class represents persistant set of properties.They can be saved to stream or loaded from stream.
Step 21-25 :Message implements part interface. Message contains set of attributes and "contents". Message defines new attributes in addition to those defined in part interface.