Advertisement
Guest User

Untitled

a guest
Jun 18th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import javax.mail.*;
  2. import javax.mail.internet.InternetAddress;
  3. import javax.mail.internet.MimeMessage;
  4. import java.util.Properties;
  5.  
  6.  
  7. public class EmailTest {
  8.  
  9. public void sendEmail(){
  10. String to="toid@gmail.com";
  11. String host="mail.javatpoint.com";
  12. final String user="fromid@gmail.com";
  13. final String password="password";
  14. //Get the session object
  15. Properties props = new Properties();
  16. props.put("mail.smtp.host",host);
  17. props.put("mail.smtp.auth", "true");
  18. System.out.println("here");
  19. Session session = Session.getDefaultInstance(props,
  20. new Authenticator() {
  21.  
  22. protected PasswordAuthentication getPasswordAuthentication() {
  23. return new PasswordAuthentication(user,password);
  24. }
  25. });
  26.  
  27. //Compose the message
  28. System.out.println("here");
  29. MimeMessage message = new MimeMessage(session);
  30. try {
  31. message.setFrom(new InternetAddress(user));
  32.  
  33. message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
  34. message.setSubject("javatpoint");
  35. message.setText("This is simple program of sending email using JavaMail API");
  36.  
  37. //send the message
  38. Transport.send(message);
  39.  
  40. System.out.println("message sent successfully...");
  41. } catch (MessagingException e) {
  42. e.printStackTrace();
  43. }
  44.  
  45.  
  46. }
  47.  
  48. }
  49.  
  50. public class MobileRequestServlet extends HttpServlet {
  51.  
  52. protected void doPost(HttpServletRequest request, HttpServletResponse response) {
  53.  
  54. EmailTest test=new EmailTest();
  55. test.sendEmail();
  56.  
  57. }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement