Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package turistinfo;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Properties;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- public class MailClient {
- private static HashMap<String, Boolean> notificationMap = new HashMap<>();
- static {
- notificationMap.put("Info Grad", false);
- notificationMap.put("Info Hotel", false);
- notificationMap.put("Info Bus", false);
- notificationMap.put("Info Event", false);
- notificationMap.put("Info Vrijeme", false);
- notificationMap.put("Info Admin", false);
- }
- private static String username;
- private static String password;
- public static Properties loadMailConfig() throws FileNotFoundException, IOException {
- Properties serverprop = new Properties();
- serverprop.load(new FileInputStream(new File("src/mail/server.properties")));
- String mailProvider = serverprop.getProperty("mail_provider");
- System.out.println("Koristi se " + mailProvider);
- Properties mailProp = new Properties();
- mailProp.load(new FileInputStream(new File("src/mail" + File.separator + mailProvider + ".properties")));
- username = mailProp.getProperty("username");
- password = mailProp.getProperty("password");
- return mailProp;
- }
- public static boolean sendMail(String to, String title, String body, String service) throws FileNotFoundException, IOException {
- //check if the mail is already sent for specified service
- if ( notificationMap.get(service) ){
- return false;
- }
- Properties props = loadMailConfig();
- Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
- });
- try {
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress(username));
- message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
- message.setSubject(title);
- message.setText(body);
- Transport.send(message);
- return true;
- } catch (MessagingException e) {
- e.printStackTrace();
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement