java 发送邮件

以一个web项目为例,代码是可以移植的

首先要导入mail.jar包,然后创建自己的类

1:HTMLSender类

  1 package com.txq.mail;
  2 
  3 import java.util.Properties;
  4 
  5 import javax.mail.Message;
  6 import javax.mail.MessagingException;
  7 import javax.mail.NoSuchProviderException;
  8 import javax.mail.Session;
  9 import javax.mail.Transport;
 10 import javax.mail.internet.AddressException;
 11 import javax.mail.internet.InternetAddress;
 12 import javax.mail.internet.MimeMessage;
 13 import javax.mail.internet.MimeMultipart;
 14 import javax.mail.internet.MimeUtility;
 15 
 16 /**
 17  * 
 18  * @author Administrator
 19  *
 20  */
 21 public class HTMLSender {
 22     private MimeMessage message;
 23     private Properties props;
 24     private Session session;
 25     private String name = "";
 26     private String password = "";
 27     
 28     public HTMLSender(String host,String name,String password)
 29     {
 30         this.name = name;
 31         this.password = password;
 32         props = System.getProperties();
 33         
 34         props.put("mail.smtp.host", host);
 35         props.put("mail.smtp.auth","true");
 36         MyAuthenticator auth = new MyAuthenticator(name,password);
 37         session = Session.getDefaultInstance(props,auth);
 38         
 39         message = new MimeMessage(session);
 40         new MimeMultipart();
 41     }
 42     
 43     public void setFrom(String from)
 44     {
 45         try 
 46         {
 47             message.setFrom(new InternetAddress(from));
 48         } 
 49         catch (AddressException e)
 50         {
 51             e.printStackTrace();
 52         } 
 53         catch (MessagingException e)
 54         {
 55             e.printStackTrace();
 56         }
 57     }
 58     
 59     public void setTo(String to)
 60     {
 61         try
 62         {
 63             message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
 64         }
 65         catch(AddressException e)
 66         {
 67             e.printStackTrace();
 68         }
 69         catch(MessagingException e)
 70         {
 71             e.printStackTrace();
 72         }
 73     }
 74     
 75     public void setSubject(String subject)
 76     {
 77         try
 78         {
 79             message.setSubject(subject);
 80         }
 81         catch(Exception e)
 82         {
 83             e.printStackTrace();
 84         }
 85     }
 86     
 87     public void setContect(String content)
 88     {
 89         try
 90         {
 91             message.setContent(content,"text/html;charset=utf-8");
 92         }
 93         catch(Exception e)
 94         {
 95             e.printStackTrace();
 96         }
 97     }
 98     
 99     public boolean send()
100     {
101         try
102         {
103             Transport transport = session.getTransport("smtp");
104             transport.connect((String)props.get("mail.smtp.host"),name,password);
105             transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
106             transport.close();
107             return true;
108         }
109         catch(NoSuchProviderException e)
110         {
111             e.printStackTrace();
112             return false;
113         }
114         catch(MessagingException e)
115         {
116             e.printStackTrace();
117             return false;
118         }
119     }
120 }

2:MyAuthenticator类

 1 package com.txq.mail;
 2 
 3 import javax.mail.Authenticator;
 4 import javax.mail.PasswordAuthentication;
 5 
 6 
 7 public class MyAuthenticator extends Authenticator{
 8     String name;
 9     String password;
10     public MyAuthenticator(String name,String pasword)
11     {
12         this.name = name;
13         this.password = pasword;
14         getPasswordAuthentication();
15     }
16     protected PasswordAuthentication getPasswordAuthentication()
17     {
18         return new PasswordAuthentication(name,password);
19     }
20 }

3:MailAction类,前两个类只要完整的移至到你的项目中就可以(要把第一行对应的包名改一下),下边这个类,只要把带注释的内容修改为自己需要的就可以了;

 1 package com.txq.action;
 2 
 3 import java.io.PrintWriter;
 4 
 5 import com.opensymphony.xwork2.ActionSupport;
 6 import com.txq.mail.HTMLSender;
 7 
 8 public class MailAction extends ActionSupport {
 9     private String email;
10     public String sendEmail(){
11         //代理主机-新浪
12         String host = "smtp.sina.cn";
13         //帐号-新浪帐号
14         String name = "qqsinabaidu";
15         //密码-新浪密码
16         String password = "baidu360";
17         //创建发送邮件对象
18         HTMLSender mail = new HTMLSender(host,name,password);
19         //发邮帐号
20         mail.setFrom("qqsinabaidu@sina.cn");
21         //收邮帐号
22         mail.setTo("youxiangzhanghe@qq.com");
23         //邮件主题
24         mail.setSubject("哈哈");
25         //邮件内容
26         mail.setContect("发送邮件的程序~");
27         
28         if(mail.send())
29         {
30             return SUCCESS;
31         }
32         else
33         {
34             return "error";
35         }
36     }
37     public String getEmail() {
38         return email;
39     }
40     public void setEmail(String email) {
41         this.email = email;
42     }
43 }

 

posted @ 2013-03-11 14:06  ZH奶酪  阅读(476)  评论(0编辑  收藏  举报