/*
* Created on 2009-1-8
*/
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* @author gjg
*/
public class SenderWithSMTPVer {
String host = "82.0.191.1";
/**
*
* @param from
* 寄件者的地址
* @param to
* 收件者地址
* @param titleMsg
* 郵件標題
*/
public void sendMail(String from, String to, String titleMsg) {
Properties props = new Properties();
props.put("mail.smtp.host", host);// 指定SMTP伺服器
props.put("mail.smtp.auth", "false");// 指定是否需要SMTP驗證,為false時,不用指定使用者名稱、密碼
try {
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);// 是否在控制台顯示debug資訊
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));// 寄件者
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));// 收件者
// 將中文轉化為GB2312編碼
message.setSubject(titleMsg, "GB2312"); // 郵件主題
message.setContent("<a href='http://www.qq.com'>請點擊此連結!</a>",
"text/html;charset=gb2312");// 郵件內容
message.saveChanges();
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, null, null);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
SenderWithSMTPVer sm = new SenderWithSMTPVer();
sm.sendMail("abc@sohu.com", "kjc_zh/xj/ccb@ccb", "Test Mail!");
}
}