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

java 发送邮件的实例代码(可移植)

程序员文章站 2023-11-21 11:48:10
以一个web项目为例,代码是可以移植的 首先要导入mail.jar包,然后创建自己的类 1:htmlsender类 复制代码 代码如下:package com.txq...

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

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

1:htmlsender类

复制代码 代码如下:

package com.txq.mail;

 import java.util.properties;

 import javax.mail.message;
 import javax.mail.messagingexception;
 import javax.mail.nosuchproviderexception;
 import javax.mail.session;
 import javax.mail.transport;
 import javax.mail.internet.addressexception;
 import javax.mail.internet.internetaddress;
 import javax.mail.internet.mimemessage;
 import javax.mail.internet.mimemultipart;
 import javax.mail.internet.mimeutility;

 /**
  *
  * @author administrator
  *
  */
 public class htmlsender {
     private mimemessage message;
     private properties props;
     private session session;
     private string name = "";
     private string password = "";

     public htmlsender(string host,string name,string password)
     {
         this.name = name;
         this.password = password;
         props = system.getproperties();

         props.put("mail.smtp.host", host);
         props.put("mail.smtp.auth","true");
         myauthenticator auth = new myauthenticator(name,password);
         session = session.getdefaultinstance(props,auth);

         message = new mimemessage(session);
         new mimemultipart();
     }

     public void setfrom(string from)
     {
         try
         {
             message.setfrom(new internetaddress(from));
         }
         catch (addressexception e)
         {
             e.printstacktrace();
         }
         catch (messagingexception e)
         {
             e.printstacktrace();
         }
     }

     public void setto(string to)
     {
         try
         {
             message.setrecipients(message.recipienttype.to,internetaddress.parse(to));
         }
         catch(addressexception e)
         {
             e.printstacktrace();
         }
         catch(messagingexception e)
         {
             e.printstacktrace();
         }
     }

     public void setsubject(string subject)
     {
         try
         {
             message.setsubject(subject);
         }
         catch(exception e)
         {
             e.printstacktrace();
         }
     }

     public void setcontect(string content)
     {
         try
         {
             message.setcontent(content,"text/html;charset=utf-8");
         }
         catch(exception e)
         {
             e.printstacktrace();
         }
     }

     public boolean send()
     {
         try
         {
             transport transport = session.gettransport("smtp");
             transport.connect((string)props.get("mail.smtp.host"),name,password);
             transport.sendmessage(message, message.getrecipients(message.recipienttype.to));
             transport.close();
             return true;
         }
         catch(nosuchproviderexception e)
         {
             e.printstacktrace();
             return false;
         }
         catch(messagingexception e)
         {
             e.printstacktrace();
             return false;
         }
     }
 }

2:myauthenticator类

复制代码 代码如下:

package com.txq.mail;

 import javax.mail.authenticator;
 import javax.mail.passwordauthentication;

 
 public class myauthenticator extends authenticator{
     string name;
     string password;
     public myauthenticator(string name,string pasword)
     {
         this.name = name;
         this.password = pasword;
         getpasswordauthentication();
     }
     protected passwordauthentication getpasswordauthentication()
     {
         return new passwordauthentication(name,password);
     }
 }

3:mailaction类

复制代码 代码如下:

package com.txq.action;

 import java.io.printwriter;

 import com.opensymphony.xwork2.actionsupport;
 import com.txq.mail.htmlsender;

 public class mailaction extends actionsupport {
     private string email;
     public string sendemail(){
         //代理主机-新浪
         string host = "smtp.sina.cn";
         //帐号-新浪帐号
         string name = "followmusic";
         //密码-新浪密码
         string password = "followmusic";
         //创建发送邮件对象
         htmlsender mail = new htmlsender(host,name,password);
         //发邮帐号
         mail.setfrom("followmusic@sina.cn");
         //收邮帐号
         mail.setto("youxiangzhanghe@qq.com");
         //邮件主题
         mail.setsubject("淘兴趣");
         //邮件内容
         mail.setcontect("你的好友邀请你来淘兴趣~");

         if(mail.send())
         {
             return success;
         }
         else
         {
             return "error";
         }
     }
     public string getemail() {
         return email;
     }
     public void setemail(string email) {
         this.email = email;
     }
 }