集册 Java实例教程 发送电子邮件文件路径

发送电子邮件文件路径

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

471
发送电子邮件文件路径


import java.util.Properties;
/**
 from
* 时 代      J a v a   公   众 号 - nowjava.com 
**/

import javax.activation.DataHandler;

import javax.activation.DataSource;

import javax.activation.FileDataSource;

import javax.mail.BodyPart;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;

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

public class Main{

    private final static Logger logger = Logger

            .getLogger(EmailSmtpUtil.class.getName());

    private final static String MAIL_PORT = "";

    private final static String MAIL_SERVER = "";

    private final static String MAIL_FROM = "";

    private final static String MAIL_STARTTLS = "";

    private final static String MAIL_AUTH = "";

    private final static String MAIL_USER = "";

    private final static String MAIL_PWD = "";

    public static void sendEmailFilePath(String to, String bodyPlain,

            String filePath, String subject) {


        logger.info("sendEmailFilePath");

        logger.info("Message send in progress...");


        Properties properties = System.getProperties();

        properties.setProperty("mail.smtp.host", MAIL_SERVER);

        properties.setProperty("mail.smtp.port", MAIL_PORT);

        Session session = Session.getDefaultInstance(properties);

        if (MAIL_AUTH.equals("true")) {

            properties.setProperty("mail.smtp.auth", MAIL_AUTH);

            properties.setProperty("mail.smtp.starttls.enable",

                    MAIL_STARTTLS);

            session = Session.getInstance(properties,

                    new javax.mail.Authenticator() {

                        protected PasswordAuthentication getPasswordAuthentication() {

                            return new PasswordAuthentication(MAIL_USER,

                                    MAIL_PWD);

                        }

                    });

        }


        try {

            MimeMessage message = new MimeMessage(session);

            message.setFrom(new InternetAddress(MAIL_FROM));

            message.addRecipient(Message.RecipientType.TO,

                    new InternetAddress(to));

            message.setSubject(subject);


            BodyPart messageBodyPart = new MimeBodyPart();

            messageBodyPart.setText(bodyPlain);

            Multipart multipart = new MimeMultipart();

            multipart.addBodyPart(messageBodyPart);


            messageBodyPart = 
展开阅读全文