Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package email;
- import java.util.Properties;
- 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;
- /**
- * A singleton class for sending mail messages.
- * @author tcolburn
- */
- public class MailService {
- /**
- * Sends a subject and message to a recipient
- * @param recipient Internet address of the recipient
- * @param subject the subject of the message
- * @param message the message
- * @throws MessagingException
- */
- public static void sendMessage(String subject, String message) throws MessagingException {
- if ( theService == null ) {
- theService = new MailService();
- }
- MimeMessage mimeMessage = new MimeMessage(mailSession);
- mimeMessage.setFrom(new InternetAddress(FROM));
- mimeMessage.setSender(new InternetAddress(FROM));
- mimeMessage.setSubject(subject);
- mimeMessage.setContent(message, "text/plain");
- mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("delegations.corpo@gmail.com"));
- Transport transport = mailSession.getTransport("smtps");
- transport.connect(HOST, PORT, USER, PASSWORD);
- transport.sendMessage(mimeMessage, mimeMessage.getRecipients(Message.RecipientType.TO));
- transport.close();
- }
- private MailService() {
- Properties props = new Properties();
- props.put("mail.transport.protocol", "smtps");
- props.put("mail.smtps.host", HOST);
- props.put("mail.smtps.auth", "true");
- props.put("mail.smtp.from", FROM);
- props.put("mail.smtps.quitwait", "false");
- mailSession = Session.getDefaultInstance(props);
- mailSession.setDebug(true);
- }
- private static MailService theService = null;
- private static Session mailSession;
- private static final String HOST = "poczta.o2.pl";
- private static final int PORT = 465;
- private static final String USER = "kowalskijan5151"; // Must be valid user in d.umn.edu domain, e.g. "smit0012"
- private static final String PASSWORD = "kowalskijan5151"; // Must be valid password for smit0012
- private static final String FROM = "PAI PROJEKT<kowalskijan5151@o2.pl>"; // Full info for user, e.g. "Fred Smith <smit0012@d.umn.edu>"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement