Java的qq邮箱发送案例
1、首先开启qq邮箱账户的发送邮件的许可
默认状态是关闭状态,点击开启会让你发送固定语句到指定账户,成功后会生成一个授权码,记住你的授权码
2、发送邮箱代码
sendEmail
public class SendEmail {public static void sendRegisterEmail(String username,String code,String email){try {//创建一个配置文件并保存Properties properties = new Properties();properties.setProperty("mail.host", "smtp.qq.com");properties.setProperty("mail.transport.protocol", "smtp");properties.setProperty("mail.smtp.auth", "true");//QQ存在一个特性设置SSL加密MailSSLSocketFactory sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);properties.put("mail.smtp.ssl.enable", "true");properties.put("mail.smtp.ssl.socketFactory", sf);//创建一个session对象Session session = Session.getDefaultInstance(properties, new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {//配置发送人右键的账号和授权码return new PasswordAuthentication("发送人的qq@qq.com", "发送人的授权码");}});//开启debug模式session.setDebug(true);//获取连接对象Transport transport = session.getTransport();//连接服务器//配置发送人右键的账号和授权码transport.connect("smtp.qq.com", "发送人的qq@qq.com", "发送人的授权码");//创建邮件对象MimeMessage mimeMessage = new MimeMessage(session);//邮件发送人mimeMessage.setFrom(new InternetAddress("发送人的qq@qq.com"));//邮件接收人
// mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(email));mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("收件人的qq@qq.com"));String content="<a href='http://locahost:8080/test/ActivationServlet?username=\"+username+\"&code=\"+code+'>点击激活</a>";//邮件标题mimeMessage.setSubject("注册邮件");//邮件内容mimeMessage.setContent(content, "text/html;charset=UTF-8");//发送邮件transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());//关闭连接transport.close();}catch (Exception e){e.printStackTrace();throw new RuntimeException("发送注册邮件是失败!");}}public static void main(String[] args) {//测试发送邮件SendEmail.sendRegisterEmail("zhangsan","0098",null);}}
3、邮件常用案例
普通的发送邮件业务、账号激活业务、修改密码业务等。
4、邮箱需要用到的jar包
由于上传太麻烦了,可以自己百度
Java的qq邮箱发送案例
1、首先开启qq邮箱账户的发送邮件的许可
默认状态是关闭状态,点击开启会让你发送固定语句到指定账户,成功后会生成一个授权码,记住你的授权码
2、发送邮箱代码
sendEmail
public class SendEmail {public static void sendRegisterEmail(String username,String code,String email){try {//创建一个配置文件并保存Properties properties = new Properties();properties.setProperty("mail.host", "smtp.qq.com");properties.setProperty("mail.transport.protocol", "smtp");properties.setProperty("mail.smtp.auth", "true");//QQ存在一个特性设置SSL加密MailSSLSocketFactory sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);properties.put("mail.smtp.ssl.enable", "true");properties.put("mail.smtp.ssl.socketFactory", sf);//创建一个session对象Session session = Session.getDefaultInstance(properties, new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {//配置发送人右键的账号和授权码return new PasswordAuthentication("发送人的qq@qq.com", "发送人的授权码");}});//开启debug模式session.setDebug(true);//获取连接对象Transport transport = session.getTransport();//连接服务器//配置发送人右键的账号和授权码transport.connect("smtp.qq.com", "发送人的qq@qq.com", "发送人的授权码");//创建邮件对象MimeMessage mimeMessage = new MimeMessage(session);//邮件发送人mimeMessage.setFrom(new InternetAddress("发送人的qq@qq.com"));//邮件接收人
// mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(email));mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("收件人的qq@qq.com"));String content="<a href='http://locahost:8080/test/ActivationServlet?username=\"+username+\"&code=\"+code+'>点击激活</a>";//邮件标题mimeMessage.setSubject("注册邮件");//邮件内容mimeMessage.setContent(content, "text/html;charset=UTF-8");//发送邮件transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());//关闭连接transport.close();}catch (Exception e){e.printStackTrace();throw new RuntimeException("发送注册邮件是失败!");}}public static void main(String[] args) {//测试发送邮件SendEmail.sendRegisterEmail("zhangsan","0098",null);}}
3、邮件常用案例
普通的发送邮件业务、账号激活业务、修改密码业务等。
4、邮箱需要用到的jar包
由于上传太麻烦了,可以自己百度