Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- public class MailDemo {
- public static void main(String[] args) {
- Properties properties=new Properties();
- properties.put("mail.smtp.auth", "true");
- properties.put("mail.smtp.starttls.enable","true");
- properties.put("mail.smtp.host","smtp.gmail.com");
- properties.put("mail.smtp.port", "587");
- Scanner scn=new Scanner(System.in);
- System.out.println("Username for Authentication :");
- final String username=scn.nextLine();
- System.out.println("Password for Authentication :");
- final String password=scn.nextLine();
- System.out.println("From Email..");
- String fromEmailAddrs=scn.nextLine();
- System.out.println("To Email..");
- String toEmail=scn.nextLine();
- System.out.println("Subject..");
- String subject=scn.nextLine();
- System.out.println("Message..");
- String textMessage=scn.nextLine();
- Session session=Session.getDefaultInstance(properties,new Authenticator() {
- @Override
- protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
- return new javax.mail.PasswordAuthentication(username, password);
- }
- });
- try{
- Message msg=new MimeMessage(session);
- msg.setFrom(new InternetAddress(fromEmailAddrs));
- msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
- msg.setSubject(subject);
- msg.setText(textMessage);
- Transport.send(msg);
- System.out.println("/n Your Message Delivered Succesfully");
- }
- catch(MessagingException m){
- throw new RuntimeException(m);
- }
- }
- }
- Username for Authentication :
- bijaybhaskar01
- Password for Authentication :
- abcdef@456789
- From Email..
- bijaybhaskar01@gmail.com
- To Email..
- bbswain1001@gmail.com
- Subject..
- Hello
- Message..
- hai
- Exception in thread "main" java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed;
- nested exception is:
- class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. fh14sm3131583pab.31 - gsmtp
- at com.mail.bhaskar.MailDemo.main(MailDemo.java:52)
- Caused by: javax.mail.SendFailedException: Sending failed;
- nested exception is:
- class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. fh14sm3131583pab.31 - gsmtp
- at javax.mail.Transport.send0(Transport.java:218)
- at javax.mail.Transport.send(Transport.java:80)
- at com.mail.bhaskar.MailDemo.main(MailDemo.java:48)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement