用户名: 密码: [免费注册] [忘记密码]
您的位置:首页 -- jsp技术
java mail发送带有图片邮件代码
发布日期:2012-12-14     返回


import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;

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;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        sendhtml(args);
    }

    public static void sendhtml(String[] argv) throws Exception {

        String to = "admin@yu-china.com", subject = "JSP空间www.yu-china.cn", from = "admin@yu-china.cn";
        String mailhost = "yu-china.cn";
        boolean debug = false;
        String username = "admin";
        String password = "12345678";
        Properties props = System.getProperties();
        props.put("mail.smtp.auth", "true");
        SMTPAuth auth = new SMTPAuth(username, password);
        

        if (mailhost != null) {
            props.put("mail.smtp.host", mailhost);
        }

        // Get a Session object
        Session session = Session.getDefaultInstance(props, auth);
        if (debug) {
            session.setDebug(true);
        }

        // construct the message
        Message msg = new MimeMessage(session);
        if (from != null) {
            msg.setFrom(new InternetAddress(from));
        } else {
            msg.setFrom();
        }

        msg.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to, false));
        if (cc != null) {
            msg.setRecipients(Message.RecipientType.CC,
                    InternetAddress.parse(cc, false));
        }
        if (bcc != null) {
            msg.setRecipients(Message.RecipientType.BCC,
                    InternetAddress.parse(bcc, false));
        }

        msg.setSubject(subject);


        MimeBodyPart mbp1 = new MimeBodyPart();
        String html = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"
                + "<html>"
                + "<head><title></title></head>"
                + "<body>"
                + "<b> see the following jpg : it is a car!</b><br>"
                + "<a href=http://www.a.com/a.jsp>hello</a><br>"
                + "<IMG SRC=cid:img1.jpg ><br>"
                + "<b> end of jpg</b>"
                + "</body>"
                + "</html>";

        mbp1.setContent(html, "text/html");

        FileDataSource fds = new FileDataSource("d:/IMG_20121003_142756.jpg");
        MimeBodyPart mbp2 = new MimeBodyPart();
        mbp2.setFileName("img1.jpg");
        mbp2.setText("This is a beautiful car !");
        mbp2.setDataHandler(new DataHandler(fds));
        mbp2.setHeader("Content-ID", "img1.jpg");

        MimeMultipart mp = new MimeMultipart("related");//alternative
        mp.addBodyPart(mbp1);
        mp.addBodyPart(mbp2);
        msg.setContent(mp);

        msg.setSentDate(new Date());
        Transport.send(msg);
        System.out.println(mp.getCount());
        System.out.println("\nMail was sent successfully.");

    }
}

class SMTPAuth extends javax.mail.Authenticator {

    private String user, password;

    public SMTPAuth(String u, String p) {
        user = u;
        password = p;
    }

    public void getuserinfo(String getuser, String getpassword) {
        user = getuser;
        password = getpassword;
    }

    @Override
    protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
        return new javax.mail.PasswordAuthentication(user, password);
    }
}

 

典型客户
JAVA技术
 
域信息科技提供专业的jsp空间服务,jsp空间,jsp空间租用,jsp空间购买,虚拟JSP主机,jsp主机,独享TOMCAT,JSP服务器
售前电话:400-004-4994
售后电话:400-004-4994
技术支持(7×24支持):400-004-4994 018241839456
公司地址:阜新市中华路65号新天地大厦9F
邮编:123000       传真:0418-2493388-606
经营性ICP证:辽B-2-4-20070114    ICP备案: 辽ICP备07005704号
域信息科技提供专业的jsp空间,虚拟JSP主机,jsp主机,独享TOMCAT,JSP服务器
域信息科技提供专业的jsp空间,虚拟JSP主机,jsp主机,独享TOMCAT,JSP服务器