Java mail发送邮件

来源:互联网 发布:解手机密码软件 编辑:程序博客网 时间:2024/06/06 01:58

导包

mail.jar
activation.jar


示例代码

package cn.yellowimg.javamail;import java.io.File;import java.io.IOException;import java.io.OutputStream;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Message.RecipientType;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 javax.mail.internet.MimeUtility;import org.junit.Test;public class Demo1 {    @Test    public void fun1() throws Exception    {        /*         * 1 得到Session对象 即登陆服务器         */        Properties properties = new Properties();        //设置主机IP        properties.setProperty("mail.host", "smtp.163.com");        //设置是否验证        properties.setProperty("mail.smtp.auth", "true");        Authenticator authenticator = new Authenticator() {            @Override            protected PasswordAuthentication getPasswordAuthentication() {                return new PasswordAuthentication("username","password");            }        };        Session session = Session.getInstance(properties, authenticator);        /*         * 2 创建MimeMessage         */        MimeMessage msg = new MimeMessage(session);        //设置发件人         msg.setFrom(new InternetAddress("me@163.com"));        /*         * 设置收件人         * RecipientType.TO 发件人直接发于收件人         * RecipientType.CC 抄送         * RecipientType.BCC 暗送         */        msg.setRecipient(RecipientType.TO,new InternetAddress("to@qq.com"));        //允许你围观 但会被别人知道        msg.setRecipient(RecipientType.CC,new InternetAddress("cc@qq.com"));        //允许你围观 不会被别人知道        msg.setRecipient(RecipientType.BCC,new InternetAddress("bcc@qq.com"));        //设置标题        msg.setSubject("这是来自莫斯科的测试邮件");        //设置正文以及格式        msg.setContent("莫名我就喜环你 深深的爱上你","text/html;charset=utf-8");        /*         * 3 发送邮件         */        Transport.send(msg);    }
    /*    *    带有附件的发送邮件    */    @Test    public void fun2() throws Exception    {        /*         * 1 得到Session对象 即登陆服务器         */        Properties properties = new Properties();        properties.setProperty("mail.host", "smtp.163.com");        properties.setProperty("mail.smtp.auth", "true");        Authenticator authenticator = new Authenticator() {            @Override            protected PasswordAuthentication getPasswordAuthentication() {                return new PasswordAuthentication("username","password");            }        };        Session session = Session.getInstance(properties, authenticator);        /*         * 2 创建MimeMessage         */        MimeMessage msg = new MimeMessage(session);        //设置发件人         msg.setFrom(new InternetAddress("me163.com"));        /*         * 设置收件人         * RecipientType.TO 发件人直接发于收件人         * RecipientType.CC 抄送         * RecipientType.BCC 暗送         */        msg.setRecipient(RecipientType.TO,new InternetAddress("to@qq.com"));        //允许你围观 但会被别人知道        msg.setRecipient(RecipientType.CC,new InternetAddress("cc@qq.com"));        //允许你围观 不会被别人知道        msg.setRecipient(RecipientType.BCC,new InternetAddress("bcc@qq.com"));        msg.setSubject("这是来自莫斯科的附带文件邮件");        ///////////////////////////////////////////////////////////////        /*         *          * 发送邮件带附件         *          */        //创建一个主体 可以理解为集合 一个容器        MimeMultipart list = new MimeMultipart();        MimeBodyPart part1 = new MimeBodyPart();        //设置正文        part1.setContent("高圆圆的相片", "text/html;charset=utf-8");        //附件部分        MimeBodyPart part2 = new MimeBodyPart();        part2.attachFile(new File("D:/高圆圆.jpg"));        //设置附件名字 为了防止乱码需要进行编码        part2.setFileName(MimeUtility.encodeText("高圆圆.jpg"));        list.addBodyPart(part1);        list.addBodyPart(part2);        msg.setContent(list);        ///////////////////////////////////////////////////////////////        /*         * 3 发送邮件         */        Transport.send(msg);    }}
原创粉丝点击