完整示例
视频锁定
{$ currentTime | date:'mm:ss' $}
{$ timeLeft | date:'mm:ss' $}
下面我们来看看完整的Java邮件示例,该示例由三个类组成:IMail邮件接口、IMailGmail邮件具体实现(Gmail邮箱)和MailerV1邮件主程序。
IMail邮件接口,该接口定义了邮件应该具备的功能(用户验证、发送邮件和接收邮件)。
import javax.mail.Message;
public interface IMail {
public void setAuthenticator(String username, String password);
public void sendMail(String from, String to, String subject, String content);
public Message[] receiveMail(String username, String password);
}
IMailGmail邮件实现类,IMail邮件接口的具体实现类,支持Gmail邮箱。
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class IMailGmail implements IMail {
private String authUsername;
private String authPassword;
public void setAuthenticator(String username, String password) {
this.authUsername = username;
this.authPassword = password;
}
public void sendMail(String from, String to, String subject, String content) {
// 关于Gmail属性设置参考 https://support.google.com/mail/answer/13287?hl=en
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(authUsername,
authPassword);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(subject);
message.setText(content);
System.out.println("正在发送邮件... <" + subject + ">");
Transport.send(message);
System.out.println("您的邮件已发送.");
} catch (MessagingException e) {
throw new RuntimeException("发送邮件异常: " + e.getMessage());
}
}
public Message[] receiveMail(String username, String password) {
// 关于Gmail属性设置参考 https://support.google.com/mail/answer/13287?hl=en
Properties properties = new Properties();
properties.put("mail.store.protocol", "pop3");
properties.put("mail.pop3.host", "pop.gmail.com");
properties.put("mail.pop3.port", "995");
properties.put("mail.pop3.starttls.enable", "true");
Session emailSession = Session.getDefaultInstance(properties);
// emailSession.setDebug(true);
Message[] messages = null;
try {
// 创建POP3 store对象并连接至pop服务器
Store store = emailSession.getStore("pop3s");
System.out.println("正在连接服务器...");
store.connect("pop.gmail.com", username, password);
// 创建文件夹并打开
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
// 从文件夹中打开邮件列表(数组)
System.out.println("读取邮件列表...");
messages = emailFolder.getMessages();
System.out.println("邮件条数: " + messages.length);
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
try {
writeEnvelope(message);
} catch (Exception e) {
System.out.println("读取邮件异常: " + e.getMessage());
}
}
// 关闭store和文件夹对象
emailFolder.close(false);
} catch (Exception e) {
System.out.println("读取邮件异常: " + e.getMessage());
}
return messages;
}
public static void writeEnvelope(Message m) throws Exception {
Address[] a;
// 显示标题
if (m.getSubject() != null)
System.out.print("标题: <" + m.getSubject() + ">");
// 显示发送人地址
if ((a = m.getFrom()) != null) {
System.out.print(" 来自: ");
for (int j = 0; j < a.length; j++)
System.out.print(a[j].toString() + ", ");
System.out.println();
}
}
}
Mailer主程序,实现完整邮件发送的功能。
/*
* 提供了最简单的邮件发送功能
*
* 测试gmail的帐户为:
* 用户名: fenby.javacourse@gmail.com
* 密码: Pwd123456
*/
public class MailerV1 {
public static void main(String[] args) {
// 创建一个邮件sender对象
IMail myMail = new IMailGmail();
// 设置用户验证信息
myMail.setAuthenticator("fenby.javacourse@gmail.com", "Pwd123456");
// 发送邮件内容
myMail.sendMail("fenby.javacourse@gmail.com", "ning.li@fenby.com", "Python好书推荐《Python核心编程》", "赶快到京东去购买吧,链接地址:http://item.jd.com/10062788.html");
}
}
在线练习
{$ activeFileHint $}