The Pursuit of Happyness


import java.util.Properties;


import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;


public class GmailSMTP

{

    public static void sendMail(String account, String auth, String to, String subject, String msg, boolean isSSL, boolean isHtml)

    {

        final String username = account;

        final String password = auth;


        String mailFrom = username;

        String mailTo = to;


        Properties props = new Properties();

        props.put("mail.smtp.auth", "true");

        props.put("mail.smtp.host", "smtp.gmail.com");


        if (isSSL)

        {

            props.put("mail.smtp.socketFactory.port", "465");

            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

            props.put("mail.smtp.port", "465");

        }

        else

        {

            props.put("mail.smtp.starttls.enable", "true");

            props.put("mail.smtp.port", "587");

        }


        Session session = Session.getInstance(props, new javax.mail.Authenticator()

        {

            protected PasswordAuthentication getPasswordAuthentication()

            {

                return new PasswordAuthentication(username, password);

            }

        });


        try

        {

            Message message = new MimeMessage(session);

            message.setFrom(new InternetAddress(mailFrom));

            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailTo));

            message.setSubject(subject);


            if (isHtml)

                message.setContent(msg, "text/html");

            else

                message.setText(msg);


            Transport.send(message);

        }

        catch (MessagingException e)

        {

            throw new RuntimeException(e);

        }

    }

}

 


ps. mail.jar 가 필요함


Comment +1

  • L'homme qui a commis mercredi un attentat devant l'ambassade de France à Bamako, http://timberlandbotases.com timberland niños, faisant deux blessés légers, est un Tunisien de 25 ans ayant, http://timberlandbotases.com timberland madrid, "à titre personnel, http://timberlandbotases.com timberland mujer 2012, la haine de la France" et qui viendrait d'un camp d'Al-Qa, http://timberlandbotases.com timberland earthkeepers?da dans le Sahara, http://timberlandbotases.com botas timberland, (source policière), http://timberlandbotases.com timberland pro.
    Related articles:


    http://show.mamahelp.net/read.php?tid=663367 http://show.mamahelp.net/read.php?tid=663367

    http://forum.sports.sina.com.cn/post.php?action=newthread&fid=31&extra=page=1 http://forum.sports.sina.com.cn/post.php?action=newthread&fid=31&extra=page=1

    http://acgwolf.uueasy.com/read.php?tid=542074 http://acgwolf.uueasy.com/read.php?tid=542074