Subscribe For Free Updates!

We'll not spam mate! We promise.

Dec 4, 2013

Send Email in Java

Views:

Today I will share how to Send Email in java programming language.

In this post I use G mail SMTP to send Email.









So now lets starts,

open Eclipse or Net bean File->New->Project (name:EmailTest). Now add new Class Name EmailTest.java with Pakage Name Email.
public static void send (String from, String to, String cc,String bcc, String subject, String text,String user_id, String pass,String host1,int port1){ //String host = "smtp.gmail.com"; String userid = user_id; String password = pass; System.out.println(from); System.out.println(to); System.out.println(subject); System.out.println(text); System.out.println(user_id); System.out.println(host1); System.out.println(port1); try { Properties props = System.getProperties(); props.put("mail.smtp.auth", "false"); props.put("mail.smtp.starttls.enable", "false"); props.put("mail.smtp.host", host1); props.put("mail.smtp.port", port1); Session session = Session.getDefaultInstance(props, null); MimeMessage message = new MimeMessage(session); InternetAddress fromAddress = null; InternetAddress toAddress = null; InternetAddress ccAddress = null; InternetAddress bccAddress = null; if(cc!=null && cc.contains(",") ) { try { String cc_arr[] = cc.split(","); InternetAddress ccAddresses[] = new InternetAddress[cc_arr.length]; for (int i=0; i<cc_arr.length; i++) { ccAddresses[i] = new InternetAddress(cc_arr[i]); } message.setRecipients(RecipientType.CC, ccAddresses); } catch (AddressException e) { e.printStackTrace(); } } else { try { if (cc != null){ ccAddress = new InternetAddress(cc); message.setRecipient(RecipientType.CC, ccAddress); } } catch (AddressException e) { e.printStackTrace(); } } if( bcc!=null && bcc.contains(",")) { try { String bcc_arr[] = bcc.split(","); InternetAddress bccAddresses[] = new InternetAddress[bcc_arr.length]; for (int i=0; i<bcc_arr.length; i++) { bccAddresses[i] = new InternetAddress(bcc_arr[i]); } message.setRecipients(RecipientType.BCC, bccAddresses); } catch (AddressException e) { e.printStackTrace(); } } else { try { if (bcc != null) { bccAddress = new InternetAddress(bcc); message.setRecipient(RecipientType.BCC, bccAddress); } } catch (AddressException e) { e.printStackTrace(); } } fromAddress = new InternetAddress(from); toAddress = new InternetAddress(to); message.setFrom(fromAddress); message.setRecipient(RecipientType.TO, toAddress); message.setSubject(subject); //message.setText(text, "UTF-8", "html"); message.setContent(text, "text/html; charset=utf-8"); Transport transport = session.getTransport("smtp"); transport.send(message); transport.close(); System.out.println("Done"); } catch (MessagingException e) { e.printStackTrace(); } }
in Main method
public static void main(String[] args) { // TODO Auto-generated method stub //testemail(); send("from_address@gmail.com", //from "to_address@gmail.com",//to null , //cc "bcc_address@gmail.com" ,//bcc "testing", "<h2>Test email</h2>", "gmail_address@gmail.com", "gmail_pass", "smtp.gmail.com", 587); }
you can download Sample Code from Here

Plese Feel Free to Socializer This Post
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment

Become a Fan

visual studio learn