使用javaxmail 开通新浪,163发送邮件,外加通过ssl使用465端口发送邮件,解决阿里服务器25端口未开放无法发送邮件

1.使用新浪邮箱发送email

      首先注册一个新浪邮箱账号,再开通新浪邮箱的smtp服务,可百度咨询查找  https://jingyan.baidu.com/article/59a015e3af0f10f7948865eb.html

下面是代码示例

public static void sendEmailForSina() throws Exception{
        Properties props = new Properties() ;

        props.setProperty("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getInstance(props);
        // 这里设置可以查看打印日志
        session.setDebug(true);

        Message message = new MimeMessage(session);
        // 新浪发送邮件必须要有主题哦,否则永远无法发送成功!
        message.setSubject("打个招呼主题");
        message.setText("你很好!");
        // 现在这里发送人必须与认证人一致,否则报错
        message.setFrom(new InternetAddress("xxx@sina.com"));
        //接收人邮箱
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxx@qq.com"));
        Transport transport = session.getTransport();
        try {
            transport.connect("smtp.sina.com.cn", "xxx@sina.com", "xxxxxx") ; //邮箱连接
            transport.sendMessage(message, message.getAllRecipients()) ;
            transport.close();
        } catch (NoSuchProviderException e) {

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

2.通过ssl使用465端口发送邮件,解决阿里服务器25端口未开放无法发送邮件

代码示例

public String sendEmailSsl(String toEmail) throws MessagingException {

        String content = "邮件正文,你好";
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        // Get a Properties object
        Properties props = new Properties();
        props.setProperty("mail.smtp.host", "smtp.sina.com.cn");
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", "465");
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.auth", "true");

        final String username = "xxx@sina.com";//邮箱账号
        final String password = "ca47ea8c8cf4xxx";//授权码
        Session session = Session.getDefaultInstance(props, new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }});
        session.setDebug(true);//打开调试
        Message msg = new MimeMessage(session);

        // 设置发件人和收件人
        msg.setFrom(new InternetAddress("xxx@sina.com"));
        /*List tos = msg1.getTo();
        Address to[] = new InternetAddress[tos.size()];
        for(int i=0;i){
            to[i] = new InternetAddress(tos.get(i));
        }*/
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
        // 多个收件人地址
        //msg.setRecipients(Message.RecipientType.TO, to);
        msg.setSubject("有新客户来了"); // 标题
        msg.setText(content);// 内容
        msg.setSentDate(new Date());
        Transport.send(msg);
        System.out.println("EmailUtil ssl协议邮件发送打印" +msg.toString());
        return "success";
    }

3.使用163邮箱发送邮件同样需要注册一个163邮箱账号,并且开通smtp服务 https://jingyan.baidu.com/article/7f41ecec3e8d35593d095c93.html

    代码示例

// 发件人 账号和密码
    public static final String MY_EMAIL_ACCOUNT = "xx@163.com";
    public static final String MY_EMAIL_PASSWORD = "xx";// 密码,是你自己的设置的授权码

    // SMTP服务器(这里用的163 SMTP服务器)
    public static final String MEAIL_163_SMTP_HOST = "smtp.163.com";
    public static final String SMTP_163_PORT = "25";// 端口号,这个是163使用到的;QQ的应该是465或者875

    // 收件人
    public static final String RECEIVE_EMAIL_ACCOUNT = "xxx@qq.com";



public static void sendEmailForWy() throws AddressException, MessagingException{
        Properties p = new Properties();
        p.setProperty("mail.smtp.host", MEAIL_163_SMTP_HOST);
        p.setProperty("mail.smtp.port", SMTP_163_PORT);
        p.setProperty("mail.smtp.socketFactory.port", SMTP_163_PORT);
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.socketFactory.class", "SSL_FACTORY");

        Session session = Session.getInstance(p, new Authenticator() {
            // 设置认证账户信息
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(MY_EMAIL_ACCOUNT, MY_EMAIL_PASSWORD);
            }
        });
        session.setDebug(true);
        System.out.println("创建邮件");
        MimeMessage message = new MimeMessage(session);
        // 发件人
        message.setFrom(new InternetAddress(MY_EMAIL_ACCOUNT));
        // 收件人和抄送人
        message.setRecipients(Message.RecipientType.TO, RECEIVE_EMAIL_ACCOUNT);
//		message.setRecipients(Message.RecipientType.CC, MY_EMAIL_ACCOUNT);

        // 内容(这个内容还不能乱写,有可能会被SMTP拒绝掉;多试几次吧)
        message.setSubject("包裹");
        message.setContent("<h1>李总,您好;你的包裹在前台</h1>", "text/html;charset=UTF-8");
        message.setSentDate(new Date());
        message.saveChanges();
        System.out.println("准备发送");
        Transport.send(message);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值