java发送消息_通过java给qq邮箱发送信息
通過(guò)java程序給qq郵箱發(fā)送信息。
1、第一步:下載mail的jar包:javax.mail.jar
下載地址:https://javaee.github.io/javamail/#Latest_News
2、直接使用下面的代碼
public static voidsendMessages() {try{//創(chuàng)建Properties 類用于記錄郵箱的一些屬性
final Properties props = newProperties();//表示SMTP發(fā)送郵件,必須進(jìn)行身份驗(yàn)證
props.put("mail.smtp.auth", "true");//此處填寫(xiě)SMTP服務(wù)器
props.put("mail.smtp.host", "smtp.qq.com");//端口號(hào),QQ郵箱給出了兩個(gè)端口,這里給出587
props.put("mail.smtp.port", "587");//此處填寫(xiě)你的賬號(hào)
props.put("mail.user", "你的qq號(hào)@qq.com");//此處的密碼就是前面說(shuō)的16位STMP口令//獲取口令
props.put("mail.password", "你的口令");//構(gòu)建授權(quán)信息,用于進(jìn)行SMTP進(jìn)行身份驗(yàn)證
Authenticator authenticator = newAuthenticator() {protectedPasswordAuthentication getPasswordAuthentication() {//用戶名、密碼
String userName = props.getProperty("mail.user");
String password= props.getProperty("mail.password");return newPasswordAuthentication(userName, password);
}
};//使用環(huán)境屬性和授權(quán)信息,創(chuàng)建郵件會(huì)話
Session mailSession =Session.getInstance(props, authenticator);//創(chuàng)建郵件消息
MimeMessage message = newMimeMessage(mailSession);//設(shè)置發(fā)件人
InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
message.setFrom(form);//設(shè)置收件人的郵箱
InternetAddress to = new InternetAddress("收件人郵箱@qq.com");
message.setRecipient(RecipientType.TO, to);//設(shè)置郵件主題
message.setSubject("測(cè)試郵件");//設(shè)置消息日期
message.setSentDate(newDate());//html文件
StringBuilder sb = newStringBuilder();
sb.append("
文本內(nèi)容
");//設(shè)置郵件的內(nèi)容體message.setContent(sb.toString(), "text/html;charset=UTF-8");//最后當(dāng)然就是發(fā)送郵件
Transport.send(message);
System.out.println("發(fā)送成功!");
}catch(AddressException e) {
System.out.println("發(fā)送失敗!"+e.getMessage());
e.printStackTrace();
}catch(MessagingException e) {
System.out.println("發(fā)送失敗!"+e.getMessage());
e.printStackTrace();
}
}
3、上面代碼中主要填寫(xiě)三個(gè)地方
①:發(fā)送的郵箱號(hào)
②:發(fā)送的郵箱號(hào)的口令(在下面會(huì)介紹如何獲取)
③:接收人的郵箱號(hào)
4、這樣就可以發(fā)送郵箱給別人了。
代碼參考:https://blog.csdn.net/qq_41750725/article/details/83831589
5、如何獲取發(fā)送人的口令
①:登錄發(fā)送方的qq郵箱-》找到賬號(hào)
②:拉到下面,找到需要開(kāi)啟的服務(wù),然后把相應(yīng)的服務(wù)開(kāi)啟即可。
③:開(kāi)啟之后,就會(huì)獲取到一個(gè)16位的口令了。
④:具體參考:https://jingyan.baidu.com/article/fedf0737af2b4035ac8977ea.html
上面的方法只是以qq郵箱為例子,其他的郵箱也是差不多同樣的操作。下面提供各個(gè)郵箱的郵件服務(wù)器和端口號(hào)
sina.com:
POP3服務(wù)器地址:pop3.sina.com.cn(端口:110)
SMTP服務(wù)器地址:smtp.sina.com.cn(端口:25)
sinaVIP:
POP3服務(wù)器:pop3.vip.sina.com (端口:110)
SMTP服務(wù)器:smtp.vip.sina.com (端口:25)
sohu.com:
POP3服務(wù)器地址:pop3.sohu.com(端口:110)
SMTP服務(wù)器地址:smtp.sohu.com(端口:25)
126郵箱:
POP3服務(wù)器地址:pop.126.com(端口:110)
SMTP服務(wù)器地址:smtp.126.com(端口:25)
139郵箱:
POP3服務(wù)器地址:POP.139.com(端口:110)
SMTP服務(wù)器地址:SMTP.139.com(端口:25)163.com:
POP3服務(wù)器地址:pop.163.com(端口:110)
SMTP服務(wù)器地址:smtp.163.com(端口:25)
QQ郵箱
POP3服務(wù)器地址:pop.qq.com(端口:110)
SMTP服務(wù)器地址:smtp.qq.com (端口:25)
QQ企業(yè)郵箱
POP3服務(wù)器地址:pop.exmail.qq.com (SSL啟用 端口:995)
SMTP服務(wù)器地址:smtp.exmail.qq.com(SSL啟用 端口:587/465)
yahoo.com:
POP3服務(wù)器地址:pop.mail.yahoo.com
SMTP服務(wù)器地址:smtp.mail.yahoo.com
yahoo.com.cn:
POP3服務(wù)器地址:pop.mail.yahoo.com.cn(端口:995)
SMTP服務(wù)器地址:smtp.mail.yahoo.com.cn(端口:587HotMail
POP3服務(wù)器地址:pop3.live.com (端口:995)
SMTP服務(wù)器地址:smtp.live.com (端口:587)
gmail(google.com)
POP3服務(wù)器地址:pop.gmail.com(SSL啟用 端口:995)
SMTP服務(wù)器地址:smtp.gmail.com(SSL啟用 端口:587)263.net:
POP3服務(wù)器地址:pop3.263.net(端口:110)
SMTP服務(wù)器地址:smtp.263.net(端口:25)263.net.cn:
POP3服務(wù)器地址:pop.263.net.cn(端口:110)
SMTP服務(wù)器地址:smtp.263.net.cn(端口:25)
x263.net:
POP3服務(wù)器地址:pop.x263.net(端口:110)
SMTP服務(wù)器地址:smtp.x263.net(端口:25)
21cn.com:
POP3服務(wù)器地址:pop.21cn.com(端口:110)
SMTP服務(wù)器地址:smtp.21cn.com(端口:25)
Foxmail:
POP3服務(wù)器地址:POP.foxmail.com(端口:110)
SMTP服務(wù)器地址:SMTP.foxmail.com(端口:25)
china.com:
POP3服務(wù)器地址:pop.china.com(端口:110)
SMTP服務(wù)器地址:smtp.china.com(端口:25)
tom.com:
POP3服務(wù)器地址:pop.tom.com(端口:110)
SMTP服務(wù)器地址:smtp.tom.com(端口:25)
etang.com:
POP3服務(wù)器地址:pop.etang.com
SMTP服務(wù)器地址:smtp.etang.com
具體參考:https://blog.csdn.net/smok56888/article/details/50070453#
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎(jiǎng)!定制產(chǎn)品紅包拿不停!總結(jié)
以上是生活随笔為你收集整理的java发送消息_通过java给qq邮箱发送信息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: FlexRay车载网络标准
- 下一篇: After paper reading.