1.1.1. 通过JNDI找到JavaMail
1.1.1.1. 使用远程访问获得JavaMail
Hashtable env=new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.apusic.naming.jndi.CNContextFactory");
env.put(Context.PROVIDER_URL,"iiop://localhost:6888");
//插入相关验证信息
env.put(Context.SECURITY_CREDENTIALS,"admin" ) ;
env.put(Context.SECURITY_PRINCIPAL,"admin");
Context initCtx=new InitialContext(env);
System.out.println(initCtx.PROVIDER_URL);
//通过RMI 取得
Session myMailSession = (Session) initCtx.lookup("javamail/myMailNoAuth");
1.1.1.2. 使用Apusic默认方式获得JavaMail
Context initCtx = new InitialContext();
Session myMailSession = (Session) initCtx.lookup("javamail/myMailNoAuth");
System.out.println(myMailSession.getProperty("mail.smtp.host"));
1.1.2. 通过资源注入配置JavaMail
@Resource(mappedName = "javamail/myMailAuth", type = javax.mail.Session.class, shareable = true, authenticationType = Resource.AuthenticationType.CONTAINER, description = "my email with auth")
private Session myAuthMailSession;
或者
@Resource(mappedName = "javamail/myMailAuth")
private Session myAuthMailSession;
1.2. 在代码中初始化
1.2.1. 无须认证的初始化
final Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.host", "localhost");
Session myMailSession = Session.getInstance(props);
1.2.2. 需要认证的初始化
final Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.163.com");
Session myMailSession = Session.getInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "password");} });
1.4. 对Session调试的配置
● 可以在mail-session中加入<property name="mail.debug" value="true" />
● 可以在Session初始化前加入props.put("mail.debug", "true");
● 可以在Session初始化后,通过代码控制myMailSession.setDebug(true);
1.5. Properties的解释
◆ mail.store.protocol:用于检索邮件的协议
◆ mail.transport.protocol:用于传送邮件的协议
◆ mail.host:默认的主机名,默认是本地计算机。
◆ mail.user:默认的用户名。
◆ mail.from:默认的返回地址。
◆ mail.debug:是否输出DEBUG信息。默认为False。
◆ mail.protocol.host:指定协议的主机名,没有指定就用mail.host。例如:mail.smtp.host=smtp.163.com
◆ mail.protocol.user:指定协议的用户名,没有指定就用mail.user。例如:mail.smtp.user=user
◆ mail.protocol.from:指定协议的返回地址,没有指定就用mail.from。例如:mail.smtp.from=user@163.com
◆ mail.smtp.auth:如果是True,就会登录SMTP服务器,获得授权才能发送邮件。默认为False。
2. 通过JavaMail发送邮件
2.1. 通过无须认证的SMTP发邮件
final Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.host", "localhost");
try {
Session myMailSession = Session.getInstance(props);
myMailSession.setDebug(true); // 打开DEBUG模式
Message msg = new MimeMessage(myMailSession);
msg.setFrom(new InternetAddress("user@163.com"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("zhuyuanxiang@apusic.com"));
msg.setContent("I have a email test!", "text/plain");
msg.setSentDate(new java.util.Date());
msg.setSubject("Test");
msg.setText("This is a test!\n");
System.out.println("1.Please wait for sending one...");
// 发送邮件
// javax.mail.Transport.send(msg); // 与下面四行的功能一样
javax.mail.Transport myTransport = myMailSession.getTransport("smtp");
myTransport.connect();
myTransport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
myTransport.close();
System.out.println("2.Your message had send!");
} catch (Exception error) {
System.out.println("*.I am sorry to tell you the fail for " + error);
}
注:测试可以使用Microsft IIS SMTP 服务,安装好启动服务后,还需要进入“IIS管理器”,增加一个“远程域”,对于“远程域”的“出站安全性”需要把用户名和密码加上(就是平时发邮件时登录用的用户名/密码),否则邮件发不出去。
2.2. 通过需要认证的SMTP发邮件
2.2.1. 默认方式初始化Session
final Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
try {
Session myMailSession = Session.getInstance(props);
myMailSession.setDebug(true); // 打开DEBUG模式
Message msg = new MimeMessage(myMailSession);
msg.setFrom(new InternetAddress("user@163.com"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("zhuyuanxiang@apusic.com"));
msg.setContent("I have a email test!", "text/plain");
msg.setSentDate(new java.util.Date());
msg.setSubject("Test");
msg.setText("This is a test!\n");
System.out.println("1.Please wait for sending two...");
// 发送邮件
javax.mail.Transport myTransport = myMailSession.getTransport("smtp");
myTransport.connect("smtp.163.com", "user", "password");
myTransport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
myTransport.close();
// javax.mail.Transport.send(msg); // 这行不能使用。
System.out.println("2.Your message had send!");
} catch (Exception error) {
System.out.println("*.I am sorry to tell you the fail for " + error);
}
}
2.2.2. 嵌入认证类初始化Session
final Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.163.com");
try {
Session myMailSession = Session.getInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "password");
}
});
myMailSession.setDebug(true); // 打开DEBUG模式
InternetAddress address = new InternetAddress("user@163.com");
String message = "I have a email test!";
Message msg = new MimeMessage(myMailSession);
msg.setFrom(address);
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("zhuyuanxiang@apusic.com"));
msg.setContent(message, "text/plain");
msg.setSentDate(new java.util.Date());
msg.setSubject("Test");
msg.setText("This is a test!\n");
out.println("1.Please wait for sending...");
// 发送邮件
javax.mail.Transport myTransport = myMailSession.getTransport("smtp");
myTransport.connect();
myTransport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
myTransport.close();
// javax.mail.Transport.send(msg); // 注释上四行,打开这行代码,功能一样
out.println("2.Your message had send!");
} catch (Exception error) {
out.println("*.I am sorry to tell you the fail for " + error);
}
注:资源注入的Session发送邮件时:
无须认证的SMTP,可以参考2.1.
需要认证的SMTP,可以参考2.2.1.