Java Email Sender

1. Launch intellij idea and create simple project.

2. Download last release of JavaMail library and add that library into project structure window.

3. In the same project structure window, add new artifact for future jar file, from modules with dependencies. Do next few steps:

4. Write next code:

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.Security;
import java.util.Properties;

public class Main {
    public static void main(String[] args) {
        String smtpServerName = "smtp.mail.ru";
        String yourEmainAddress = "your email address";
        String yourEmailPassword = "your password";
        int smtpPort;
        String dastEmail;
        String theme;
        String message;
        if (args.length != 4) {
            System.out.println("Wrong arguments!\n1st - destination email;\n2nd - theme;\n3rd - message.\n4th - port(secure 465 or unsecure 587 or 2525)");
            return;
        } else {
            try {
                dastEmail = args[0];
                theme = args[1];
                message = args[2];
                smtpPort = Integer.parseInt(args[3]);
            } catch (Exception e) {
                System.out.println("Error: " + e.getMessage());
                return;
            }
        }
        sendEmail(smtpServerName, yourEmainAddress, yourEmailPassword, smtpPort, dastEmail, theme, message);
    }

    private static String sendEmail(String smtpServerName, String userName, String password, int smtpPort, String dastEmail, String theme, String message) {
        try {
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            Properties props = new Properties();
            props.put("mail.smtp.user", userName);
            props.put("mail.smtp.host", smtpServerName);
            props.put("mail.smtp.port", smtpPort);
            props.put("mail.smtp.starttls.enable","true");
            props.put("mail.smtp.debug", "true");
            props.put("mail.smtp.auth", "true");
            if (smtpPort == 465) {
                props.put("mail.smtp.socketFactory.port", smtpPort);
                props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            }
            String[] parts = userName.split("@");
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(userName, password);
                }
            };
            javax.mail.Session sess = Session.getInstance(props, authenticator);
            MimeMessage msg = new MimeMessage(sess);
            msg.setFrom(new InternetAddress(userName, userName));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(dastEmail, dastEmail));
            msg.setSubject((theme.length() > 100 ? theme.substring(0, 100) : theme) + "...");
            msg.setText(message);
            Transport transport = sess.getTransport(new URLName(smtpPort == 465 ? "smtps" : "smtp", null, smtpPort, null, null, null));
            transport.connect(smtpServerName, smtpPort, parts[0], password);
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
            return e.getMessage();
        }
        return null;
    }
}

5. Build artifacts now and try launch your generated jar file, use command with 4 parameters: java -jar emailsender.jar “some destination@email_address.com” “theme” “message” “465”

 

Leave a Comment