1.需求
公司有個需求要將頻繁登錄服務器的ip地址及時進行郵件和短信報警,故要寫一個發送郵件和短信的程序
2.問題描述
從網上down了一些java代碼,不是較爲理想,大部分都使用javamail來實現,發送到網易郵箱沒什麼問題,但qq郵箱不行,網易郵箱接收到郵件後提醒不及時,安裝了360郵箱通後還是不太理想,故還是要解決發送到QQ郵箱的問題
3.解決
最新QQ郵箱機制要將輸入密碼的地方改成開啓pop3/smtp的時候提供的驗證碼 即可
4.代碼
發送到QQ郵箱
private static boolean send_qqmail(String strMail, String strTitle, String strText){
boolean bret = false;
try
{
final Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.user", "[email protected]");
props.put("mail.password", "xxxxx");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.starttls.enable", "true");
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
Session mailSession = Session.getInstance(props, authenticator);
MimeMessage message = new MimeMessage(mailSession);
String username = props.getProperty("mail.user");
InternetAddress form = new InternetAddress(username);
message.setFrom(form);
InternetAddress to = new InternetAddress(strMail);
message.setRecipient(RecipientType.TO, to);
message.setSubject(strTitle);
message.setContent(strText, "text/html;charset=UTF-8");
Transport.send(message);
bret = true;
}
catch (AddressException e) {
e.printStackTrace();
}
catch (MessagingException e) {
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
return bret;
}
發送到網易郵箱
private static boolean send_163mail(String strMail, String strTitle, String strText){
boolean bret = false;
try
{
final Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.user", "[email protected]");
props.put("mail.password", "xxxx");
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
Session mailSession = Session.getInstance(props, authenticator);
MimeMessage message = new MimeMessage(mailSession);
String username = props.getProperty("mail.user");
InternetAddress form = new InternetAddress(username);
message.setFrom(form);
InternetAddress to = new InternetAddress(strMail);
message.setRecipient(RecipientType.TO, to);
message.setSubject(strTitle);
message.setContent(strText, "text/html;charset=UTF-8");
Transport.send(message);
bret = true;
}
catch (AddressException e) {
e.printStackTrace();
}
catch (MessagingException e) {
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
return bret;
}
測試部分
import java.util.Properties
import javax.mail.Authenticator
import javax.mail.Message.RecipientType
import javax.mail.MessagingException
import javax.mail.internet.AddressException
import javax.mail.PasswordAuthentication
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
public static void main(String[] args) {
// TODO Auto-generated method stub
if (send_qqmail("[email protected]", "測試QQ郵箱發送", "<body><p>你們好嗎</p></body>"))
System.out.println("QQ郵件發送成功")
if (send_163mail("[email protected]", "測試網易郵箱發送", "<body><p>你們好嗎</p></body>"))
System.out.println("網易郵件發送成功")
}
參考文章