java使用smtp发送邮件问题
来源:互联网 发布:私募研究员 知乎 编辑:程序博客网 时间:2024/05/22 08:04
原先使用smtp通过邮箱发送邮件不需要认证,但是后来发现如果使用的是QQ邮箱会报认证失败的异常。原来QQ邮箱是需要ssl认证的,需要先到QQ邮箱里的 设置--账户--POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 开通POP3/SMTP服务获得授权码,在transport.connect的时候传的是这个授权码,并且需要在props里添加几个参数,于是在原来的基础上做了修改加了一个了方法,QQ邮箱也可以发送了.
java使用smtp发送邮件代码
开通SMTP服务
props添加代码
props.put("mail.smtp.ssl.enable", "true");props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");props.setProperty("mail.smtp.socketFactory.port", "465");
java使用smtp发送邮件代码
import java.util.Properties;import java.util.StringTokenizer;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;public class SendMail { String SMTPServer ; String user ; String password ; Properties props = new Properties(); Session sendMailSession; Store store; Transport transport; MimeMessage newMessage; public SendMail(String SMTPServer,String user,String password){ this.SMTPServer = SMTPServer; this.user = user; this.password = password; try { props.put("mail.smtp.host", SMTPServer); props.put("mail.smtp.auth", "true"); qqMailConf(SMTPServer, props); sendMailSession=Session.getInstance(props, null); sendMailSession.setDebug(false); newMessage = new MimeMessage(sendMailSession); }catch(Exception e) { System.err.println(e.getMessage());}} /** * qq邮箱设置 */ public void qqMailConf(String SMTPServer, Properties props){ if(SMTPServer.indexOf("smtp.qq.com")!=-1){ props.put("mail.smtp.ssl.enable", "true"); props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.setProperty("mail.smtp.socketFactory.port", "465"); } } /** * 下面是邮件发送的方法 * 参数注释 * from: 发信人邮箱地址 * to: 收信人邮箱地址 * cc: 抄送人 可以为null * bcc: 暗送人 可以为null * subject:主题 * content:内容 */ public String send(String from,String to,String cc,String bcc,String subject,String content) { try { System.out.println("hhhh"); newMessage.setFrom(new InternetAddress(from, "")); newMessage.setSubject(subject); newMessage.setSentDate(new java.util.Date()); StringTokenizer tokenTO = new StringTokenizer(to, ","); InternetAddress[] addrArrTO = new InternetAddress[tokenTO.countTokens()]; int i = 0; while(tokenTO.hasMoreTokens()) { addrArrTO[i] = new InternetAddress(tokenTO.nextToken().toString()); i++; } newMessage.saveChanges(); transport = sendMailSession.getTransport("smtp"); transport.connect(SMTPServer, user, password); if(i>0) newMessage.setRecipients(Message.RecipientType.TO, addrArrTO); else if(to!=null) newMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); if(cc!=null) newMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc)); if(bcc!=null) newMessage.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc)); newMessage.setText(content); transport.sendMessage(newMessage, newMessage.getAllRecipients()); } catch(Exception e) { e.printStackTrace(); return e.toString();//邮件发送失败 } return "OK"; //邮件发送成功 } public static void main(String[] arg) { SendMail sendMail = new SendMail("smtp.qq.com", "*******@qq.com", "..授权码.."); for (int i = 0; i < 1; i++) { String msg = sendMail.send("*******@qq.com","test@qq.com",null,null,"测试subject" + 1,"测试contents"); System.out.println("Message:"+ i + msg); } } }
0 0
- java使用smtp发送邮件问题
- php 使用smtp发送邮件问题
- 使用SMTP发送邮件
- 使用SMTP发送邮件
- 使用SMTP发送邮件
- 使用SMTP发送邮件
- Java SMTP发送邮件
- java smtp发送邮件
- 使用SMTP协议发送邮件
- 使用cackPHP发送smtp邮件
- C# 使用smtp发送邮件
- PHP使用SMTP发送邮件
- bugzilla使用SMTP发送邮件
- 使用smtp命令发送邮件
- python使用smtp发送邮件
- Python使用SMTP发送邮件
- Python -- 使用SMTP发送邮件
- PHP使用smtp发送邮件
- Ubuntu创建android studio桌面图标
- 测试环境搭建
- Eclipse打不开,提示: An error has occurred. see the log file解决办法
- WEB服务器、应用程序服务器、HTTP服务器区别
- 宏定义
- java使用smtp发送邮件问题
- Systemd入门教程:命令篇
- 一笔画问题【图论】
- 刨根问底 HTTP 和 WebSocket 协议(上)
- java中Class对象详解和类名.class, class.forName(), getClass()区别
- UVA 10689 矩阵快速幂 + 快速幂取模
- hadoop 文件分块,block与split关系
- Eclipse打不开,提示: An error has occurred. see the log file解决办法
- 刨根问底 HTTP 和 WebSocket 协议(下)