ลองเล่นดูหน่อย..
เริ่มแรกไป download JavaMail API มาก่อน
จากนั้นก็ซัดไป
import java.util.Date;
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;
public class Hello {
public static void main(String[] args) {
try {
String smtpServer = "smtp.yourdomain.com";
String to = "youraccount@yourdomain.com";
String from = "me@mydomain.com";
String subject = "hello";
String body = "test message";
System.out.println("send mail " + smtpServer );
System.out.println("from: " + from);
System.out.println("to: " + to);
send(smtpServer, to, from, subject, body);
System.out.println("--- complete ---");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void send(String smtpServer, String to, String from, String subject, String body) {
try {
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
msg.setSubject(subject);
msg.setText(body);
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
Transport transport = session.getTransport("smtps");
transport.send(msg);
System.out.println("Message send OK.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
No comments:
Post a Comment