Java mail

JavaMail Projects

JavaMail Project 1

How Can Send Your First Message Program By Using JavaMail API
Previous Home Next

If you want to send Message then using following steps:

  1. firstly follow the total task of JavaMail API Setup implementation.
  2. Then write a Skelton code

    Skeleton code:

    FirstMailProgram.java

    import java.util.Properties;
    import javax.mail.*;
    import javax.mail.internet.*;
    public class FirstMailProgram {
      public static void main (String args[]) throws Exception {
        String host = args[0];
        String from = args[1];
        String to = args[2];
        // Get system properties
        // Setup mail server
        // Get session
        // Define message
        // Set the from address
        // Set the to address
        // Set the subject
        // Set the content
        // Send message
      }
    }
    
  3. To start Skeleton Code and then get the system Properties. To get the

    Properties props - sytem.get properties();

  4. After the Skeleton Code add the name of your SMTP server to the properties for the mail.smtp.host key.

    props. put ("mail.smtp.host",host);

  5. When you add the name of your SMTP server after that Get a Session object based on the Properties.

    Session session -Session.getDefaultInstance(props,null)

    In this situation the session class is defines a basic mail session. And the session object takes the advantages of a java.util.Properties. The object to get information like mail server, username, password, and other information that can be shared across your entire application.

  6. To Create a MimeMessage from the session. In this session create a object based for MimeMessage.

    MimeMessage message- new MimeMessage (session);

  7. Then set the total form field of the message. message.setFrom(new InternetAddress(from)); If you have message then you can set the parts , and set the message implement the parts interface. and the basic mechanism is set the content of SetContent () method , and the argument is content for Mime type. message.SetContent("Hellor4r");

    If using plain text then you use method is setText() method which only requires the actual content.

    message.setText("Hello r4r");

  8. Now set the to field of the message

    message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

  9. When you set to field after that set the message for plain Text. To the message sending for other languages like as HTML messages. then setting the subject for using setSubject() method.

    message.setSubject("Hello R4R");

  10. If you select the messages then select the content of this messages then using method is

    message.setText("Welcome to r4rtechsoft");

  11. The total content write or Gathered then Transport the messages so you using the method for Transport

    Transport.sent(messages);

    The Final part of the messages is the Transport class. The class Speaks the protocol specific messges(SMTP usually). It is just like work for abstract class and working is Session. then you can using the default version of the class just calling the static send() method.

    Transport.send(message);

  12. Finally Compile the program, and passing your SMTP server,from address and to address on the command line

    java MailExample SMTP.Server from@address to@address previous

Previous Home Next