Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Send simple text mails using JavaMail API

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 370
    Comment on it

           You can build build mail and messaging applications with the help of JavaMail API, which gives you access to a platform-independent and protocol-independent framework. You can use the JavaMail API as an optional package, but ensure to use with the Java SE platform. Java EE platform is also connected with it.

    Following are some of the protocols supported in JavaMail API:

    1. SMTP
    2. POP
    3. IMAP
    4. MIME
    5. NNTP

    If you want to send email through your program, all you need to do is download the following jar file(s).

    Sending mail through SMPT server
    SMTP server is must to send emails.
    Below are some techniques to get the SMTP server:

    • Install and use any SMTP server such as Postfix server, Apache James server
    • Use the SMTP server provided by the host provider for eg: free SMTP provide by JangoSMTP
      OR
      Use the SMTP Server provided by companies e.g. gmail, yahoo, etc.

      Steps to send email
      I am using JangoSMTP service which is free trail to use.
      Create your account on JangoMail and you are ready to go then.

    Create a java class file SendEmail, the contents of which are as follows:

    1. package yourPackage;
    2.  
    3. import java.util.Properties;
    4.  
    5. import javax.mail.Message;
    6. import javax.mail.MessagingException;
    7. import javax.mail.PasswordAuthentication;
    8. import javax.mail.Session;
    9. import javax.mail.Transport;
    10. import javax.mail.internet.InternetAddress;
    11. import javax.mail.internet.MimeMessage;
    12.  
    13. public class SendEmail {
    14. public static void main(String[] args) {
    15.  
    16. String to = "receiver@xyz.com"; // Recipient's email ID
    17.  
    18. String from = "sender@abc.com"; // Sender's email ID
    19. final String username = "username";//change accordingly (Put your User name here)
    20. final String password = "*******"; //change accordingly (Put your Password here)
    21.  
    22. // if sending mails through relay.jangosmtp.net
    23. String host = "relay.jangosmtp.net";
    24.  
    25. Properties props = new Properties();
    26. props.put("mail.smtp.auth", "true");
    27. props.put("mail.smtp.starttls.enable", "true");
    28. props.put("mail.smtp.host", host);
    29. props.put("mail.smtp.port", "25");
    30.  
    31. // Get the Session object.
    32. Session session = Session.getInstance(props,
    33. new javax.mail.Authenticator() {
    34. protected PasswordAuthentication getPasswordAuthentication() {
    35. return new PasswordAuthentication(username, password);
    36. }
    37. });
    38.  
    39. try {
    40. // Create a default MimeMessage object.
    41. Message message = new MimeMessage(session);
    42.  
    43. // Set From: header field of the header.
    44. message.setFrom(new InternetAddress(from));
    45.  
    46. // Set To: header field of the header.
    47. message.setRecipients(Message.RecipientType.TO,
    48. InternetAddress.parse(to));
    49.  
    50. // Set Subject: header field
    51. message.setSubject("Testing Mail");
    52.  
    53. // Now set the actual message
    54. message.setText("Hi there" +
    55. "Put your message body here.");
    56.  
    57. // Send message
    58. Transport.send(message);
    59.  
    60. System.out.println("Message Sent");
    61.  
    62. } catch (MessagingException e) {
    63. throw new RuntimeException(e);
    64. }
    65. }
    66. }

    Now compile the class with the downloaded jar file(s). and run

    If you see Message Sent in the console of your browser then the message is sent successfully.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: