首页 > 代码库 > java发送email

java发送email

package com.assess.util;import java.io.File;import java.util.ArrayList;import java.util.List;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;import javax.mail.internet.MimeUtility;public class SendmailUtil {    /*     * 参考此文章     * http://www.cnblogs.com/xdp-gacl/p/4216311.html     *      *      * smtp.sohu.com 搜狐邮箱主机     * smtp.163.com 163邮箱主机,默认端口25     * smtp.qq.com qq邮箱主机     * */        /**     * from 发件人邮箱     * passwd 发件人邮箱密码     * to 收件人邮箱     * subject 主题     * txt 内容     * files 附件     * host 服务主机      * protocol 协议 (smtp)     * auth 身份验证(true)     * */     public static boolean send(String from,String passwd,String to,                                 String subject, String txt,List<File> files,                                 String host,String protocol,String auth) {                     try {             Properties prop = new Properties();            prop.setProperty("mail.host", host);            prop.setProperty("mail.transport.protocol", protocol);            prop.setProperty("mail.smtp.auth", auth);            //使用JavaMail发送邮件的5个步骤            //1、创建session            Session session = Session.getInstance(prop);            //开启Session的debug模式,这样就可以查看到程序发送Email的运行状态            session.setDebug(true);            //2、通过session得到transport对象            Transport ts = session.getTransport();            //3、使用邮箱的用户名和密码连上邮件服务器,            //    发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,            //    用户名和密码都通过验证之后才能够正常发送邮件给收件人。            ts.connect(host, from, passwd);            //4、创建邮件           Message message = createMixedMail(session, from, to, subject, txt, files);            //5、发送邮件            ts.sendMessage(message, message.getAllRecipients());            ts.close();                     }catch(Exception e) {             return false;         }            return true;        }                /**         * 发送文字、附件的邮件         * */        public static MimeMessage createMixedMail(Session session,String from,String to,String subject,String txt,List<File> files) throws Exception {            //创建邮件            MimeMessage message = new MimeMessage(session);                        //设置邮件的基本信息            message.setFrom(new InternetAddress(from));            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));            message.setSubject(subject);                        //正文            MimeBodyPart text = new MimeBodyPart();            text.setContent(txt,"text/html;charset=UTF-8");                        //图片//            MimeBodyPart image = new MimeBodyPart();//            image.setDataHandler(new DataHandler(new FileDataSource("src\\check.png")));//            image.setContentID("aaa.jpg");                        //附件            List<MimeBodyPart> mimeBodyParts = new ArrayList<MimeBodyPart>();            for(int i=0;i<files.size();i++) {                MimeBodyPart attach = new MimeBodyPart();                DataHandler dh = new DataHandler(new FileDataSource(files.get(i)));                attach.setDataHandler(dh);                attach.setFileName(MimeUtility.encodeWord(dh.getName()));                mimeBodyParts.add(attach);            }                        //描述关系:正文和图片            MimeMultipart mp1 = new MimeMultipart();            mp1.addBodyPart(text);           // mp1.addBodyPart(image);            mp1.setSubType("related");                        //描述关系:正文和附件            MimeMultipart mp2 = new MimeMultipart();            for(int i=0;i<mimeBodyParts.size();i++) {                mp2.addBodyPart(mimeBodyParts.get(i));            }          //  mp2.addBodyPart(attach2);                        //代表正文的bodypart            MimeBodyPart content = new MimeBodyPart();            content.setContent(mp1);            mp2.addBodyPart(content);            mp2.setSubType("mixed");                        message.setContent(mp2);            message.saveChanges();                       // message.writeTo(new FileOutputStream("D:\\MixedMail.eml"));            //返回创建好的的邮件            return message;        }}
package com.assess.util;import java.io.File;import java.util.ArrayList;import java.util.List;public class Main {        private static final String HOST = "smtp.163.com";    private static final String PROTOCOL = "smtp";    private static final String AUTH = "true";        private static final String USER_NAME = "135****920@163.com";//发件人邮箱    private static final String PASSWORD = "1234";//密码    private static final String RECRIVER ="139****893@qq.com";//接收人邮箱        public static void main(String[] args) {        List<File> files = new ArrayList<File>();        File f = new File("src\\备忘.rar");        File f2 = new File("src\\check.png");        files.add(f);        files.add(f2);        if(SendmailUtil.send(USER_NAME, PASSWORD, RECRIVER, "主题信息", "内容信息", files, HOST, PROTOCOL, AUTH)) {                        System.out.println("success");        }    }}

 

java发送email