JavaMail发送HTML内容DEMO

下载:Javamail包以及jaf包。

javamail:http://java.sun.com/products/javamail/downloads/index.html

JAF:http://java.sun.com/javase/technologies/desktop/javabeans/jaf/downloads/index.html

如果在运行过程中报错可参考:

http://wangking717.iteye.com/blog/764407

 

package com.test;   
  
import java.io.IOException;   
import java.util.Date;   
import java.util.Properties;   
  
import javax.activation.DataHandler;   
import javax.mail.Address;   
import javax.mail.Authenticator;   
import javax.mail.Message;   
import javax.mail.MessagingException;   
import javax.mail.PasswordAuthentication;   
import javax.mail.Session;   
import javax.mail.Transport;   
import javax.mail.internet.InternetAddress;   
import javax.mail.internet.MimeMessage;   
import javax.mail.util.ByteArrayDataSource;   
  
  
public class EmailTest {   
  
    String serverauth ="true";   
    private String mailhost=null;//SMTP邮件接收服务器   
    private String outgoingport=null;//SMTP邮件接收服务器端口号,默认为25号端口   
    private String senderadress=null;//邮件发送者的邮箱地址   
    private String senderpassword=null;//邮件发送者的邮箱密码   
      
    /**  
    * Default constructor  
    */  
    public EmailTest(String mailserver,String mailaddress,String password) {   
        mailhost=mailserver;   
        senderadress=mailaddress;   
        senderpassword=password;   
    }   
      
    public boolean sendMailWithAtt(String email_to,String email_subject, String email_body) throws IOException {   
        boolean result = true;   
        try {   
            //set properties   
            Properties props = new Properties();   
            props.setProperty("mail.transport.protocol", "smtp");   
            //发送邮件服务器   
            props.setProperty("mail.host", mailhost);   
            if (serverauth != null&& serverauth.equalsIgnoreCase("true")) {   
                props.put("mail.smtp.auth", serverauth);   
            }   
            if (outgoingport!= null&& !outgoingport.trim().equals("")) {   
                props.put("mail.smtp.port", outgoingport);   
                props.put("mail.smtp.socketFactory.port",outgoingport);   
                props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");   
                props.put("mail.smtp.socketFactory.fallback", "false");   
                props.setProperty("mail.smtp.quitwait", "false");   
            }   
            // Get Session using Properties Object   
            Session session = null;   
            if (serverauth != null&& serverauth.equalsIgnoreCase("true")) {   
                Authenticator authen = new EmailAuthenticator();   
                session = Session.getInstance(props, authen);   
            } else {   
                session = Session.getInstance(props);   
            }   
            // Define message   
            MimeMessage message = new MimeMessage(session);   
            message.setFrom(new InternetAddress(senderadress));   
            /*  
            * Multiple(多个接收者)  
            */  
            if ( email_to != null && !email_to.trim().equals( "" )) {   
                String tos[] = email_to.trim().split(",");   
                Address toAdd[] = new Address[tos.length];   
                for ( int i = 0; i < toAdd.length; i++ ) {   
                    toAdd[i] = new InternetAddress( tos[i] );   
                }   
                message.addRecipients(Message.RecipientType.TO, toAdd);          
            }              
            message.setSubject(email_subject);   
            message.setSentDate(new Date());   
            StringBuffer sb = new StringBuffer();   
            sb.append("<HTML>\n");   
            sb.append("<HEAD>\n");   
            sb.append("<TITLE>\n");   
            sb.append(email_subject + "\n");   
            sb.append("</TITLE>\n");   
            sb.append("</HEAD>\n");   
  
            sb.append("<BODY>\n");   
            sb.append("<H1>" + email_subject + "</H1>" + "\n");   
  
            sb.append(email_body+"\n");   
  
            sb.append("</BODY>\n");   
            sb.append("</HTML>\n");   
            message.setDataHandler(new DataHandler(   
                    new ByteArrayDataSource(sb.toString(), "text/html")));   
  
            Transport.send(message);   
        } catch (MessagingException mex) {   
            result = false;   
            mex.printStackTrace();   
        }   
        return result;   
    }   
  
    /**  
    * Inner class  
    */  
    private class EmailAuthenticator extends javax.mail.Authenticator {   
        public PasswordAuthentication getPasswordAuthentication() {   
            return new PasswordAuthentication(senderadress, senderpassword);   
        }   
    }   
      
    /**  
    * 发送邮件的测试方法  
     * @throws IOException   
    */  
    public static void main(String[] args) throws IOException {   
        EmailTest jm=new EmailTest("smtp.qq.com","[email protected]", "xxxxx");   
        boolean sendresult=jm.sendMailWithAtt("[email protected]", "正在测试邮件发送", "sss<img src="http://www.ceconlinebbs.com/images/CEC-Forum-logo.gif" mce_src="http://www.ceconlinebbs.com/images/CEC-Forum-logo.gif" alt=\"555555\"/><br/><a href="\" mce_href="\""http://www.baidu.com\">我的链接</a><br/><font color=\"red\"><b>哈哈</b></font>");   
        if(sendresult){   
            System.out.println("邮件发送成功");   
        }else{   
            System.out.println("邮件发送失败");   
        }   
    }   
}  

 

 

 

 

你可能感兴趣的:(html,.net,qq,Blog,sun)