fork download
  1. import javax.mail.*;
  2. import javax.mail.internet.*;
  3. import java.util.*;
  4. import javax.activation.*;
  5. public class SimpleMailTransferProtocol
  6. {public static void main(String[] args){
  7. Scanner in = new Scanner(System.in);
  8. String SenderUser = "ap.monishkumar";
  9. String SenderMail = "ap.monishkumar@gmail.com";
  10. System.out.print("Enter Password for Authentication:");
  11. String SenderPassword = in.nextLine();
  12. System.out.print("Enter Destination Address:");
  13. String ToMail = in.nextLine();
  14. String ToHost = "smtp.gmail.com";
  15. Properties SessionProperties = new Properties();
  16. SessionProperties.put("mail.smtp.auth","true");
  17.  
  18. 35
  19.  
  20. SessionProperties.put("mail.smtp.starttls.enable","true");
  21. SessionProperties.put("mail.smtp.host",ToHost);
  22. SessionProperties.put("mail.smtp.port",587);
  23. Session CurrentSession = Session.getInstance(SessionProperties,
  24. new javax.mail.Authenticator(){
  25. protected PasswordAuthentication
  26. getPasswordAuthentication(){ return new
  27. PasswordAuthentication(SenderMail, SenderPassword);
  28. }
  29. });
  30. try{
  31. Message ThisMessage = new MimeMessage(CurrentSession);
  32. ThisMessage.setFrom(new InternetAddress(SenderMail));
  33. ThisMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(ToMail));
  34. System.out.print("Enter Subject for mail:");
  35. String Subject = in.nextLine();
  36. System.out.println("Enter Body of the mail:");
  37. String Body = in.nextLine();
  38. System.out.print("Do you want to add attachment?(y/n):");
  39. String c = in.nextLine();
  40. if(c.equalsIgnoreCase("y")){ System.out.print("E
  41. nter FileName to Attach:");String FileName =
  42. in.nextLine();
  43. DataSource FileSource = new FileDataSource(FileName);
  44. MimeBodyPart PartOne = new MimeBodyPart();
  45. PartOne.setText(Body);
  46. MimeBodyPart PartTwo = new MimeBodyPart();
  47. PartTwo.setDataHandler(new DataHandler(FileSource));
  48. PartTwo.setFileName(FileName);
  49. Multipart MessageBody = new MimeMultipart();
  50. MessageBody.addBodyPart(PartOne);
  51. MessageBody.addBodyPart(PartTwo);
  52. ThisMessage.setContent(MessageBody);
  53. }
  54. else ThisMessage.setContent(Body,"text/html");
  55. ThisMessage.setSubject(Subject);
  56. Transport.send(ThisMessage);
  57. System.out.println("The Message was sent successfully...");
  58. }
  59. catch(Exception
  60. e){ e.printStackTra
  61. ce();
  62. }
  63. in.close();
  64. }
  65. }
Success #stdin #stdout 0.02s 25556KB
stdin
Standard input is empty
stdout
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.activation.*;
public class SimpleMailTransferProtocol
{public static void main(String[] args){
Scanner in = new Scanner(System.in);
String SenderUser = "ap.monishkumar";
String SenderMail = "ap.monishkumar@gmail.com";
System.out.print("Enter Password for Authentication:");
String SenderPassword = in.nextLine();
System.out.print("Enter Destination Address:");
String ToMail = in.nextLine();
String ToHost = "smtp.gmail.com";
Properties SessionProperties = new Properties();
SessionProperties.put("mail.smtp.auth","true");

35

SessionProperties.put("mail.smtp.starttls.enable","true");
SessionProperties.put("mail.smtp.host",ToHost);
SessionProperties.put("mail.smtp.port",587);
Session CurrentSession = Session.getInstance(SessionProperties,
new javax.mail.Authenticator(){
protected PasswordAuthentication
getPasswordAuthentication(){ return new
PasswordAuthentication(SenderMail, SenderPassword);
}
});
try{
Message ThisMessage = new MimeMessage(CurrentSession);
ThisMessage.setFrom(new InternetAddress(SenderMail));
ThisMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(ToMail));
System.out.print("Enter Subject for mail:");
String Subject = in.nextLine();
System.out.println("Enter Body of the mail:");
String Body = in.nextLine();
System.out.print("Do you want to add attachment?(y/n):");
String c = in.nextLine();
if(c.equalsIgnoreCase("y")){ System.out.print("E
nter FileName to Attach:");String FileName =
in.nextLine();
DataSource FileSource = new FileDataSource(FileName);
MimeBodyPart PartOne = new MimeBodyPart();
PartOne.setText(Body);
MimeBodyPart PartTwo = new MimeBodyPart();
PartTwo.setDataHandler(new DataHandler(FileSource));
PartTwo.setFileName(FileName);
Multipart MessageBody = new MimeMultipart();
MessageBody.addBodyPart(PartOne);
MessageBody.addBodyPart(PartTwo);
ThisMessage.setContent(MessageBody);
}
else ThisMessage.setContent(Body,"text/html");
ThisMessage.setSubject(Subject);
Transport.send(ThisMessage);
System.out.println("The Message was sent successfully...");
}
catch(Exception
e){ e.printStackTra
ce();
}
in.close();
}
}