程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JavaMail完成郵件發送的辦法

JavaMail完成郵件發送的辦法

編輯:關於JAVA

JavaMail完成郵件發送的辦法。本站提示廣大學習愛好者:(JavaMail完成郵件發送的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是JavaMail完成郵件發送的辦法正文


本文實例講述了JavaMail完成郵件發送的辦法。分享給年夜家供年夜家參考。詳細以下:

下載 activation.jar 和 mail.jar
設置裝備擺設CLASSPATH,將下面兩個jar包參加到CLASSPATH中。

JavaBean : "SendEmail.java"

package cls; 
import java.util.Date; 
import java.util.Properties; 
import javax.mail.*; 
import javax.mail.internet.*; 
public class SendEmail 
{ 
 String host; 
 String username; 
 String password; 
 String fromAddr; 
 String toAddr; 
 String subject; 
 String content; 
 public SendEmail() 
 { 
 // 變量初始化 
 host = "smtp.qq.com"; 
 username = ""; 
 password = ""; 
 fromAddr = "@"; 
 } 
 // 發送郵件 
 public boolean sendMail() 
 { 
 Properties prop; 
 Session session; 
 MimeMessage msg; 
 try 
 { 
  prop = new Properties(); // 存儲銜接參數 
  prop.put("mail.smtp.host",host); 
  prop.put("mail.smtp.auth","true"); 
  session = Session.getDefaultInstance(prop,null);
  // 取得一個郵件的Session 
  msg = new MimeMessage(session); // 郵件信息 
  // 檢討郵件地址能否正當 
  if(fromAddr == null || fromAddr.equals("")) 
  { 
  throw new Exception("發件地址毛病"); 
  } 
  if(toAddr == null || toAddr.equals("")) 
  { 
  throw new Exception("目的地址毛病"); 
  } 
  //設置源地址 
  msg.setFrom(new InternetAddress(fromAddr)); 
  //設置目標地址 
  msg.setRecipient(Message.RecipientType.TO,new InternetAddress(toAddr)); 
  //設置主題 
  msg.setSubject(subject); 
  Multipart mp = new MimeMultipart(); //郵件內容 
  MimeBodyPart mbpContent = new MimeBodyPart(); 
  mbpContent.setContent(content,"text/html"); // 郵件格局 
  mp.addBodyPart(mbpContent); 
  msg.setContent(mp); 
  msg.setSentDate(new Date()); 
  // 發送郵件 
  Transport transport = session.getTransport("smtp"); 
  transport.connect((String)prop.get("mail.smtp.host"),username,password); 
  transport.sendMessage(msg,msg.getRecipients(MimeMessage.RecipientType.TO)); 
  transport.close(); 
  return true; 
 } 
 catch(Exception e) 
 { 
  System.out.println(e); 
  return false; 
 } 
 } 
 // getter and setter 
 public String getPassword() 
 { 
 return password; 
 } 
 public void setPassword(String password) 
 { 
 this.password = password; 
 } 
 public String getHost() 
 { 
 return host; 
 } 
 public void setHost(String host) 
 { 
 this.host = host; 
 } 
 public String getUsername() 
 { 
 return username; 
 } 
 public void setUsername(String username) 
 { 
 this.username = username; 
 } 
 public String getFromAddr() 
 { 
 return this.fromAddr; 
 } 
 public void setFromAddr(String addr) 
 { 
 fromAddr = addr; 
 } 
 public String getToAddr() 
 { 
 return this.toAddr; 
 } 
 public void setToAddr(String addr) 
 { 
 toAddr = addr; 
 } 
 public String getSubject() 
 { 
 return subject; 
 } 
 public void setSubject(String sub) 
 { 
 subject = sub; 
 } 
 public String getContent() 
 { 
 return content; 
 } 
 public void setContent(String content) 
 { 
 this.content = content; 
 } 
}

sendmail.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
 <head> 
 <title>Mail</title> 
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 </head> 
 <body> 
 <center> 
  <jsp:useBean id="email" class="cls.SendEmail" scope="page" /> 
  <jsp:setProperty name="email" property="toAddr" value="host" />
  <jsp:setProperty name="email" property="fromAddr" value="host" />
  <jsp:setProperty name="email" property="username" value="username" />
  <jsp:setProperty name="email" property="password" value="password" />
  <jsp:setProperty name="email" property="subject" value="mail test" />
  <jsp:setProperty name="email" property="content" value="hello world"/>
  <%=email.sendMail() %>
 </center> 
 </body> 
</html>

願望本文所述對年夜家的java法式設計有所贊助。

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