集册 Java实例教程 发送邮件验证SSL

发送邮件验证SSL

欢马劈雪     最近更新时间:2020-01-02 10:19:05

71
发送邮件验证SSL

/**
n o w    j a v a  . c o m
**/

import java.io.File;

import java.io.UnsupportedEncodingException;

import java.util.Properties;

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.mail.Authenticator;

import javax.mail.BodyPart;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.Session;

import javax.mail.Transport;// from NowJava.com - 时代Java

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

import javax.mail.internet.MimeUtility;


public class Main{

    

    private static final String DEFAULT_CHARSET = "euc-kr";

    

    public static void sendMailAuthSSL(String smtpHost, String smtpPort,

            String smtpUser, String smtpPassword, String subject,

            String content, String toEmail, String fromEmail,

            String fromName) throws UnsupportedEncodingException,

            MessagingException {

        sendMailAuthSSL(smtpHost, smtpPort, smtpUser, smtpPassword,

                subject, content, toEmail, fromEmail, fromName,

                DEFAULT_CHARSET, null);

    }

    

    public static void sendMailAuthSSL(String smtpHost, String smtpPort,

            String smtpUser, String smtpPassword, String subject,

            String content, String toEmail, String fromEmail,

            String fromName, String charset)

            throws UnsupportedEncodingException, MessagingException {

        sendMailAuthSSL(smtpHost, smtpPort, smtpUser, smtpPassword,

                subject, content, toEmail, fromEmail, fromName, charset,

                null);

    }

    

    public static void sendMailAuthSSL(String smtpHost, String smtpPort,

            String smtpUser, String smtpPassword, String subject,

            String content, String toEmail, String fromEmail,

            String fromName, String charset, File[] attachFiles)

            throws UnsupportedEncodingException, MessagingException {

        Properties props = new Properties();

        props.put("mail.smtp.host", smtpHost);

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

        props.put("mail.smtp.user", smtpUser);

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

        props.put("mail.smtp.socketFactory.class",

                "javax.net.ssl.SSLSocketFactory");

        MyAuthenticator auth = new MyAuthenticator(smtpUser, smtpPassword);

        Session session = Session.getDefaultInstance(props, auth);

        sendMail(subject, content, toEmail, fromEmail, fromName, charset,

                attachFiles, session);

    }

    

    private static void sendMail(String subject, String content,

            String toEmail, String fromEmail, String fromName,

            String charset, File[] attachFiles, Session session)

            throws UnsupportedEncodingException, MessagingException {

        MimeMessage message = new MimeMessage(session);

        InternetAddress addr = new InternetAddress(fromEmail, fromName,

                charset);

        message.setFrom(addr);

        message.setSubject(subject);

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(

                toEmail));


        if (attachFiles == null) {

            message.setContent(content, "text/html; charset=" + charset);

        } else {

            Multipart mult
展开阅读全文