A previous post on [Plain Text] Sending E-Mail using SSL (Java) : Gmail Demonstration might be helpful before reading this one.
[Source: www.journaldev.com]
Here is the (modified) code:
Althought this is just a Command Line E-Mail client but using this is fun sometimes -.-
[sendAttachmentEmail method added for attachments]
[Note: MIME Read this if you don't understand the (MIME) code below]
[Source: www.journaldev.com]
Here is the (modified) code:
Althought this is just a Command Line E-Mail client but using this is fun sometimes -.-
[sendAttachmentEmail method added for attachments]
[Note: MIME Read this if you don't understand the (MIME) code below]
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Properties; import javax.activation.*; import javax.mail.*; import javax.mail.internet.*; class EmailUtil { /** * Utility method to send simple HTML email * * @param session * @param toEmail * @param subject * @param body */ public static void sendEmail(Session session, String toEmail, String subject, String body) { try { MimeMessage msg = new MimeMessage(session); //set message headers msg.addHeader("Content-type", "text/HTML; charset=UTF-8"); msg.addHeader("format", "flowed"); msg.addHeader("Content-Transfer-Encoding", "8bit"); msg.setFrom(new InternetAddress("abc@xyz.com", "NoReply-JD")); msg.setReplyTo(InternetAddress.parse("abc@xyz.com", false)); msg.setSubject(subject, "UTF-8"); msg.setText(body, "UTF-8"); msg.setSentDate(new Date()); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false)); System.out.println("\nSending Mail Now..."); Transport.send(msg); System.out.println("\nEMail Sent Successfully!"); } catch (Exception e) { e.printStackTrace(); } } public static void sendAttachmentEmail(Session session, String toEmail, String subject, String body){ try{ MimeMessage msg = new MimeMessage(session); msg.addHeader("Content-type", "text/HTML; charset=UTF-8"); msg.addHeader("format", "flowed"); msg.addHeader("Content-Transfer-Encoding", "8bit"); msg.setFrom(new InternetAddress("abc@xyz.com", "Your friend")); msg.setReplyTo(InternetAddress.parse("abc@xyz.com", false)); msg.setSubject(subject, "UTF-8"); msg.setSentDate(new Date()); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false)); //Create the message body part BodyPart messageBodyPart = new MimeBodyPart(); //Fill the message body part messageBodyPart.setText(body); //Create a multipart message for attachment Multipart multipart = new MimeMultipart(); //Set text message part multipart.addBodyPart(messageBodyPart); //second part is attachment messageBodyPart = new MimeBodyPart(); System.out.println("File path: "); String filename = ""; try{ filename = new BufferedReader(new InputStreamReader(System.in)).readLine(); }catch(IOException e){ System.out.println("Error while reading filename"); } DataSource source = new FileDataSource(filename); //filename is the loation of the file here messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName("Attached File"); //add the second part multipart.addBodyPart(messageBodyPart); //Set the content msg.setContent(multipart); System.out.println("Attaching Files..."); //Send message Transport.send(msg); System.out.println("EMail Sent Successfully with attachment!"); }catch(MessagingException e){ e.printStackTrace(); }catch(UnsupportedEncodingException e){ e.printStackTrace(); } } } public class SSLEmail { /** * Outgoing Mail (SMTP) Server requires TLS or SSL: smtp.gmail.com (use * authentication) Use Authentication: Yes Port for SSL: 465 */ public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter your GMail id: "); final String fromEmail = br.readLine(); //requires valid gmail id System.out.println("Password: "); final String password = br.readLine(); // correct password for gmail id System.out.println("Send To: "); final String toEmail = br.readLine(); // can be any email id System.out.println("\nSSL Email [Start]"); Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host props.put("mail.smtp.socketFactory.port", "465"); //SSL Port props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); //SSL Factory Class props.put("mail.smtp.auth", "true"); //Enabling SMTP Authentication props.put("mail.smtp.port", "465"); //SMTP Port props.put("mail.smtp.connectiontimeout", "5000"); props.put("mail.smtp.timeout", "5000"); Authenticator auth = new Authenticator() { //override the getPasswordAuthentication method protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(fromEmail, password); } }; Session session = Session.getDefaultInstance(props, auth); System.out.println("[Session created]"); System.out.println("Subject: "); String subject = br.readLine(); System.out.println("Body: [newline \"X\" ends the input]"); String temp = ""; String body=""; while(!(temp=br.readLine()).equalsIgnoreCase("X")){ body+=temp; } String choice = ""; System.out.println("Add a file attachment?(y/n): "); choice = br.readLine(); if(choice.equalsIgnoreCase("yes")|choice.equalsIgnoreCase("y")) EmailUtil.sendAttachmentEmail(session, toEmail, subject, body); else EmailUtil.sendEmail(session, toEmail, subject, body); } }
Note: Turn off all firewalls that might be running. [Windows firewall/Antivirus' Firewall]
Any improvements?
No comments:
Post a Comment