Advertisement
Guest User

email

a guest
Sep 16th, 2014
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1. package epics.server.servlet;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.mail.Message;
  6. import javax.mail.MessagingException;
  7. import javax.mail.PasswordAuthentication;
  8. import javax.mail.Session;
  9. import javax.mail.Transport;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeMessage;
  12. //Klasa za email notifikaciju
  13. public class SendMail {
  14.    
  15.     public static void sendMessage(String titlep,String messageTextp,String addressp){
  16.         final String title=titlep;
  17.         final String messageText=messageTextp;
  18.         final String address=addressp;
  19.         Thread smt=new Thread(new Runnable() {
  20.             @Override
  21.             public void run() {
  22.                 //ovo je server koji salje tj od koga je uvek
  23.                 final String username = "epicsE13572@gmail.com";
  24.                 final String password = "epicsserver";
  25.          
  26.                 //Ovo moze i u tomee.xml mada mi je odmah ovde preglednije i brze za izmenu
  27.                 Properties props = new Properties();
  28.                 props.put("mail.smtp.auth", "true");
  29.                 props.put("mail.smtp.starttls.enable", "true");
  30.                 props.put("mail.smtp.host", "smtp.gmail.com");
  31.                 props.put("mail.smtp.port", "587");
  32.          
  33.                 Session session = Session.getInstance(props,
  34.                   new javax.mail.Authenticator() {
  35.                     protected PasswordAuthentication getPasswordAuthentication() {
  36.                         return new PasswordAuthentication(username, password);
  37.                     }
  38.                   });
  39.          
  40.                 try {
  41.                     // Constructs the message
  42.                     Message message = new MimeMessage(session);
  43.                     message.setFrom(new InternetAddress(username));//od epicsa
  44.                     message.setRecipients(Message.RecipientType.TO,
  45.                         InternetAddress.parse(address));//ko se obavestava
  46.                     message.setSubject(title);//naslov
  47.                     message.setText(messageText);//sadrzaj
  48.          
  49.                     // Sends the message
  50.                     Transport.send(message);
  51.                            
  52.                 } catch (MessagingException e) {
  53.                     e.printStackTrace();
  54.                 }
  55.             }
  56.         });
  57.         smt.run();
  58.        
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement