程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> java 發送郵件的實例代碼(可移植)

java 發送郵件的實例代碼(可移植)

編輯:JAVA編程入門知識

以一個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("[email protected]");
         //收郵帳號
         mail.setTo("[email protected]");
         //郵件主題
         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;
     }
 }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved