問題①: java發送qq郵件出現以下錯誤的解決方法:java
503 Error: need EHLO and AUTH first !spring
pop.put("mail.smtp.auth", "true");//注意value值不能不設置,而且不能是Boolean類型,應爲字符串,不然會報如上所示錯誤問題②: Could not connect to SMTP host: smtp.qq.com, port: 465, response: -1安全
緣由:socket
465端口是爲SMTPS(SMTP-over-SSL)協議服務開放的,這是SMTP協議基於SSL安全協議之上的一種變種協議.ide
解決方法:測試
配置文件須要加上以下代碼便可解決.ui
pop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
附上全部的代碼:this
配置文件:.net
mail: mailhost: smtp.qq.com mailport: 465 user: 郵箱用戶名 password: 密碼(qq爲受權碼) mailFrom: xxx@foxmail.com mailTo: xxx@qq.com,xxx@qq.com,xxx@foxmail.com properties: mail: smtp: auth: true mailtimeout: 1000 mailssl: javax.net.ssl.SSLSocketFactory starttls: enable: true required: truejava代碼:code
@Value("${mailhost}") private String host; @Value("${mailport}") private Integer port; @Value("${user}") private String username; @Value("${password}") private String password; @Value("${mailFrom}") private String from; @Value("${mailTo}") private String [] to; @Value("${auth}") private String auth; @Value("${enable}") private Boolean enable; @Value("${required}") private Boolean required; @Value("${mailtimeout}") private Integer timeout; @Value("${mailssl}") private String ssl; public void onApplicationEvent(ApplicationEvent event) { System.out.println("email cfg...."); System.out.println("print:"+this.toString()); //建立一個發送者對象 JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost(this.host); mailSender.setPort(this.port); mailSender.setUsername(this.username); mailSender.setPassword(this.password); // 加認證機制 Properties pop = new Properties(); pop.put("mail.smtp.host", this.host); // pop.put("mail.smtp.port", this.port);//已經在上面加過 pop.put("mail.smtp.auth", this.auth);//必須是字符串而不能是boolean不然報錯 pop.put("mail.smtp.starttls.enable", this.enable); pop.put("mail.smtp.timeout", this.timeout); pop.put("mail.smtp.socketFactory.class", this.ssl); mailSender.setJavaMailProperties(pop); //組織郵件參數並執行發送 SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(this.from); message.setTo(this.to); message.setSubject("主題:測試,spring boot發送郵件."); message.setText("測 試\n 郵件爲程序發送,是否收到?請回答"); mailSender.send(message); System.out.println("send end...."); } @Override public String toString() { return "EmailConfiguration [host=" + host + ", port=" + port + ", username=" + username + ", password=" + password + ", from=" + from + ", to=" + to + ", auth=" + auth + ", enable=" + enable + ", required=" + required + ", timeout=" + timeout + "]"; }