JAVAMAIL发邮件

最近工作太忙了,所以好长时间没来CSDN了,呵呵,昨天花了点时间研究了一下JAVA发邮件的功能用到了几个开源的API,然后有上网参考了一下高手们的文章,自己稍加整理了一下,贴出来供大家参考。
import java.io.IOException;

import java.util.Date;

import java.util.Properties;



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



	private static String to = "";//接受人的邮件地址如:[email protected]

	private static String from = "";//发送人的邮件地址如:[email protected]

	private static String host = "smtp.163.com";//接受邮件的服务器,如163的是:smtp.163.com

	//private static String filename = "";//邮件的附件,如upload.txt

	private static final String username = "";  //发件人的邮件帐户   

	private static final String password = "";   //发件人的邮件密码

	private static boolean debug = true;

	private static String msgText1 = "网页防篡改记录在附件中,请注意查收./n"; //正文内容

	private static String subject = "网页防篡改"; //标题

	

	public static void sendMail(String filename) {

	

		// create some properties and get the default Session

		Properties props = System.getProperties();

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

		props.put("mail.smtp.auth", "true");//是否需要验证发送人的邮箱信息 



        PopupAuthenticator popA = new PopupAuthenticator();// 邮件安全认证。   

       

	    popA.performCheck(username, password); // 填写用户名及密码  

	    

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

		session.setDebug(debug);

		

		try {

		    // create a message

		    MimeMessage msg = new MimeMessage(session);

		    msg.setFrom(new InternetAddress(from));

		    InternetAddress[] address = {new InternetAddress(to)};

		    msg.setRecipients(Message.RecipientType.TO, address);

		    msg.setSubject(subject);



		    // create and fill the first message part

		    MimeBodyPart mbp1 = new MimeBodyPart();

		    mbp1.setText(msgText1);



		    // create the second message part

		    MimeBodyPart mbp2 = new MimeBodyPart();



	            // attach the file to the message

		    mbp2.attachFile(filename);



		    /*

		     * Use the following approach instead of the above line if

		     * you want to control the MIME type of the attached file.

		     * Normally you should never need to do this.

		     *

	   	    FileDataSource fds = new FileDataSource(filename) {

			public String getContentType() {

			    return "application/octet-stream";

			}

		    };

		    mbp2.setDataHandler(new DataHandler(fds));

		    mbp2.setFileName(fds.getName());

		     */



		    // create the Multipart and add its parts to it

		    Multipart mp = new MimeMultipart();

		    mp.addBodyPart(mbp1);

		    mp.addBodyPart(mbp2);



		    // add the Multipart to the message

		    msg.setContent(mp);



		    // set the Date: header

		    msg.setSentDate(new Date());



		    /*

		     * If you want to control the Content-Transfer-Encoding

		     * of the attached file, do the following.  Normally you

		     * should never need to do this.

		     *

		    msg.saveChanges();

		    mbp2.setHeader("Content-Transfer-Encoding", "base64");

		     */



		    // send the message

		    Transport.send(msg);

		    

		} catch (MessagingException mex) {

		    mex.printStackTrace();

		    Exception ex = null;

		    if ((ex = mex.getNextException()) != null) {

			ex.printStackTrace();

		    }

		} catch (IOException ioex) {

		    ioex.printStackTrace();

		}

	} 

}
package com.cssweb.mail;



import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;



public class PopupAuthenticator extends Authenticator {   

    String username = null;   

    String password = null;   



    public PopupAuthenticator() {   

    }   



    public PasswordAuthentication performCheck(String user, String pass) {   

        username = user;   

        password = pass;   

        return getPasswordAuthentication();   

    }   



    protected PasswordAuthentication getPasswordAuthentication() {   

        return new PasswordAuthentication(username, password);   

    }   

       

    // 直接返回检查的结果 ;    

    public PasswordAuthentication getPasswordAuthentication(String username,String pass){   

        return new PasswordAuthentication(username, password);   

    }   

       

}
//调用方法
SendFile.sendMail("upload.txt");

你可能感兴趣的:(exception,properties,String,session,null,javamail)