วันนี้เราจะมาดูตัวอย่างการใช้ JavaMail เพื่อส่งอีเมลโดยใช้ JavaMail API สามารถใช้เพื่อส่งอีเมลผ่านเซิร์ฟเวอร์ SMTP ได้ JavaMail API ยังรองรับการตรวจสอบสิทธิ์ทั้ง TLS และ SSL สำหรับการส่งอีเมล
ขั้นตอนการเขียนโปรแกรม Java สำหรับส่งอีเมล

- สร้าง
javax.mail.Sessionobject - สร้าง
javax.mail.internet.MimeMessageobject, เพื่อใช้สำหรับการกำหนด ที่อยู่อีเมลผู้รับ, หัวข้ออีเมล, อีเมลตอบกลับ, เนื้อหาอีเมล, ไฟล์แนบ เป็นต้น - สร้าง
javax.mail.Transportในการส่งข้อความ
no authentication
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailUtil {
public static void sendEmail() {
Session session = null;
try {
Properties properties = System.getProperties();
properties.put("mail.smtp.host", "mail.example.com");
properties.put("mail.smtp.auth", "false");
properties.put("mail.smtp.port", "25"); //SMTP Port
session = Session.getInstance(properties);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("mailFrom@example.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("mailTo@example.com"));
message.setSubject("My Subject");
message.setText("My Body");
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
} finally {
}
}
}
TLS Authentication
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailUtil {
public static void sendEmail() {
Session session = null;
try {
Properties properties = System.getProperties();
properties.put("mail.smtp.host", "mail.example.com");
properties.put("mail.smtp.port", "587"); //TLS Port
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
Authenticator authen = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(apiKey, serectKey);
}
};
session = Session.getInstance(properties, authen);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("mailFrom@example.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("mailTo@example.com"));
message.setSubject("My Subject");
message.setText("My Body");
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
} finally {
}
}
}
SSL Authentication
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailUtil {
public static void sendEmail() {
Session session = null;
try {
Properties properties = System.getProperties();
properties.put("mail.smtp.host", "mail.example.com");
props.put("mail.smtp.socketFactory.port", "465"); //SSL Port
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory"); //SSL Factory Class
props.put("mail.smtp.auth", "true"); //Enabling SMTP Authentication
props.put("mail.smtp.port", "465"); //SMTP Port
Authenticator authen = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(apiKey, serectKey);
}
};
session = Session.getInstance(properties, authen);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("mailFrom@example.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("mailTo@example.com"));
message.setSubject("My Subject");
message.setText("My Body");
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
} finally {
}
}
}
ส่งอีเมลเรียบร้อย
บทสรุป
ในบทความนี้ เราได้เรียนรู้วิธีส่งอีเมลโดยใช้ JavaMail API