基于javamail简单实现邮件发送

来源:互联网 发布:蚁群优化算法 matlab 编辑:程序博客网 时间:2024/06/15 05:39

在日常生活中,会比较多的使用到邮件,无论生活还是工作。有QQ邮箱,163邮箱等等。那邮件发送和接受那也会有协议的吧,对下面是常见邮件协议:
SMTP:简单邮件传输协议,用于发送电子邮件的传输协议;

POP3:用于接收电子邮件的标准协议;

IMAP:互联网消息协议,是POP3的替代协议。

代码实现会使用到那些关键类:
Properties类:配置相关参数

Session类:会话对象

Message类:邮件类

Address类:地址类

Authenticator:认证类

Transport:运送类

具体代码实现如下:

import java.util.Date;import java.util.Properties;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;/** * 实现一个email发送功能 * @author chenjingbin * */public class EmailDemo01 {    public static void main(String[] args) {        try {            new EmailDemo01().createSession();        } catch (Exception e) {            // TODO Auto-generated catch block            System.out.println("出现错误了");        }    }    public void createSession() {        //创建配置类,然后把需要的参数加入,这里使用qq邮箱去发送,那就会使用到SSL        Properties properties = new Properties();        properties.put("mail.host", "smtp.qq.com");        properties.put("mail.transport.protocol", "smtp");        properties.put("mail.smtp.auth", "true");        //QQ邮箱需要配置下面参数        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");        properties.put("mail.smtp.port", "465");        properties.put("mail.smtp.socketFactory.port", "465");        //获取会话对象,需要去实现一个继承Authenticator的类,一个是账号,一个是你去QQ邮箱里面设置获取的验证码        Session session = Session.getInstance(properties, new MyAuthenticator("3248412693@qq.com", "**************"));        //启动调试        session.setDebug(true);        //创建消息类,把会话对象传进去        MimeMessage message = new MimeMessage(session);        try {            //设置发送人地址            message.setFrom(new InternetAddress("3248412693@qq.com"));            //设置发送人地址和接受类型            message.addRecipient(Message.RecipientType.TO, new InternetAddress("764024755@qq.com"));            //设置主题            message.setSubject("你好", "utf-8");            //设置发送时间            message.setSentDate(new Date());            //设置发送内容和格式            message.setContent("曾经有一段真挚的爱情放在我的面前,我没有珍惜。直到失去我才追悔莫急。如果上天再给我一次机会的话,我一定会对那个女孩说三个字“我爱你”。如果一定要给这个承诺加上一个期限的话,我希望是“一万年”。", "text/html;charset=utf-8");        } catch (MessagingException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        //发送邮件        try {            //通过会话对象去获得传输对象            Transport transport = session.getTransport("smtp");            //连接用户            transport.connect("smtp.qq.com", "3248412693@qq.com", "*****************");            //发送            transport.send(message);            System.out.println("发送成功");        } catch (MessagingException e) {            // TODO Auto-generated catch block            System.out.println("发送失败");            e.printStackTrace();        }    }}//------------------------------------------------import javax.mail.Authenticator;import javax.mail.PasswordAuthentication;public class MyAuthenticator extends Authenticator {    private String usernameString;    private String passwordString;    public MyAuthenticator(String usernameString, String passwordString) {        this.usernameString = usernameString;        this.passwordString = passwordString;    }    @Override    protected PasswordAuthentication getPasswordAuthentication() {        // TODO Auto-generated method stub        return new PasswordAuthentication(usernameString, passwordString);    }}

编写邮件发送的时候会出现这样的情况:
1、Unrecognized SSL message, plaintext connection。
2、530 Error: A secure connection is requiered(such as ssl)
要么是验证码过期了 ,要么是自己没有设置SSLSocketFactory类在配置文件中,因为QQ邮箱需要SSL而163不用。

0 0
原创粉丝点击