Tutorial List
Home
Interview Questions
Interview
Interview Questions
Links
Web Home
About Us

Send Email From Java

This Method is used to send Email from Private Email Server. These code are used when need email to be sent from your application. If you get some exception, just add activation.jar, mail.jar and remove j2ee.jar in class path.
**************************************************************************
/**Method: sendEmail
* Send Email to given email address in HTML format
* @param toEmail Recipient Email 
* @param subjectStr Email subjet
* @param bodyStr HTML Body String 
* @return boolean 
* @throws UnsupportedEncodingException
* @throws MessagingException
*/
public static boolean sendEmail(String toEmail, String subjectStr,
   String bodyStr) throws UnsupportedEncodingException, MessagingException 
{
   boolean isSent = true;
   Properties props = new Properties();
   // ask IT People for company mail server
   String mailHost = "Your Company Email Server"
   String mailSender = "Your Office Mail ID";
   String malSenderName = "Your Email";
   props.put("mail.host", mailHost);
   props.put("mail.user", mailSender);
   Session mailSession = Session.getDefaultInstance(props, null);
   MimeMessage msg = new MimeMessage(mailSession); 
   Multipart multipart = new MimeMultipart();
   BodyPart messageBodyPart = new MimeBodyPart();
   messageBodyPart.setContent(bodyStr, "text/html");
   multipart.addBodyPart(messageBodyPart);
   msg.setFrom(new InternetAddress(mailSender, malSenderName));
   msg.addRecipient(Message.RecipientType.TO,
   new InternetAddress(toEmail));
   msg.setSubject(subjectStr);
   msg.setContent(multipart);
   Transport.send(msg);
   return isSent;
}
***********************************************************************
If you want to send email from public Mail Server like Gmail, Following method will do well for you, Just replace sender and receiver in program with your desired values. You need to provide your authentication details in program as you are talking to external server. Another thing is, just add activation.jar, mail.jar and remove j2ee.jar in class path.
**********************************************************************************
package codinguide.com;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
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 EmailSender {
 
   /*Main method to test the program*/  
   public static void main(String[] args) 
   {  
      EmailSender m = new EmailSender();  
      m.sendEmail();  
   }
   /*Method to send Email*/
 
   public void sendEmail() 
   {  
      String from = "xxxx@xxxx.com";  
      String to = "xxxxx.xxx@gmail.com";  
      Properties props = System.getProperties();  
      props.put("mail.smtp.host", "smtp.gmail.com");  
      props.put("mail.smtp.auth", "true");  
      props.put("mail.smtp.starttls.enable", "true");  
props.put("mail.smtp.socketFactory.port", "465");  
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false");  
      Authenticator auth = new SMTPAuthenticator();  
      Session session = Session.getInstance(props, auth);  
      try {  
           Message msg = new MimeMessage(session);  
           msg.setFrom(new InternetAddress(from));  
           InternetAddress[] address = { new InternetAddress(to) };  
           msg.setRecipients(Message.RecipientType.TO, address);
 msg.setSubject("Welcome To Codinguide");    
           msg.setSentDate(new Date());  
           msg.setText("This mail is sent from Codinguide Program");
 Transport.send(msg);   
        } catch (MessagingException mex) {  
    }  
}  
/*Custom Authenticator*/  
private class SMTPAuthenticator extends javax.mail.Authenticator
{  
   public PasswordAuthentication getPasswordAuthentication() 
   {  
      String username = "XXXXUser Name";  
      String password = "XXXXPassword";  
      return new PasswordAuthentication(username, password);  
   }  
}
}

***********************************************************************
You can use above code in your program to save your time and efforts. Just get a basic understanding how Java Mail API works and no one will doubt your excellence in Java.


Thanks,
Mohit Singh

No comments: