Java 邮件发送代码

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

发送邮件

1、MailOperation.java 主要文件,邮件操作,发送
package main;

import java.util.Date;

import java.util.Properties;

import javax.mail.Message;

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 MailOperation {

    /**

     * @author goming

    *TODO:发送邮件

    *@param user

    *@param password

    *@param host

    *@param from

    *@param to

    *@param subject

    *@param content

    *@return

    *@throws Exception

     */

    public String sendMail(String   user,String password,String host,String from,String to,String subject,String   content) throws Exception {

        // TODO Auto-generated method stub

       if(to!=null)

       {

           Properties props=System.getProperties();

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

           props.put("mail.smtp.auth", "true"); 

 

           MailAuthenticator auth=new   MailAuthenticator();

           MailAuthenticator.USERNAME=user;

           MailAuthenticator.PASSWORD=password;

           Session session=Session.getInstance(props,auth);

           session.setDebug(true);

           try{

              MimeMessage message=new   MimeMessage(session);

              //message.setDataHandler(new DataHandler(content, "text/html;   charset=utf-8"));//设置邮件内容

              message.setFrom(new   InternetAddress(from));

              if(!to.trim().equals(""))

              message.addRecipient(Message.RecipientType.TO, new   InternetAddress(to.trim()));

              message.setSubject(subject);

              //  message.setContent(mp)

              //message.setText("this is 一个测试邮件");

              MimeBodyPart mbp1=new   MimeBodyPart();  //正文

              mbp1.setContent(content,"text/html;charset=utf-8");

 

              Multipart mp=new MimeMultipart();   //整个邮件:正文+附件

              mp.addBodyPart(mbp1);

              //mp.addBodyPart(mbp2);

              message.setContent(mp);

              message.setSentDate(new Date());

              message.saveChanges();

 

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

              trans.send(message);

              System.out.println(message.toString());

           }catch(Exception e)

           {

              e.printStackTrace();

              return "failure";

           }

           return "success";

       }

       else

           return "failure";

 

    }

 

    public static void main(String[] args) {

       MailOperation operation = new MailOperation();

       String user = "admin@xiamengoldenway.com";

       String password = "xmG0ldenway";

       String host = "smtp.qq.com";

       String from = "admin@qq.com";

       String to = "******@qq.com";//收件人

       String subject = "Test";

       String content = "测试邮件";

      

       try {

           String res = operation.sendMail(user,   password, host, from, to, subject, content);

           System.out.println(res);

       } catch (Exception e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

       System.out.println(new Date());

    }

 

}


 

2、MailAuthenticator.jave 封装发件人账号密码
import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

 

/**

 *

 *   @author goming

 *

 */

public class MailAuthenticator extends   Authenticator

 

{

         public   static String USERNAME = "";

 

         public   static String PASSWORD = "";

 

         public   MailAuthenticator(){}

         protected   PasswordAuthentication getPasswordAuthentication(){

                   return   new PasswordAuthentication(USERNAME, PASSWORD);

         }

}


 

3、SendEmail.java 调用类,主要做邮件内容,以及发送一个地址回访!
/**

     * 发送邮件

     * @author goming

     * @param to

     * @param uuid

     */

    @RequestMapping(params = "sendMail")

    @ResponseBody

    private void sendMail(String   to,HttpServletRequest request){

              MailOperation operation = new MailOperation();

       //发件人

       String user = "*****@qq.com";

       String password = "******";

       String host = "smtp.qq.com";

       String from = "1915960810@qq.com";

       String subject = "感谢您注册创意生态资源集成系统软件";

       //邮箱内容

       String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";

       StringBuffer sb = new StringBuffer();

       Date date=new Date();

       Long longtimeLong=date.getTime();

       sb.append("<!DOCTYPE>"+"<div bgcolor='#f1fcfa'   style='border:1px solid #d9f4ee; font-size:12px; line-height:22px; color:#005aa0;padding-left:1px;padding-top:5px;   padding-bottom:5px;'><span style='font-weight:bold;'>安全提示:</span>"

                      + "<div style='width:950px;font-family:arial;'>为了您的账户安全,我们建议您点击以下链接验证邮箱:<br/><a href='"+basePath+"UserRegister.do?ValidateEmail&time="+longtimeLong+"&token="+to+"'>"+basePath+"UserRegister.do?ValidateEmail&time="+longtimeLong+"&token="+to+"</a><br/>请在24小时内点击该链接,您也可以将链接复制到浏览器地址栏访问。<br/>本邮件由系统自动发出,请勿回复。<br/>感谢您的使用。<br/>厦门交叉媒体科技有限公司</div>"

                     +"</div>");

       try {

           String res = operation.sendMail(user,   password, host, from, to, subject, sb.toString());

//         System.out.println(res);

       } catch (Exception e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

    }