javaMail 发送邮件成功例子[转贴]

一个通过JSP,JAVABEAN组合成功发送邮件的例子;需要的包有;mail.jar ,activation.jar;
java.sun.com中有下载的;
第一:通过163.com发送邮件时要验证用户,在这我们创建一个JAVABEAN 继承Authenticator类;
package zh;

/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MyAuth extends Authenticator
{
 private String userName = "";
 private String password = "";
 private PasswordAuthentication  pass = null;
 public MyAuth(String userName,String password)
 {
  pass = new PasswordAuthentication(userName,password);
 }
 
 public PasswordAuthentication getPasswordAuthentication()
 {
  return pass;
 }
 public static void main(String[] args) {
 }
}
通过用户名和用户密码来创建一个验证类的实例MyAuth;
 MyAuth my = MyAuth(String userName,String password);//userName 用户邮箱名;passowrd :用户邮箱登录密码;例:userName = chshanhaojuan ,password = *******;

第二:
创建一个JSP页面来发送邮件;

<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%>
<%@ page import="javax.naming.NamingException"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="javax.naming.Context"%>
<%@ page import="java.util.*"%>
<%@ page import="java.net.*"%>
<%@ page import="zh.MyAuth"%>

<html>
<head>
 <title>OA</title>
</head>
<body topmargin=0 leftmargin=0 bgcolor="#ffffff">
 <table border=0 cellpadding=0 cellspacing=0 width=100%>
  <tr height=24>
   <td width=100% colspan=2 align="left" valign="middle" style="font-size:12px;padding-left:10px" background="images/bgcolor.gif">
    <img src="images/handle.gif"> 公共信息->公共通讯簿
   </td>
  </tr>
  <tr height=500>
   <td width=60% align="left" valign="top">
    <%
     try{
      
      
Properties props = new Properties ();
/**在这mail.smtp.host 是邮件服务器的地址,比喻smtp.163.com 是163的接收邮件的服务器一般用smtp.163.com 就可以了但是,在我的AIX操作系统中不能解析这个地址,所以用220.181.12.16,
你也可以通过在dos底下用ping smtp.163.com 来得到一个IP地址
*/     
props.put("mail.smtp.host", "220.181.12.16");

/**
 mail.transport.protocol 是邮件传输协议中的接收协议;smtp既可;
*/      
props.put("mail.transport.protocol","smtp");
/**
  是否通过验证;一般为true 。false不能通过验证;
*/        
props.put("mail.smtp.auth","true");
//mail.user 为用户邮箱登陆名;        
props.put("mail.user", "chshanhaojuan");
//mail.password 邮箱登陆密码;        
//props.put("mail.password", "******");
      
Session sendMailSession;
//用户验证使用的实例;      
MyAuth auth = new MyAuth("chshanhaojuan","******");
//根据用户验证和当前属性来创建一个会话对象;     
sendMailSession = Session.getDefaultInstance(props,auth);
      
sendMailSession.setDebug(false);
      
Transport tran = sendMailSession.getTransport("smtp");
      
tran.connect();
      
out.println(tran.isConnected());//测试连接是否成功;
      
Message mess = new MimeMessage(sendMailSession);//通过会话
//发送邮件的邮件地址;      
mess.setFrom(new InternetAddress("chshanhaojuan@163.com"));
//接收邮件的邮件地址;      
mess.setRecipient(Message.RecipientType.TO,new InternetAddress("xkg2001@163.com"));
//接收邮件的邮件地址;       
//mess.addRecipient(Message.RecipientType.TO,new InternetAddress("chshanhaojuan@yahoo.com.cn"));
//发送的内容;      
mess.setText("现在开始测试发送文件是否成功");
//发送时间;      
mess.setSentDate(new Date());
//内容主题;      
mess.setSubject("现在开始测试发送文件是否成功");
      
mess.saveChanges();
      
Address[] ad = new Address[1];
//接收邮件的地址;      
ad[0] = new InternetAddress("xkg2001@163.com");
//发送邮件;     
tran.sendMessage(mess,ad);
      
out.println("发送成功");
      
/*
//接收邮件;      
Properties props = new Properties ();
//接收邮件邮件的服务器地址;mail.pop3.host ;220.181.12.113 同上面的一样;就不多说了;       
props.put("mail.pop3.host", "220.181.12.113");
      
props.put("mail.store.protocol","pop3");//接收邮件的协议mail.store.protocol = pop3
        
props.put("mail.password", "chshanhaojuan");
        
props.put("mail.user", "******");//用户密码;
        
props.put("mail.port", "110"); //接收邮件服务器开的端口;163.com的端口是110;
      
Session sendMailSession;
      
sendMailSession = Session.getInstance(props,null);
      
Provider pro = sendMailSession.getProvider("pop3");
//默认的不能创建Store store = sendMailSession.getStore();创建不出来;为NULL异常;
Store store = sendMailSession.getStore(pro);
//先建立连接才能接收邮件;      
store.connect("220.181.12.113",110,"chshanhaojuan","******");
//是否连接成功;      
out.println(store.isConnected());
//打开收件箱,得到一个目录;      
Folder folder = store.getFolder("INBOX");
//打开目录;只读方式打开;      
folder.open(Folder.READ_ONLY);
从收件箱中提出邮件;      
Message[] mess = folder.getMessages();
//底下应该看的懂了;      
out.println(mess.length);
      
out.println(mess[0].getSubject());
      
out.println(mess[0].getSentDate().toString());
      
*/        
}catch(NullPointerException e)
     
{
      
out.println(e.getMessage()+"异常");
     
}catch(Exception ex)
     
{
      
out.println(ex.getMessage()+"异常");
     
}
    
%>    
   </td>
  </tr>
 </table>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值