天天看點

java 發送郵件 html 模闆,javaMail2種方式實作,并讀取html格式郵件模闆

首先是郵件模闆的讀取工具類

package gamutsoft.mail.test;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

public class ReadHTML {

//public static void main(String[] args) {

// TODO Auto-generated method stub

public static String reMailString(){

//String info="";

StringBuffer buff=new StringBuffer();

InputStreamReader in=null;

BufferedReader br=null;

String path = System.getProperty("user.dir") + "/src/html/email2.html";

File file=new File(path);

try {

in=new InputStreamReader(new FileInputStream(file));

br=new BufferedReader(in);

String line=null;

while((line=br.readLine()) != null){

//System.out.println(line);

buff.append(line).append("\n");

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

if(in!=null){

try {

in.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if(br!=null){

try {

br.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return buff.toString();

}

}

郵件的html模闆:

email2.html(亂寫的不喜勿噴)

您好:

網易

歡迎光臨,呵呵呵呵呵呵呵呵額

十分感謝

您好:

網易

歡迎光臨,呵呵呵呵呵呵呵呵額

十分感謝

您好:

網易

歡迎光臨,呵呵呵呵呵呵呵呵額

十分感謝

您好:

網易

歡迎光臨,呵呵呵呵呵呵呵呵額

十分感謝

您好:

網易

郵件發送類:這裡的郵箱是為了自己的隐私我亂寫了下,如果測試的話還得填寫正确的

package gamutsoft.mail.test;

import java.io.UnsupportedEncodingException;

import java.util.Date;

import java.util.Properties;

import javax.mail.Address;

import javax.mail.Authenticator;

import javax.mail.BodyPart;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

public class MailTest1 {

public static void send() throws MessagingException, UnsupportedEncodingException {

String info=ReadHTML.reMailString();

//郵件伺服器

String host="smtp.163.com";

//發件人

String from="[email protected]";

//收件人

String to="[email protected]";

//抄送人

String toCC1="[email protected]";

String toCC2="[email protected]";

String username="[email protected]";

String password="51234";

//郵件會話屬性

//Properties p=System.getProperties();

Properties p=new Properties();

p.put("mail.smtp.host", host);

//sesion獲得Transprot方法

Session session=Session.getDefaultInstance(p,null);

session.setDebug(true);

/

//建立Message資訊

MimeMessage message=new MimeMessage(session);

//建立郵件發送者位址

Address fromAD = new InternetAddress(from,"李建勳");

//nternetAddress(from)

//設定郵件發送者

message.setFrom(fromAD);

//建立郵件的接收位址

Address toAD = new InternetAddress(to);

//建立抄送人數組

Address toCAD1=new InternetAddress(toCC1);

Address toCAD2=new InternetAddress(toCC2);

Address [] toCs={toCAD1,toCAD2};

//設定郵件的接收位址

message.setRecipient(Message.RecipientType.TO,toAD);

message.addRecipients(Message.RecipientType.CC,toCs );

//設定發送時間

message.setSentDate(new Date());

//設定主題

message.setSubject("Hello JavaMail44");

// MimeMultipart類是一個容器類,包含MimeBodyPart類型的對象

Multipart mainPart = new MimeMultipart();

//建立一個包含HTML内容的MimeBodyPart

BodyPart body=new MimeBodyPart();

//設定html内容

body.setContent(info,"text/html;charset=utf-8");

//将MimeMultipart設定為郵件内容

mainPart.addBodyPart(body);

message.setContent(mainPart);

///sesion獲得Transprot

Transport transport=session.getTransport("smtp");

transport.connect(host, username, password);

transport.sendMessage(message,message.getAllRecipients());

transport.close();

//

//Transport.send(message);

}

public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {

// TODO Auto-generated method stub

send();

}

}

接下來是MyAuthenticator類

package gamutsoft.mail.test;

import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

public class MyAuthenticator extends Authenticator{

String username=null;

String password=null;

public MyAuthenticator(){

}

public MyAuthenticator(String username,String password){

this.username=username;

this.password=password;

}

public PasswordAuthentication getPasswordAuthentication(){

return new PasswordAuthentication(username, password);

}

}

這樣的寫法,可以提供我們在外部寫模闆,并且根據不同的客戶發送不同的郵件,代碼寫的比較快,比較粗糙,還望讀者見諒