Java-QQ郵箱傳送郵件
阿新 • • 發佈:2020-07-14
steps
When we register an account, we often see the way to send email verification code to verify, so how is this function achieved?
The first step is to log into your QQ email
After successful opening, you will get the authorization code
This authorization code is critical,please save now.
I've already written the code for qqEmail
package Demo; import com.sun.deploy.util.StringUtils; import org.apache.commons.mail.HtmlEmail; import java.util.*; /* writer:zzl date: 2020-5.5 function: send QQEmail interface */ public class QEmail { public static void main(String[] args) { String Email="[email protected]"; sendEamilCode(Email); } public static String sendEamilCode(String eamil) { // 建立HtmlEmail物件 HtmlEmail send = new HtmlEmail(); // 獲取驗證碼 String achieveCode = achieveCode(); try { send.setHostName("smtp.qq.com");// 伺服器名稱 smtp.qq.com send.setSmtpPort(465);// 埠號 /* send.setSSL(true); send.setTLS(true);*/ send.setSSLOnConnect(true);// 開啟SSL服務 send.setCharset("utf-8");// 設定字符集 System.out.println("receiver : "+eamil); send.addTo(eamil); // 接收者的QQEamil pxkryqrpxxhkcaaj send.setFrom("[email protected]", "GRE-ZZL");// 第一個引數是傳送者的QQEamil 第二個引數是傳送者QQ暱稱 send.setAuthentication("[email protected]", "");// 第一個引數是傳送者的QQEamil 第二個引數是授權碼 send.setSubject(""); // 主題 send.setMsg("功能測試\t驗證碼:" + achieveCode + "\t"); // 設定內容 send.send();// 傳送資訊 System.out.println("傳送成功"); } catch (Exception e) { e.printStackTrace(); } return achieveCode; } private static String achieveCode() { Set<String>set=new HashSet<String>(); Random random=new Random(); for (int i=0;i<=10;i++){ int change=random.nextInt(10); String string=String.valueOf(change); set.add(string); if (set.size()==5)break; } List<String>StringList=new ArrayList<String>(set); String s=StringUtils.join(StringList,""); System.out.println(s); return s; } }
import pom.xml settings
<!-- https://mvnrepository.com/artifact/activation/activation --> <dependency> <groupId>activation</groupId> <artifactId>activation</artifactId> <version>1.0.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.5</version> </dependency> <!-- https://mvnrepository.com/artifact/mail/mail --> <dependency > <groupId >javax.mail </groupId > <artifactId >mail </artifactId > <version >1.4.6 </version > </dependency > <dependency > <groupId >com.sun.mail </groupId > <artifactId >javax.mail </artifactId > <version >1.5.5 </version > </dependency >