使用java发送邮件,多邮箱发送

使用java发送邮件,多邮箱发送

先说说我遇到的坑

我在网上看了怎么用Java发送邮件,我为了高可靠,我注册了三个邮箱分别是163,foxmail,outlook
在163发送过多就不能成功发送,折腾了好久包括抄送等等都没有达到预期的效果。组后只能弃用。但是我在最后发现变换ip可以解决这个问题,但是我觉得不是长远之计,所以只能放弃。
在多个邮箱一起用的时候又出现一个问题,我刚开始以为是我代码写的有问题,但是我惊奇的发现每一个单独测试都没问题,但是一起执行就会报错,无论前面的发送代码能不能执行,都会报错。然后我发现163加入ssl就可以跑,但是foxmail和outlook就不行,最后到墙上看了看,我找到了问题,是缺少mail.smtp.starttls.enable,我之前写的是mail.smtp.ssls.enable,
到此松了口气,困扰我好久的问题终于解决了

实现思路

我的发送实现思路是这样的,把数据存入队列,然后开一个线程单独处理发送请求(只是测试,加入线程完全是因为这玩意发送太慢了,至今没有找到加速的办法)。

话不多说了,贴代码

 public int sendemailby163(String address, String username , String code, String date) throws Exception {
        String emailfrom="*****@163.com";
        Properties properties=System.getProperties();
        properties.put("mail.smtp.starttls.enable","true");
        Properties newproperties=setsmtp(properties,"smtp.163.com");
        Session session=Session.getInstance(newproperties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("账号","授权码");
            }
        });
        if (redisConnect.delEmailVcode(redisConnect.getRedisConnect(),address))
        {
            Transport.send(setmessage(session,emailfrom,address,username,code,date,"163"));
            System.out.println("163发送成功");
            redisConnect.saveData15(redisConnect.getRedisConnect(),address,code);
            System.out.println("redis数据存入成功");

        }

        return 1;
    }

    public int sendemailbyoutlook(String address, String username , String code, String date) throws Exception {
        String emailfrom="*****@outlook.com";
        Properties properties=System.getProperties();
        Properties newproperties=setsmtp(properties,"smtp.office365.com");
        newproperties.put("mail.smtp.starttls.enable","true");
        newproperties.put("mail.smtp.port","587");

        Session session=Session.getInstance(newproperties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("账号","密码");
            }
        });
        if (redisConnect.delEmailVcode(redisConnect.getRedisConnect(),address))
        {
 Transport.send(setmessage(session,emailfrom,address,username,code,date,"outlook"));
            System.out.println("outlook发送成功");
            redisConnect.saveData15(redisConnect.getRedisConnect(),address,code);
            System.out.println("redis数据存入成功");

        }

        return 1;
    }

    public int sendemailbyqq(String address, String username , String code, String date) throws Exception {
        String emailfrom="*****@foxmail.com";
        Properties properties=System.getProperties();
        Properties newproperties=setsmtp(properties,"smtp.qq.com");
        MailSSLSocketFactory mailSSLSocketFactory=new MailSSLSocketFactory();
        mailSSLSocketFactory.setTrustAllHosts(true);
        newproperties.put("mail.smtp.starttls.enable","true");
        newproperties.put("mail.smtp.starttls.socketFactory",mailSSLSocketFactory);

        Session session=Session.getInstance(newproperties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("账号","授权码");
            }
        });
        if (redisConnect.delEmailVcode(redisConnect.getRedisConnect(),address))
        {
            System.out.println(session.getProperties());
            Transport.send(setmessage(session,emailfrom,address,username,code,date,"foxmail"));
            System.out.println("QQ发送成功");
            redisConnect.saveData15(redisConnect.getRedisConnect(),address,code);
            System.out.println("redis数据存入成功");

        }

        return 1;
    }

    public Message setmessage(Session session,String emailfrom,String address,String username,String code,String date,String string)
    {
        Message message=new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(emailfrom));
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(address));
            message.setSubject("******");
            String content=content1+username+content2+code+content3+date+content4;
            message.setContent(content,"text/html;charset=UTF-8");
        } catch (MessagingException e) {
            e.printStackTrace();
        }finally {
            return message;
        }

    }

    public Properties setsmtp(Properties properties,String host)
    {
        properties.put("mail.smtp.timeout",1000);
        properties.put("mail.smtp.connectiontimeout",1000);
        properties.put("mail.smtp.writetimeout",1000);
        properties.setProperty("mail.smtp.host",host);
        properties.setProperty("mail.smtp.auth","true");
        return properties;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值