Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Properties;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.Message.RecipientType;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- public class MailSender {
- private static String host = "";
- private static String user = "";
- private static String password = "";
- private static int port = 465;
- private static String from = "";
- private static String to = "";
- private static String auth = "true";
- private static String debug = "false";
- private static String socket_factory = "javax.net.ssl.SSLSocketFactory";
- private static String subject = "";
- private static String text = "";
- /**
- * Constructor. Fields - parameters for send mail.
- *
- * @param host - mail server.
- * @param user - user
- * @param password - login
- * @param port - port
- * @param from - mail from address
- * @param to - mail to address
- * @param subject - subject
- * @param text - text of mail
- */
- public MailSender(String host, String user, String password, int port,
- String from, String to, String subject, String text) {
- if (!host.isEmpty())
- setHost(host);
- if (!user.isEmpty())
- setUser(user);
- if (!password.isEmpty())
- setPassword(password);
- if (port == 0)
- setPort(port);
- if (!from.isEmpty())
- setFrom(from);
- if (!to.isEmpty())
- setTo(to);
- if (!subject.isEmpty())
- setSubject(subject);
- if (!text.isEmpty())
- setText(text);
- }
- /**
- * Send mail with parameters from constructor.
- */
- public void send() {
- // Use Properties object to set environment properties
- Properties props = new Properties();
- props.setProperty("mail.transport.protocol", "smtp");
- props.put("mail.smtp.host", getHost());
- props.put("mail.smtp.port", getPort());
- props.put("mail.smtp.user", getUser());
- props.put("mail.smtp.auth", getAuth());
- props.put("mail.smtp.starttls.enable","true");//for gmail?
- props.put("mail.smtp.debug", getDebug());
- props.put("mail.smtp.socketFactory.port", getPort());
- props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");//for gmail?
- props.put("mail.smtp.socketFactory.fallback", "false");
- try {
- // Obtain the default mail session
- Session session = Session.getDefaultInstance(props, null);
- session.setDebug(new Boolean(getDebug()));
- // Construct the mail message
- MimeMessage message = new MimeMessage(session);
- message.setText(getText());
- message.setSubject(getSubject());
- message.setFrom(new InternetAddress(getFrom()));
- message
- .addRecipient(RecipientType.TO,
- new InternetAddress(getTo()));
- message.saveChanges();
- // Use Transport to deliver the message
- Transport transport = session.getTransport("smtp");
- transport.connect(getHost(), getUser(), getPassword());
- transport.sendMessage(message, message.getAllRecipients());
- transport.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static String getHost() {
- return host;
- }
- public static void setHost(String host) {
- MailSender.host = host;
- }
- public static String getUser() {
- return user;
- }
- public static void setUser(String user) {
- MailSender.user = user;
- }
- public static String getPassword() {
- return password;
- }
- public static void setPassword(String password) {
- MailSender.password = password;
- }
- public static int getPort() {
- return port;
- }
- public static void setPort(int port) {
- MailSender.port = port;
- }
- public static String getFrom() {
- return from;
- }
- public static void setFrom(String from) {
- MailSender.from = from;
- }
- public static String getTo() {
- return to;
- }
- public static void setTo(String to) {
- MailSender.to = to;
- }
- public static String getAuth() {
- return auth;
- }
- public static void setAuth(String auth) {
- MailSender.auth = auth;
- }
- public static String getDebug() {
- return debug;
- }
- public static void setDebug(String debug) {
- MailSender.debug = debug;
- }
- public static String getSocket_factory() {
- return socket_factory;
- }
- public static void setSocket_factory(String socketFactory) {
- socket_factory = socketFactory;
- }
- public static String getSubject() {
- return subject;
- }
- public static void setSubject(String subject) {
- MailSender.subject = subject;
- }
- public static String getText() {
- return text;
- }
- public static void setText(String text) {
- MailSender.text = text;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement