zoukankan      html  css  js  c++  java
  • javamail邮件发送

    第一步:maven引入javamail相关jar包

    <!-- javamail邮件发送 -->
    		<dependency>
    			<groupId>javax.mail</groupId>
    			<artifactId>mail</artifactId>
    			<version>1.4.4</version>
    		</dependency>
    <!-- javamail邮件发送 -->

        

    第二步:工具类已测试(注意:发送人邮箱要开启smtp服务,使用授权码作为密码)

    package com.hailian.modules.credit.utils;

    import java.util.Date;
    import java.util.List;
    import java.util.Properties;
    import java.util.Random;

    import javax.mail.Address;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;

    public class SendMailUtil {
    // //发件人地址
    public static String SenderAddress = "******@qq.com";

    // //发件人账户名
    public static String SenderAccount = "*********8@qq.com";

    // //发件人账户密码
    public static String SenderPassword = "*********************";

    //收件人地址
    public String recipientAddress;
    public String recipientAddressCC;
    //邮件主题
    public String title;
    //邮件内容
    public String content;




    public SendMailUtil(String recipientAddress, String recipientAddressCC, String title, String content) {
    super();
    this.recipientAddress = recipientAddress;
    this.recipientAddressCC = recipientAddressCC;
    this.title = title;
    this.content = content;
    }

    public void sendMail() throws Exception {
    //1、连接邮件服务器的参数配置
    Properties props = new Properties();
    //设置用户的认证方式
    props.setProperty("mail.smtp.auth", "true");
    //设置传输协议
    props.setProperty("mail.transport.protocol", "smtp");
    //设置发件人的SMTP服务器地址
    props.setProperty("mail.smtp.host", "smtp.qq.com");
    //2、创建定义整个应用程序所需的环境信息的 Session 对象
    Session session = Session.getInstance(props);
    //设置调试信息在控制台打印出来
    session.setDebug(true);
    //3、创建邮件的实例对象
    Message msg = getMimeMessage(session,title,content);

    //4、根据session对象获取邮件传输对象Transport
    Transport transport = session.getTransport();
    //设置发件人的账户名和密码
    transport.connect(SenderAccount, SenderPassword);
    //发送邮件,并发送到所有收件人地址,message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
    transport.sendMessage(msg,msg.getAllRecipients());

    //如果只想发送给指定的人,可以如下写法
    //transport.sendMessage(msg, new Address[]{new InternetAddress("xxx@qq.com")});

    //5、关闭邮件连接
    transport.close();
    }

    /**
    * 获得创建一封邮件的实例对象
    * @param session
    * @return
    * @throws MessagingException
    * @throws AddressException
    */
    public MimeMessage getMimeMessage(Session session,String title,String content) throws Exception{
    //创建一封邮件的实例对象
    MimeMessage msg = new MimeMessage(session);
    //防止成为垃圾邮件,披上outlook的马甲
    msg.addHeader("X-Mailer","Microsoft Outlook Express 6.00.2900.2869");
    //设置发件人地址
    msg.setFrom(new InternetAddress(SenderAddress));
    /**
    * 设置收件人地址(可以增加多个收件人、抄送、密送),即下面这一行代码书写多行
    * MimeMessage.RecipientType.TO:发送
    * MimeMessage.RecipientType.CC:抄送
    * MimeMessage.RecipientType.BCC:密送
    */
    // 设置邮件接收方
    Address[] internetAddressTo = new InternetAddress().parse(recipientAddress);
    // msg.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(recipientAddress));
    msg.setRecipients(MimeMessage.RecipientType.TO,internetAddressTo);
    msg.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(SenderAddress));
    //设置邮件主题
    msg.setSubject(title,"UTF-8");
    StringBuffer messageText=new StringBuffer();//内容以html格式发送,防止被当成垃圾邮件
    messageText.append(content);

    //设置邮件正文
    msg.setContent(messageText.toString(), "text/html;charset=UTF-8");
    //设置邮件的发送时间,默认立即发送
    msg.setSentDate(new Date());

    return msg;
    }
    public static String sendMailCode(String recipientAddress) {
    String title="这是一封验证码邮件";
    String code=getCode();
    String content="您的验证码是:"+code+"。如果不是本人操作请忽略。";

    try {
    new SendMailUtil(recipientAddress, recipientAddress, title, content).sendMail();
    } catch (Exception e) {
    e.printStackTrace();
    return code;
    }
    return code;
    }

    public static void main(String[] args) throws Exception {
    sendMailCode("15269274025@163.com");
    }
    /**
    * 生成邮箱验证码
    * @author doushuihai
    * @date 2018年10月10日上午11:03:44
    * @TODO
    */
    public static String getCode(){
    StringBuffer sb=new StringBuffer();
    Random random=new Random();
    for(int i=0;i<6;i++){
    sb.append(random.nextInt(10));
    }
    return sb.toString();
    }
    }

     调用方式:

    public static String sendMailCode(String recipientAddress) {
    String title="这是一封验证码邮件";
    String code=getCode();
    String content="您的验证码是:"+code+"。如果不是本人操作请忽略。";

    try {
    new SendMailUtil(recipientAddress, recipientAddress, title, content).sendMail();
    } catch (Exception e) {
    e.printStackTrace();
    return code;
    }
    return code;
    }

     附加:随着项目的进行 ,根据开发要求希望根据网络url文档作为附件发送。这也是可以实现的

      

    package com.hailian.modules.credit.utils;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.security.Security;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Random;
    
    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.NoSuchProviderException;
    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.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    import javax.mail.util.ByteArrayDataSource;
    
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    
    import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;
    
    public class SendMailUtil {
    //     //发件人地址
    //    public static String SenderAddress = "2530644578@qq.com";
        public static String SenderAddress = "international@inter-credit.net";
    
    //    //发件人账户名
    //    public static String SenderAccount = "2530644578@qq.com";
        public static String SenderAccount = "international@inter-credit.net";
    
    //    //发件人账户密码
    //    public static String SenderPassword = "typwolfiqocrecaf";
        public static String SenderPassword = "Chris777";
        
        //收件人地址
        public static  String recipientAddress;
        public  String recipientAddressCC;
        //邮件主题
        public static String title;
        //邮件内容
        public static String content;
        public  static List<Map<String,String>> list;
       
    
    
       
        
      
        public SendMailUtil(String recipientAddress,String recipientAddressCC,String title, String content,List<Map<String,String>> list) {
            super();
            this.recipientAddress = recipientAddress;
            this.recipientAddressCC = recipientAddressCC;
            this.title = title;
            this.content = content;
            this.list = list;
        }
    
         public  boolean sendEmail() throws Exception  {
            boolean result=false;
            try {
                Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
                Properties props = new Properties();
                props.setProperty("mail.smtp.socketFactory.class",
                        "javax.net.ssl.SSLSocketFactory");
                props.setProperty("mail.smtp.socketFactory.fallback", "false");
                props.setProperty("mail.store.protocol", "smtp");
                props.setProperty("mail.smtp.host", "smtp.inter-credit.net");
                props.setProperty("mail.smtp.port", "465");
                props.setProperty("mail.smtp.socketFactory.port", "465");
                props.put("mail.smtp.auth", "true");
                Session session = Session.getInstance(props, new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(SenderAddress, SenderPassword);
                    }
                });
                Message msg = getMimeMessage(session,title,content,list);
                Transport.send(msg);
                result=true;
                return result;
            } catch (NoSuchProviderException e) {
                e.printStackTrace();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            return result;
        }
        public static InputStream  downLoadFromUrl(String urlStr) throws IOException{
            URL url = new URL(urlStr);  
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
                    //设置超时间为3秒
            conn.setConnectTimeout(3*1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //得到输入流
            InputStream inputStream = conn.getInputStream();  
            return inputStream;
     }
        
        /**
         * 获得创建一封邮件的实例对象
         * @param session
         * @return
         * @throws MessagingException
         * @throws AddressException
         */
        public  MimeMessage getMimeMessage(Session session,String title,String content,List<Map<String,String>> list) throws Exception{
            //创建一封邮件的实例对象
            MimeMessage msg = new MimeMessage(session);
            String nick=javax.mail.internet.MimeUtility.encodeText("Inter-Credit"); 
            msg.setFrom(new InternetAddress(SenderAddress, nick));
            msg.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(recipientAddress, false));
            msg.addRecipients(Message.RecipientType.CC, InternetAddress.parse(SenderAddress));
            msg.addRecipients(Message.RecipientType.CC, InternetAddress.parse(recipientAddressCC));
            msg.setSubject(title);
            msg.setSentDate(new Date());
            
            MimeMultipart multipart = new MimeMultipart("mixed");
            // 邮件内容,采用HTML格式
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.removeHeader("Content-Type");
            messageBodyPart.removeHeader("Content-Transfer-Encoding");
            messageBodyPart.addHeader("Content-Type", "text/html; charset=gbk");
            messageBodyPart.addHeader("Content-Transfer-Encoding", "base64");
            
            messageBodyPart.setContent(content, "text/html;charset=GBK");
            
            
            multipart.addBodyPart(messageBodyPart);
            //内嵌图片
            try {
                //添加附件
    //            messageBodyPart=new MimeBodyPart();
    //            DataSource dataSource1=new FileDataSource("d:/aa.doc");
    //            dataHandler=new DataHandler(dataSource1);
    //            messageBodyPart.setDataHandler(dataHandler);
    //            messageBodyPart.setFileName(MimeUtility.encodeText(dataSource1.getName()));
                if(CollectionUtils.isNotEmpty(list) && list != null){
                    for(Map<String,String> map:list){
                        for(String name : map.keySet()){
                               String fileURL = map.get(name);
                               messageBodyPart=new MimeBodyPart();
                                 InputStream is=downLoadFromUrl(fileURL);
                                 //DataSource dataSource1=new FileDataSource("d:/aa.doc");
                                 DataSource dataSource1=new ByteArrayDataSource(is, "application/png");
                                 DataHandler dataHandler=new DataHandler(dataSource1);
                                 messageBodyPart.setDataHandler(dataHandler);
                                 String subStringB = fileURL.substring(fileURL.lastIndexOf("/")+1);
    //                             messageBodyPart.setFileName(MimeUtility.encodeText(subStringB));
                                 messageBodyPart.setFileName(MimeUtility.encodeText(name));
                                 
                                 multipart.addBodyPart(messageBodyPart);
                              }
                    }
                    
                }
               
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          
            
            msg.setContent(multipart);
            msg.saveChanges();
             
            return msg;
        }
      
        public static void main(String[] args) throws Exception {
    //        sendMailCode("dou_shai@163.com");
            //new SendMailUtil("15269274025@163.com", "", "你好", "mycontent", "http://60.205.229.238:9980/zhengxin_File/2018-11-16/1a183ad043a64af0bde653aa718cd144.doc").sendEmail();
            List<Map<String,String>> list=new ArrayList<Map<String,String>>();
            Map<String,String> map=new HashMap<String, String>();
            map.put("哈哈.doc", "http://60.205.229.238:9980/zhengxin_File/2018-11-16/1a183ad043a64af0bde653aa718cd144.doc");
            list.add(map);
            new SendMailUtil("dou_shuihai@163.com", "", "你好", "mycontent", list).sendEmail();
            System.out.println("ok");
            
        }
       
    }

        

  • 相关阅读:
    将java库转换为.net库(转载)
    web sql设计器(连接)
    [SQLSERVER]SQL中的全文检索(转邹建)
    网络最经典命令行-网络安全工作者的必杀技
    无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。
    06毕业设计 VB导出Excel文档
    06毕业设计 VB导出word文档
    C# 交替显示项的DataGird,鼠标上移时转变颜色,退出后能恢复原来颜色
    js.offsetParent属性
    自动提醒IE6访客升级浏览器,
  • 原文地址:https://www.cnblogs.com/dsh2018/p/9670316.html
Copyright © 2011-2022 走看看