Package Java

Source Code of Java.Email

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Java;

import com.sun.mail.smtp.SMTPTransport;
import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
*
* @author Geert
*/
public class Email {
   
    private static final String username = "pts4Photoproducer";
    private static final String password = "PTS4s43a";

    public static void sendRegistrationMail(String recipientEmail, String personUsername, String personPassword) {
        String message = "Bedankt voor het registreren bij fotoproducent,<br>"
                        + "<br>"
                        + "<b>Uw inloggegevens zijn als volgt:</b><br>"
                        + "<i>Gebruikersnaam:</i> " + personUsername + "<br>"
                        + "<i>Wachtwoord:</i> " + personPassword + "<br>"
                        + "<br>"
                        + "Heeft u niet zelf dit account geregistreerd? Dan heeft uw fotograaf dat voor u gedaan.<br>"
                        + "U kunt nu op onze site inloggen en de gemaakte foto's bestellen!<br>"
                        + "<A HREF=\"http://localhost:21702/WebApplication\">http://www.fotoproducent.nl/</A>";
       
        Email.sendMail(recipientEmail, "Registratie", message);
    }
   
    private static void sendMail(final String recipientEmail, final String title, final String message) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
                    final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

                    // Get a Properties object
                    Properties props = System.getProperties();
                    props.setProperty("mail.smtps.host", "smtp.gmail.com");
                    props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
                    props.setProperty("mail.smtp.socketFactory.fallback", "false");
                    props.setProperty("mail.smtp.port", "465");
                    props.setProperty("mail.smtp.socketFactory.port", "465");
                    props.setProperty("mail.smtps.auth", "true");

                    props.put("mail.smtps.quitwait", "false");

                    Session session = Session.getInstance(props, null);

                    // -- Create a new message --
                    final MimeMessage msg = new MimeMessage(session);

                    // -- Set the FROM and TO fields --
                    msg.setFrom(new InternetAddress("noreply@fotoproducent.com", "Fotoproducent"));
                    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));

                    msg.setSubject(title);
                    msg.setContent(message, "text/html; charset=utf-8");
                    msg.setSentDate(new Date());

                    SMTPTransport t = (SMTPTransport)session.getTransport("smtps");

                    t.connect("smtp.gmail.com", username, password);
                    t.sendMessage(msg, msg.getAllRecipients());     
                    t.close();
                } catch (MessagingException ex) {
                    Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }, "sendMail").start();
    }
}
TOP

Related Classes of Java.Email

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.