如何在java-mail中发送完整的html图像?我能够发送常规的html文本

时间:2016-10-12 06:01:20

标签: javascript html html5 javamail

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package jmail;

import java.io.File; import java.io.FileInputStream; import
java.io.IOException; import java.io.StringWriter; import
java.util.Date; import java.util.Properties; import
javax.mail.Authenticator; import javax.mail.Message; import
javax.mail.MessagingException; import
javax.mail.PasswordAuthentication; import javax.mail.Session; import
javax.mail.Transport; import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress; import
javax.mail.internet.MimeMessage; import sun.misc.IOUtils;

/**
*
* @author Admin
*/ public class HtmlJavaSend {

    public void sendHtmlEmail(String host, String port,
            final String userName, final String password, String toAddress,
            String subject, String message) throws AddressException,
            MessagingException,
            IOException {

        // sets SMTP server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", "false");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.ssl.trust","mail.man.com");


        // creates a new session with an authenticator
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        };

        Session session = Session.getInstance(properties, auth);

        // creates a new e-mail message
       Message msg = new MimeMessage(session);

        msg.setFrom(new InternetAddress(userName));
        InternetAddress[] toAddresses = {new InternetAddress(toAddress)};
        msg.setRecipients(Message.RecipientType.TO, toAddresses);
        msg.setSubject(subject);
        msg.setSentDate(new Date());

        // set plain text message
        //msg.setContent(message, "text/html");
        StringWriter writer = new StringWriter();
        org.apache.commons.io.IOUtils.copy(new FileInputStream(new File("C:\\netbeansproject\\En\\web\\EnGt.xhtml")),
writer);
        org.apache.commons.io.IOUtils.copy(new FileInputStream(new File("C:\\netbeansproject\\En\\web\\resources\\images\\logo_1.png")),
writer);
        msg.setContent(writer.toString(), "text/html");



        // sends the e-mail
        Transport.send(msg);

    }

    public static void main(String[] args) {
        // SMTP server information
        String host = "mail.man.com";
        String port = "25";
        String mailFrom = "admin@man.com";
        String password = "XXX";

        // outgoing message information
        String mailTo = "t.@man.com";
        String subject = "Hello En User";

        // message contains HTML markups
        String message = "<i>Greetings!</i><br>";
        message += "<b>Wish you a nice day!</b><br>";
        message += "<font color=red>Admin</font>";

        HtmlJavaSend mailer = new HtmlJavaSend();

        try {
            mailer.sendHtmlEmail(host, port, mailFrom, password, mailTo,
                    subject, message);
            System.out.println("Email sent.");
        } catch (Exception ex) {
            System.out.println("Failed to sent email.");
            ex.printStackTrace();
        }
    } }

0 个答案:

没有答案