首先是邮件模板的读取工具类
- 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 {
- /**
- * @param args
- */
- //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(乱写的不喜勿喷)
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- </head>
- <body>
- <h4>您好: </h4>
- <a href="http://www.163.com">网易</a>
- <br>
- 欢迎光临,呵呵呵呵呵呵呵呵额
- <br>
- 十分感谢
- <h4>您好:</h4>
- <a href="http://www.163.com">网易</a>
- <br>
- 欢迎光临,呵呵呵呵呵呵呵呵额
- <br>
- 十分感谢
- <h4>您好:</h4>
- <a href="http://www.163.com">网易</a>
- <br>
- 欢迎光临,呵呵呵呵呵呵呵呵额
- <br>
- 十分感谢
- <h4>您好:</h4>
- <a href="http://www.163.com">网易</a>
- <br>
- 欢迎光临,呵呵呵呵呵呵呵呵额
- <br>
- 十分感谢
- <h4>您好:</h4>
- <a href="http://www.163.com">网易</a>
- <br>
- </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="1111@163.com";
- //收件人
- String to="1111@qq.com";
- //抄送人
- String toCC1="111111@163.com";
- String toCC2="444444@126.com";
- String username="5555555555@163.com";
- String password="51234";
- //邮件会话属性
- //Properties p=System.getProperties();
- Properties p=new Properties();
- p.put("mail.smtp.host", host);
- /*
- p.put("mail.smtp.auth", "true");
- //创建一个密码验证器
- Authenticator auth = new MyAuthenticator(username, password);
- //获得Session
- Session session=Session.getDefaultInstance(p,auth);
- */
- //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");
- /*
- //设置消息正文,文本
- message.setText("Welcome To JavaMail");
- //设置HTML内容
- message.setContent("<a href='http://www.163.com'>百度</a>","text/html;charset=utf-8");
- */
- // 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{
- /*在使用Authenticator这个抽象类时,我们必须采用继承该抽象类的方式,并且该继承类必须具
- * 有返回PasswordAuthentication对象(用于存储认证时要用到的用户名、密码)getPasswordAuthentication()
- * 方法。并且要在Session中进行注册,使Session能够了解在认证时该使用哪个类。
- * */
- 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);
- }
- }
这样的写法,可以提供我们在外部写模板,并且根据不同的客户发送不同的邮件,代码写的比较快,比较粗糙,还望读者见谅
以上内容转自 http://blog.csdn.net/rookie_li/article/details/13627613