Showing posts with label Mail API. Show all posts
Showing posts with label Mail API. Show all posts

Monday, May 14, 2012

Send Email in Java Mail API using Gmail SMTP Server

7 comments
To send email using java you must have mail server and user name and password to access the mail server. In this tutorial I used gmail SMTP server with SSL connection. You must add Java Mail API to your class path.


package jsupport.mail;

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
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;

/**
 *
 * @author Java Srilankan Support - JSupport
 */
public class SendMail {
    
    public void sendMail(String m_from,String m_to,String m_subject,String m_body){

      try {

            m_properties     = new Properties();
            m_properties.put("mail.smtp.host", "smtp.gmail.com"); 
            m_properties.put("mail.smtp.socketFactory.port", "465");
            m_properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
            m_properties.put("mail.smtp.auth", "true");
            m_properties.put("mail.smtp.port", "465");
            

            m_Session        =   Session.getDefaultInstance(m_properties,new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("username","password"); // username and the password
                }

            });
            
            m_simpleMessage  =   new MimeMessage(m_Session);

            m_fromAddress    =   new InternetAddress(m_from);
            m_toAddress      =   new InternetAddress(m_to);


            m_simpleMessage.setFrom(m_fromAddress);
            m_simpleMessage.setRecipient(RecipientType.TO, m_toAddress);
            m_simpleMessage.setSubject(m_subject);
            m_simpleMessage.setContent(m_body,"text/plain");

            Transport.send(m_simpleMessage);

        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {

      SendMail send_mail    =   new SendMail();
      send_mail.sendMail("from-mail@gmail.com", "to-mail@gmail.com", "Test Mail", "Hi this is Test mail from Java Srilankan Support");
    }

    private Session m_Session;
    private Message m_simpleMessage;
    private InternetAddress m_fromAddress;
    private InternetAddress m_toAddress;
    private Properties m_properties;
}
Read more...

Saturday, March 24, 2012

How To Validate Email Address in Java Mail API

1 comments

Validate Email Address using Java Mail API


In this tutorial you will find how to validate email address using java Mail API. You must to import javax.mail.internet.InternetAddress 



package jsupport;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

/**
 *
 * @author Jsupport http://javasrilankansupport.blogspot.com
 */
public class ValidateEmailAddress {

    public static  boolean  validateEmail(String email) {
        try {
            new InternetAddress(email).validate();

        } catch (AddressException ex) {
            System.out.println("Error : " + ex.getMessage());
            return false;
        }
        return true;
    }

    public static void main(String[] args) {

        validateEmail("info@jsupport.com");

    }
}

Read more...

Tuesday, February 28, 2012

How to Validate Email Address using Java Pattern

8 comments


Validate an email address is easy if you have good valid pattern expression.   

package com.jsupport;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author JSupport
 */
public class ValidateEmailAddress {

    public boolean isValidEmailAddress(String emailAddress) {
        
        String expression = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
        CharSequence inputStr = emailAddress;
        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputStr);
        return matcher.matches();
        
    }

    public static void main(String[] args) {
        
        ValidateEmailAddress vea = new ValidateEmailAddress();

        System.out.println(vea.isValidEmailAddress("info@jsupport.info"));      // Valide emaill address
        System.out.println(vea.isValidEmailAddress("info@jsup@port.info"));     // Invalide emaill address
        System.out.println(vea.isValidEmailAddress("info@jsupport.in_fo"));     // Invalide emaill address
        System.out.println(vea.isValidEmailAddress("in@fo@jsupport.info"));     // Invalide emaill address
        System.out.println(vea.isValidEmailAddress("info@jsupport.com.lk"));    // Valide emaill address
    }
}


Here you will find how to validate email address using Java Mail API
Read more...