Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Java Mail API : Sending Simple Mail

    • 0
    • 2
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 3.03k
    Comment on it

    Java MAil API composed of classes and some Interfaces which is used to send(compose) , read(fetch) and delete e-mail messages

    Sending Simple Text Mail:

    To send a simple Text Mail you can follow the following steps:

    1. Get a Session object which handles configuration setting and authentication and stores all the information of host like host name, username, password etc.

    2. Create a default MimeMessage object and set the "From","To" attributes & subject of this message.

    3. Write and set your actual message as: message.setText("Message Text");

    4. Send the message using the Transport object.

    Example using Gmail SMTP Server:-

    package mail;
    
    import java.util.Properties;
    
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    public class MailTestGmail {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
              // Specifying Recipient's email ID 
              String To = "xxx@xxx.com";//change accordingly
    
              // Specifying Sender's email ID
              String From = "xxx@xxx.com";//change accordingly
              final String username = "xxx@xxx.com";//change accordingly
              final String password = "*****";//change accordingly
    
              // Set up the SMTP server
              String host = "smtp.gmail.com";
    
              Properties props = new Properties();
              props.put("mail.smtp.auth", "true");
              props.put("mail.smtp.starttls.enable", "true");
              props.put("mail.smtp.host", host);
              props.put("mail.smtp.port", "587");
    
           // Get the Session object to handle both configuration setting and authentication
    
            Session session = Session.getInstance(props,
              new javax.mail.Authenticator() {
                 protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                 }
              });
    
              try {
                 // Create a default MimeMessage object.
                 Message msz = new MimeMessage(session);
    
                 // Set the "From" attribute in this Message
                 msz.setFrom(new InternetAddress(From));
    
                 // Set the "To" attribute in this Message
                 msz.setRecipients(Message.RecipientType.TO,
                 InternetAddress.parse(To)); //   You can add multiple recipients by adding their mail address here
    
                 // Set the subject of this message
                 msz.setSubject("Testing Subject");
    
                 // Now Write and set the actual message
                 msz.setText("Hello, this is sample text for to check send "
                    + "using JavaMailAPI ");
    
                      // Send  the message by send method
                 Transport.send(msz);
    
                 System.out.println("Message Sent successfully....");
              } catch (MessagingException e) {
                    throw new RuntimeException(e);
              }
        }
    
    }
    
    

    Refrence link

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: