Search in sources :

Example 1 with PasswordAuthentication

use of javax.mail.PasswordAuthentication in project javaee7-samples by javaee-samples.

the class ProgrammaticEmailServlet method processRequest.

/**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Sending email using JavaMail API</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Sending email using JavaMail API</h1>");
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.debug", "true");
        Session session = Session.getInstance(props, new javax.mail.Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(creds.getFrom(), creds.getPassword());
            }
        });
        try {
            out.println("Sending message from \"" + creds.getFrom() + "\" to \"" + creds.getTo() + "\"...<br>");
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(creds.getFrom()));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(creds.getTo()));
            message.setSubject("Sending message using Programmatic JavaMail " + Calendar.getInstance().getTime());
            message.setText("Java EE 7 is cool!");
            //                Transport t = session.getTransport();
            //                t.connect(creds.getFrom(), creds.getTo());
            //                t.sendMessage(message, message.getAllRecipients());
            Transport.send(message, message.getAllRecipients());
            out.println("message sent!");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
        out.println("</body>");
        out.println("</html>");
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) PrintWriter(java.io.PrintWriter) Session(javax.mail.Session) PasswordAuthentication(javax.mail.PasswordAuthentication)

Example 2 with PasswordAuthentication

use of javax.mail.PasswordAuthentication in project midpoint by Evolveum.

the class PageSecurityQuestions method sendMailToUser.

public void sendMailToUser(final String userLogin, final String password, String newPassword, String host, String port, String sender, String receiver) {
    try {
        Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(props, new javax.mail.Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userLogin, password);
            }
        });
        Message message = new MimeMessage(session);
        // TODO Localization
        message.setSubject("New Midpoint Password");
        message.setText("Password : " + newPassword + "\n");
        message.setFrom(new InternetAddress(sender));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiver));
        Transport.send(message);
    /*
			 * Session mailSession = Session.getDefaultInstance(props);
			 * MimeMessage message = new MimeMessage(mailSession);
			 * 
			 * message.setSubject("Engerek KYS Yeni Şifreniz");
			 * 
			 * message.setText("User Login : " + userLogin + "\n Password : " +
			 * password + "\n"); message.setFrom(new InternetAddress(sender));
			 * message.addRecipient(Message.RecipientType.TO, new
			 * InternetAddress(receiver)); Transport transport =
			 * mailSession.getTransport(); transport.connect();
			 * transport.sendMessage(message,
			 * message.getRecipients(Message.RecipientType.TO));
			 * transport.close();
			 */
    } catch (MessagingException ex) {
        LoggingUtils.logUnexpectedException(LOGGER, "Mail send Exception", ex);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Session(javax.mail.Session) PasswordAuthentication(javax.mail.PasswordAuthentication)

Example 3 with PasswordAuthentication

use of javax.mail.PasswordAuthentication in project opennms by OpenNMS.

the class JavaMailer2 method createAuthenticator.

/**
     * Helper method to create an Authenticator based on Password Authentication
     *
     * @param user a {@link java.lang.String} object.
     * @param password a {@link java.lang.String} object.
     * @return a {@link javax.mail.Authenticator} object.
     */
public Authenticator createAuthenticator(final String user, final String password) {
    Authenticator auth;
    auth = new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, password);
        }
    };
    return auth;
}
Also used : Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication)

Example 4 with PasswordAuthentication

use of javax.mail.PasswordAuthentication in project jmeter by apache.

the class MailerModel method sendMail.

/**
     * Sends a mail with the given parameters using SMTP.
     *
     * @param from
     *            the sender of the mail as shown in the mail-client.
     * @param vEmails
     *            all receivers of the mail. The receivers are seperated by
     *            commas.
     * @param subject
     *            the subject of the mail.
     * @param attText
     *            the message-body.
     * @param smtpHost
     *            the smtp-server used to send the mail.
     * @param smtpPort the smtp-server port used to send the mail.
     * @param user the login used to authenticate
     * @param password the password used to authenticate
     * @param mailAuthType {@link MailAuthType} Security policy
     * @param debug Flag whether debug messages for the mail session should be generated
     * @throws AddressException If mail address is wrong
     * @throws MessagingException If building MimeMessage fails
     */
