Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.bulkmail.test;
- import java.util.*;
- import javax.mail.*;
- import javax.mail.internet.*;
- public class MailSend {
- private String host;
- private String port;
- private String username;
- private String password;
- private String from;
- public MailSend() {
- ResourceBundle rb = ResourceBundle.getBundle("SMTPAuthenticator");
- username = rb.getString("username");
- password = rb.getString("password");
- host = rb.getString("host");
- port = rb.getString("port");
- from = rb.getString("from");
- }
- public void send(String to, String subject, String text) throws
- AuthenticationFailedException, MessagingException {
- sendMail(to, subject, text, false);
- }
- public void sendHTML(String to, String subject, String text) throws
- AuthenticationFailedException, MessagingException {
- sendMail(to, subject, text, true);
- }
- /**
- * sendMail - Send the mail message. This method uses Simple Authentication
- * schema (LOGIN), through Base64 enconding
- *
- * @param to String - The mail recepient
- * @param subject String - The mail subject
- * @param text String - Message to be sent
- * @param isHTML boolean - Set the encoding. True for html.
- * @throws AuthenticationFailedException
- * @throws MessagingException
- */
- public void sendMail(String to, String subject, String text,
- boolean isHTML) throws
- AuthenticationFailedException, MessagingException {
- // Get system properties
- Properties props = System.getProperties();
- // Setup mail server properties
- props.put("mail.transport.protocol", "smtp");
- props.put("mail.smtp.submitter","LOGIN");
- props.put("mail.smtp.debug", "true");
- props.put("mail.smtp.user", username);
- props.put("mail.smtp.host", host);
- props.put("mail.smtp.port", port);
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.allow8bitmime", "true");
- props.put("mail.smtp.socketFactory.port", port);
- props.put("mail.smtp.socketFactory.class", "javax.net.SocketFactory");
- props.put("mail.smtp.socketFactory.fallback", "true");
- // Get session
- Session mailSession = javax.mail.Session.getInstance(props,
- new SMTPAuthenticator());
- // Set debug
- mailSession.setDebug(true);
- // Define message
- MimeMessage message = new MimeMessage(mailSession);
- // Set the from address
- message.setFrom( (Address)new InternetAddress(from));
- Address[] toUser = InternetAddress.parse(to);
- message.setRecipients(MimeMessage.RecipientType.TO, toUser);
- // Set the subject
- message.setSubject(subject);
- // Set the content
- if (isHTML) {
- message.setContent(text, "text/html");
- } else {
- message.setText(text);
- }
- // Send message
- Transport.send(message);
- }
- // class SMTPAuthenticator extends Authenticator {
- // public PasswordAuthentication getPasswordAuthentication() {
- // sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
- // return new PasswordAuthentication(enc.encode(username.getBytes()),
- // enc.encode(password.getBytes()));
- // }
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement