Java Mail 例子
來源:互聯網
上載者:User
網上很多,不過一般都是沒有smtp驗證的,下邊是一段範例程式碼:
不能直接啟動並執行,不過,可以看看裡面關於驗證的部分。
//發送郵件函數
public boolean sendMail(String mailTo,String mailSubject,String mailBody){
//發送email
try {
//default account information
String smtpServer = "smtp.smtpserver.com";
String smtpAuth = "true";
String smtpUser = "username";
String smtpPassword = "password";
String From = "from@yourserver.com";
String To = mailTo;
String Subject = mailSubject;
String Text = mailBody;
java.util.ResourceBundle resBundle;
resBundle = java.util.ResourceBundle.getBundle("mailinfo",
Locale.SIMPLIFIED_CHINESE);
if (resBundle != null) {
smtpServer = resBundle.getString("mail.smtp.host");
smtpAuth = resBundle.getString("mail.smtp.auth");
smtpUser = resBundle.getString("mail.smtp.user");
smtpPassword = resBundle.getString("mail.smtp.password");
From = resBundle.getString("mail.smtp.from");
}
Properties props = new Properties();
Session sendMailSession;
Transport transport;
props.put("mail.smtp.host", smtpServer);