直接使用java来调用mail.jar的API应用实例
/** 修改历史
* 日期 作者 修改内容
* -----------------------------------------------------------------------------
* 2008-11-10 李小强 创建CLASS
*/
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
/**
* <p>Title:领头鸟科技全球信息咨询服务与解决方案提供商自主研发产品,直接使用java来调用mail.jar的API应用实例</p>
* <p>Copyright: Copyright (c) 2008</p>
* <p>Company: 领头鸟科技全球信息咨询服务与解决方案提供商</p>
* @author 李小强
* <p>author E-Mail: lsl-120@163.com
* <p>http://leaderbird.blogcn.com
* <p>@version 2.1</p>
*
*/
public class JavaMailTest {
public static void main (String args[]) throws Exception {
String host = "mail.163.com"; //发件人使用发邮件的电子信箱服务器
String from = "aaa@163.com"; //发邮件的出发地(发件人的信箱)
String to = "lsl-120@163.com"; //发邮件的目的地(收件人信箱), Get system properties
Properties props = System.getProperties(); // Setup mail server
props.put("mail.smtp.host", host); // Get session
props.put("mail.smtp.auth", "true"); //这样才能通过验证
MyAuthenticator myauth = new MyAuthenticator("myname", "mypwd");
Session session = Session.getDefaultInstance(props, myauth);
// session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set the subject
message.setSubject("测试程序!");
// Set the content
message.setText("这是用java写的发送电子邮件的测试程序!");
message.saveChanges();
Transport.send(message);
}
}
//校验发信人权限的方法
// package com.hyq.test;
//
// import javax.mail.PasswordAuthentication;
class MyAuthenticator extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
/** 修改历史
* 日期 作者 修改内容
* -----------------------------------------------------------------------------
* 2008-11-10 李小强 创建CLASS
*/
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
/**
* <p>Title:领头鸟科技全球信息咨询服务与解决方案提供商自主研发产品,直接使用java来调用mail.jar的API应用实例</p>
* <p>Copyright: Copyright (c) 2008</p>
* <p>Company: 领头鸟科技全球信息咨询服务与解决方案提供商</p>
* @author 李小强
* <p>author E-Mail: lsl-120@163.com
* <p>http://leaderbird.blogcn.com
* <p>@version 2.1</p>
*
*/
public class JavaMailTest {
public static void main (String args[]) throws Exception {
String host = "mail.163.com"; //发件人使用发邮件的电子信箱服务器
String from = "aaa@163.com"; //发邮件的出发地(发件人的信箱)
String to = "lsl-120@163.com"; //发邮件的目的地(收件人信箱), Get system properties
Properties props = System.getProperties(); // Setup mail server
props.put("mail.smtp.host", host); // Get session
props.put("mail.smtp.auth", "true"); //这样才能通过验证
MyAuthenticator myauth = new MyAuthenticator("myname", "mypwd");
Session session = Session.getDefaultInstance(props, myauth);
// session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set the subject
message.setSubject("测试程序!");
// Set the content
message.setText("这是用java写的发送电子邮件的测试程序!");
message.saveChanges();
Transport.send(message);
}
}
//校验发信人权限的方法
// package com.hyq.test;
//
// import javax.mail.PasswordAuthentication;
class MyAuthenticator extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}