public void sendMail(String from, List<String> vEmails, String subject, String attText, String smtpHost, String smtpPort, final String user, final String password, MailAuthType mailAuthType, boolean debug) throws MessagingException {
    InternetAddress[] address = new InternetAddress[vEmails.size()];
    for (int k = 0; k < vEmails.size(); k++) {
        address[k] = new InternetAddress(vEmails.get(k));
    }
    // create some properties and get the default Session
    Properties props = new Properties();
    props.put(MAIL_SMTP_HOST, smtpHost);
    // property values are strings
    props.put(MAIL_SMTP_PORT, smtpPort);
    Authenticator authenticator = null;
    if (mailAuthType != MailAuthType.NONE) {
        props.put(MAIL_SMTP_AUTH, "true");
        switch(mailAuthType) {
            case SSL:
                props.put(MAIL_SMTP_SOCKETFACTORY_CLASS, "javax.net.ssl.SSLSocketFactory");
                break;
            case TLS:
                props.put(MAIL_SMTP_STARTTLS, "true");
                break;
            default:
                break;
        }
    }
    if (!StringUtils.isEmpty(user)) {
        authenticator = new javax.mail.Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, password);
            }
        };
    }
    Session session = Session.getInstance(props, authenticator);
    session.setDebug(debug);
    // create a message
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(from));
    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSubject(subject);
    msg.setText(attText);
    Transport.send(msg);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Properties(java.util.Properties) Authenticator(javax.mail.Authenticator) Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication) Session(javax.mail.Session)

Example 5 with PasswordAuthentication

use of javax.mail.PasswordAuthentication in project SpringStepByStep by JavaProgrammerLB.

the class SendEmail method main.

public static void main(String[] args) {
    // Recipient's email ID needs to be mentioned.
    String to = "destinationemail@gmail.com";
    // Sender's email ID needs to be mentioned
    String from = "fromemail@gmail.com";
    //change accordingly
    final String username = "manishaspatil";
    //change accordingly
    final String password = "******";
    // Assuming you are sending email through relay.jangosmtp.net
    String host = "relay.jangosmtp.net";
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", "25");
    // Get the Session object.
    Session session = Session.getInstance(props, new javax.mail.Authenticator() {

        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });
    try {
        // Create a default MimeMessage object.
        Message message = new MimeMessage(session);
        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));
        // Set To: header field of the header.
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        // Set Subject: header field
        message.setSubject("Testing Subject");
        // Now set the actual message
        message.setText("Hello, this is sample for to check send " + "email using JavaMailAPI ");
        // Send message
        Transport.send(message);
        System.out.println("Sent message successfully....");
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Session(javax.mail.Session) PasswordAuthentication(javax.mail.PasswordAuthentication)

Aggregations

PasswordAuthentication (javax.mail.PasswordAuthentication)21 Session (javax.mail.Session)19 Properties (java.util.Properties)16 MimeMessage (javax.mail.internet.MimeMessage)16 InternetAddress (javax.mail.internet.InternetAddress)15 Message (javax.mail.Message)13 MessagingException (javax.mail.MessagingException)13 Authenticator (javax.mail.Authenticator)9 Date (java.util.Date)5 IOException (java.io.IOException)3 Multipart (javax.mail.Multipart)3 MimeBodyPart (javax.mail.internet.MimeBodyPart)3 MimeMultipart (javax.mail.internet.MimeMultipart)3 GeneralSecurityException (java.security.GeneralSecurityException)2 DataHandler (javax.activation.DataHandler)2 DataSource (javax.activation.DataSource)2 FileDataSource (javax.activation.FileDataSource)2 BodyPart (javax.mail.BodyPart)2 PropertiesUtil (com.myspringboot.utils.PropertiesUtil)1 POP3SSLStore (com.sun.mail.pop3.POP3SSLStore)1