欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用JavaMail发送邮件(含实例)

程序员文章站 2022-06-15 11:20:57
...
JavaMail是Sun发布的处理电子邮件的应用程序接口,它预置了一些最常用的邮件传送协议的实现方法,并且提供了很容易的方法去调用它们。JavaMail是Sun发布的应用程序接口,所以目前它还没有被JDK包含。因此你需要从Sun的官方网站上下载到JavaMail类文件包。除此之外还需要Sun的JAF(JavaBeans Activation Framework ),否则JavaMail将不能运行。
        核心JavaMail API 由七个类组成:Session 、Message 、Address 、Authenticator 、Transport 、Store 及 Folder ,它们都来自javax.mail 、即JavaMail API *包。可以用这些类完成大量常见的电子邮件任务,包括发送消息、检索消息、删除消息、认证、回复消息、转发消息、管理附件、处理基于HTML文件格式的消息以及搜索或过滤邮件列表。
       测试邮件发送的源代码:

import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Properties; 
import java.util.Date; 
import javax.mail.*; 
import javax.mail.internet.*; 
/* 
*  测试用JavaMail发送电子邮件 
*/ 
public class SendMail { 
private String from; 
private String to; 
private String smtpServer; 
private String subject; 
private String content; 
private String username; 
private String password; 

/* 
  * 构造函数 
  */ 
public void mail(String smtpServer,String from,String to,String username,String password,String subject,String content) { 
  this.smtpServer = smtpServer; 
  this.from = from; 
  this.to = to; 
  this.username = username; 
  this.password = password; 
  this.subject = subject; 
  this.content = content; 
} 

public String getFrom() { 
  return from; 
} 
public void setFrom(String from) { 
  this.from = from; 
} 
public String getTo() { 
  return to; 
} 
public void setTo(String to) { 
  this.to = to; 
} 
public String getSmtpServer() { 
  return smtpServer; 
} 
public void setSmtpServer(String smtpServer) { 
  this.smtpServer = smtpServer; 
} 
public String getSubject() { 
  return subject; 
} 
public void setSubject(String subject) { 
  this.subject = subject; 
} 
public String getContent() { 
  return content; 
} 
public void setContent(String content) { 
  this.content = content; 
} 
public String getUsername() { 
  return username; 
} 
public void setUsername(String username) { 
  this.username = username; 
} 
public String getPassword() { 
  return password; 
} 
public void setPassword(String password) { 
  this.password = password; 
} 

public void send() { 
  Properties props = System.getProperties(); 
  props.put("mail.smtp.host", smtpServer); 
  props.put("mail.smtp.auth", "true");   
  /* 
   * Session: 邮件会话 
   * 使用Session类提供的getDefaultInstance()静态工厂方法获得一个默认的Session对象 
   */ 
  Session session = Session.getDefaultInstance(props,new PasswordAuthenticator(username, password)); 
  //信息对象 
  Message msg = new MimeMessage(session); 
  try { 
   msg.setFrom(new InternetAddress(from)); 
   msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); 
   msg.setSubject(subject); 
   msg.setText(content); 
   //发送邮件的时间 
   msg.setSentDate(new Date()); 
   //邮件的发送是由Transport来完成的 
   Transport.send(msg); 
  } catch (AddressException e) { 
   e.printStackTrace(); 
  } catch (MessagingException e) { 
   e.printStackTrace(); 
  } 
} 

public static void main(String[] argv) { 
  SendMail sendMail = new SendMail(); 
  //设置SMTP服务器地址    
  sendMail.setSmtpServer("smtp.sina.com"); 
  //发送方的邮件地址 
  sendMail.setFrom("[email=[ft=,2,]zhangxuey@sina.com]zhangxueyi@sina.com[/email]"); 
  //接收方的邮件地址 
  sendMail.setTo("[email=[ft=,2,]623064832@qq.com]623064832@qq.com[/email]"); 
  //发送方的邮件名称 
  sendMail.setUsername("[email=[ft=,2,]zhangxueyi@sina.com]zhangxueyi@sina.com[/email]"); 
  //发送方的邮箱密码(根据情况自己设定) 
  sendMail.setPassword("******"); 
  //邮件的主题 
  sendMail.setSubject("测试"); 
  //邮件的内容 
  sendMail.setContent("测试用JavaMail发送电子邮件!"); 
  sendMail.send(); 
} 
} 
/* 
* 密码验证类 PasswordAuthenticator 
* @param username:邮件地址 
* @param password:密码 
*/ 
class PasswordAuthenticator extends Authenticator { 
private String username; 
private String password; 
public PasswordAuthenticator(String username, String password) { 
  this.username = username; 
  this.password = password; 
}//构造方法 
protected PasswordAuthentication getPasswordAuthentication() { 
  return new PasswordAuthentication(username, password); 
} 
}