JavaMail: Achieving Mailing
The JavaMail API is a powerful tool for sending and receiving emails in Java applications. In this section, we will explore the basics of JavaMail and provide a step-by-step guide on how to use it to send emails.
Common Mail Servers and Corresponding Ports
Before we dive into the code, let’s take a look at some common mail servers and their corresponding ports:
- Sina.com: POP3 server address:
pop3.sina.com.cn(Port: 110), SMTP server address:smtp.sina.com.cn(Port: 25) - Sohu.com: POP3 server address:
pop3.sohu.com(Port: 110), SMTP server address:smtp.sohu.com(Port: 25) - 126 E-mail: POP3 server address:
pop.126.com(Port: 110), SMTP server address:smtp.126.com(Port: 25) - 139 E-mail: POP3 server address:
POP.139.com(Port: 110), SMTP server address:SMTP.139.com(Port: 25) - 163.com: POP3 server address:
pop.163.com(Port: 110), SMTP server address:smtp.163.com(Port: 25) - QQ-mail: POP3 server address:
pop.qq.com(Port: 110), SMTP server address:smtp.qq.com(Port: 25) - QQ E-mail: POP3 server address:
pop.exmail.qq.com(SSL-enabled port: 995), SMTP server address:smtp.exmail.qq.com(SSL-enabled port: 587/465) - Yahoo.com: POP3 server address:
pop.mail.yahoo.com, SMTP server address:smtp.mail.yahoo.com - Yahoo.com.cn: POP3 server address:
pop.mail.yahoo.com.cn(Port: 995), SMTP server address:smtp.mail.yahoo.com.cn(Port: 587) - HotMail: POP3 server address:
pop3.live.com(Port: 995), SMTP server address:smtp.live.com(Port: 587) - Gmail (google.com): POP3 server address:
pop.gmail.com(SSL-enabled port: 995), SMTP server address:smtp.gmail.com(SSL-enabled port: 587) - 263.net: POP3 server address:
pop3.263.net(Port: 110), SMTP server address:smtp.263.net(Port: 25) - 263.net.cn: POP3 server address:
pop.263.net.cn(Port: 110), SMTP server address:smtp.263.net.cn(Port: 25) - x263.net: POP3 server address:
pop.x263.net(Port: 110), SMTP server address:smtp.x263.net(Port: 25) - 21cn.com: POP3 server address:
pop.21cn.com(Port: 110), SMTP server address:smtp.21cn.com(Port: 25) - Foxmail: POP3 server address:
POP.foxmail.com(Port: 110), SMTP server address:SMTP.foxmail.com(Port: 25) - China.com: POP3 server address:
pop.china.com(Port: 110), SMTP server address:smtp.china.com(Port: 25) - Tom.com: POP3 server address:
pop.tom.com(Port: 110), SMTP server address:smtp.tom.com(Port: 25) - Etang.com: POP3 server address:
pop.etang.com, SMTP server address:smtp.etang.com
Mail Transmitting Process
The mail transmitting process involves the following steps:
- Creating a Session object: This object creates a Transport Object/Store object used to send/save the message.
- Transport Object/Store objects connected to the mail server: The Transport Object/Store object creates a Message object (that is, the message content).
- Transport objects send messages; Store objects get mail mailbox.
Simple Code Example
Here is a simple code example that demonstrates how to use JavaMail to send an email:
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
// Create a session object
Session session = Session.getInstance(props, null);
try {
// Create a message object
MimeMessage msg = new MimeMessage(session);
// Set the sender information
msg.setFrom(new InternetAddress("me@example.com"));
// Set the recipient information
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("you@example.com"));
// Set the message subject
msg.setSubject("JavaMail hello world example");
// Set the message content
msg.setText("! Hello, world \n");
// Send the message
Transport.send(msg, "me@example.com", "my-password");
} catch (MessagingException mex) {
System.out.println("send failed, exception:" + mex);
}
Optimized Code Example
To make the code more maintainable, we can create a MailBean class that holds the email configuration:
public class MailBean {
private String fromAddress;
private String toAddress;
private String userName;
private String userPassword;
private List<String> recipients;
private String subject;
private MailContent content;
// Getters and setters
}
We can then use the MailBean class to send the email:
MailBean mailBean = new MailBean();
mailBean.setFromAddress("me@example.com");
mailBean.setToAddress("you@example.com");
mailBean.setSubject("JavaMail hello world example");
mailBean.setContent(new MailContent("Hello, world!", new Date(), "! Hello, world \n"));
Transport.send(mailBean, "me@example.com", "my-password");
Mail Configuration Constants
To make the code more configurable, we can define mail configuration constants:
public class MailConstant {
public static final String MAIL_SMTP_HOST = "mail.smtp.host";
public static final String MAIL_SMTP_PORT = "mail.smtp.port";
public static final String MAIL_SMTP_STARTTLS_ENABLE = "mail.smtp.starttls.enable";
public static final String MAIL_SMTP_AUTH = "mail.smtp.auth";
public static final String MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol";
public static final String MAIL_FROM = "mail.from";
public static final String MAIL_SMTP_SSL_TRUST = "mail.smtp.ssl.trust";
public static final String MAIL_USERNAME = "mail.username";
public static final String MAIL_PASSWORD = "mail.password";
}
We can then use the mail configuration constants to configure the email:
props.put(MailConstant.MAIL_SMTP_HOST, "my-mail-server");
props.put(MailConstant.MAIL_SMTP_PORT, "25");
props.put(MailConstant.MAIL_SMTP_STARTTLS_ENABLE, "true");
props.put(MailConstant.MAIL_SMTP_AUTH, "true");
props.put(MailConstant.MAIL_TRANSPORT_PROTOCOL, "SMTP");
props.put(MailConstant.MAIL_FROM, "me@example.com");
props.put(MailConstant.MAIL_SMTP_SSL_TRUST, "my-trust-store");
props.put(MailConstant.MAIL_USERNAME, "my-username");
props.put(MailConstant.MAIL_PASSWORD, "my-password");
By following these steps and using the MailBean class and mail configuration constants, we can make the code more maintainable and configurable.