首页 > 技术文章 > javaMail发邮件

jackszc 2017-07-11 13:51 原文

普通 25端口 发送方式:
         // 创建Properties 类用于记录邮箱的一些属性
        final Properties props = new Properties();
        // 表示SMTP发送邮件,必须进行身份验证
        props.put("mail.smtp.auth", "true");
        //此处填写SMTP服务器
        props.put("mail.smtp.host", "smtp.qq.com");
        //端口号,QQ邮箱给出了两个端口,但是另一个我一直使用不了,所以就给出这一个587
        props.put("mail.smtp.port", "587");
        // 此处填写你的账号
        props.put("mail.user", "xxxxxxx@qq.com");
        // 此处的密码就是前面说的16位STMP口令
        props.put("mail.password", "xxxxxxxxxxxxxxxxxxx");

        // 构建授权信息,用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator() {

        protected PasswordAuthentication getPasswordAuthentication() {
                // 用户名、密码
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };
        // 使用环境属性和授权信息,创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
        // 创建邮件消息
        MimeMessage message = new MimeMessage(mailSession);
        // 设置发件人
        InternetAddress form = new InternetAddress(
                props.getProperty("mail.user"));
        message.setFrom(form);

        // 设置收件人的邮箱
        InternetAddress to = new InternetAddress("xxxxxxxx@qq.com");
        message.setRecipient(RecipientType.TO, to);

        // 设置邮件标题
        message.setSubject("测试邮件");

        // 设置邮件的内容体
        message.setContent("这是一封测试邮件", "text/html;charset=UTF-8");

        // 最后当然就是发送邮件啦
        Transport.send(message);

 

ssl   465发送方式:

package com.boredou.easypay.biz.util;

import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Email {


        /**
         * 使用加密的方式,利用465端口进行传输邮件,开启ssl
         *
         * @param to
         *            为收件人邮箱
         * @param message
         *            发送的消息
         */
        public static void sendEmil(String to, String message) {
            try {
                Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
                final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
                // 设置邮件会话参数
                Properties props = new Properties();
                // 邮箱的发送服务器地址
                props.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
                props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
                props.setProperty("mail.smtp.socketFactory.fallback", "false");
                // 邮箱发送服务器端口,这里设置为465端口
                props.setProperty("mail.smtp.port", "465");
                props.setProperty("mail.smtp.socketFactory.port", "465");
                props.put("mail.smtp.auth", "true");
                //设置发件人帐号密码
                final String username = "1234567@.com";
                final String password = "12345678";
                // 获取到邮箱会话,利用匿名内部类的方式,将发送者邮箱用户名和密码授权给jvm
                Session session = Session.getDefaultInstance(props, new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
                // 通过会话,得到一个邮件,用于发送
                Message msg = new MimeMessage(session);
                // 设置发件人
                msg.setFrom(new InternetAddress("1234567@.com"));
                // 设置收件人,to为收件人,cc为抄送,bcc为密送
                msg.setRecipients(Message.RecipientType.TO

                        , InternetAddress.parse(to, false));
                msg.setRecipients(Message.RecipientType.CC

                        , InternetAddress.parse(to, false));
                msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(to, false));
                msg.setSubject("邮件主题11111");
                // 设置邮件消息
                msg.setText(message);
                // 设置发送的日期
                msg.setSentDate(new Date());

                // 调用Transport的send方法去发送邮件
                Transport.send(msg);

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


    }
public static void main(String[] args) {
        sendEmil("12345678@qq.com","sssssssssssssssss");
}
}

 


   

推荐阅读