java邮件发送

1 需要的jar包

 activation.jar,mail.jar,mailapi.jar


2 SendMailObject.java

package com.mc.common.javasendmail;

/**
 * @author Zhangkun [email protected]
 * @version 1.0
 */

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

import java.util.Date;
import javax.activation.*;
import java.io.*;

public class SendMailObject {

	// ���巢���ˡ��ռ��ˡ������

	private String to = "";
	private String from = "";
	private String cc = "";
	private String bcc = "";
	private String bounceback = "";
	private String subject = "";
	private String TextContent = "";
	private String SMTPServer = "";
	private String filename = "";
	private String sasluser = "";
	private String saslpass = "";
	private boolean isHtmlMail = true;

	// ���ڱ��淢�͸������ļ���ļ���
	Vector file = new Vector();

	public SendMailObject(String SMTPServer) {
		this.SMTPServer = SMTPServer;

	}

	public SendMailObject(String SMTPServer, String SASLUser, String SASLPass) {
		// the mail server need to saslauth,initialize tthe sasl parameter
		this.SMTPServer = SMTPServer;
		this.sasluser = SASLUser;
		this.saslpass = SASLPass;
	}

	public void setMailInfo(String from, String to, String cc, String bcc,
			String bounceback, String subject, boolean isHtmlMail,
			String TextContent) {
		// ��ʼ�������ˡ��ռ��ˡ������
		this.from = from;
		this.to = to;
		this.cc = cc;
		this.bcc = bcc;
		this.bounceback = bounceback;
		this.subject = subject;
		this.isHtmlMail = isHtmlMail;
		this.TextContent = TextContent;
	}

	// �÷��������ռ�������
	public void attachfile(String fname) {
		file.addElement(fname);
	}

	// ��ʼ�����ż��ķ���
	public boolean startSendMail() {

		try {
			// ����properties����
			Properties props = System.getProperties();
			// �����ż�������
			props.put("mail.smtp.host", SMTPServer);
			// �����ż������� ����Ͷ��Դ��ַ ���ô������ŵ�ַ
			props.put("mail.smtp.from", bounceback);

			// set the smtp auth option of the mail server
			if ("".equalsIgnoreCase(sasluser.trim())) {
				props.put("mail.smtp.auth", "false");
				System.out
						.println("set smtp sasl authentication mail.smtp.auth = false");
			} else {
				props.put("mail.smtp.auth", "true");
				System.out
						.println("set smtp sasl authentication mail.smtp.auth = true");
			}

			// �õ�Ĭ�ϵĶԻ�����
			Session session = Session.getDefaultInstance(props, null);

			// ����һ����Ϣ������ʼ������Ϣ�ĸ���Ԫ��
			MimeMessage msg = new MimeMessage(session);

			// �����ż�ͷ�ķ�������
			msg.setSentDate(new Date());

			// ���͵�ַ
			msg.setFrom(new InternetAddress(from));

			// ���յ�ַ
			msg.setRecipients(Message.RecipientType.TO, to);

			// ���͵�ַ
			if (!"".equalsIgnoreCase(cc.trim())) { // if the cc is not null ,set
													// the cc field of the mail
				msg.setRecipients(Message.RecipientType.CC, cc);
			}

			// �����͵�ַ
			if (!"".equalsIgnoreCase(bcc.trim())) { // if the cc is not null
													// ,set the cc field of the
													// mail
				msg.setRecipients(Message.RecipientType.BCC, bcc);
			}

			// ����
			msg.setSubject(subject);

			// �����bodypart�����뵽�˴�������multipart��
			Multipart mp = new MimeMultipart();

			// add attachment , ����ö��������ı����
			Enumeration efile = file.elements();
			// ����������Ƿ��и��Ķ���
			while (efile.hasMoreElements()) {
				MimeBodyPart mbp = new MimeBodyPart();
				// ѡ���ÿһ��������
				filename = efile.nextElement().toString();
				// �õ����Դ
				FileDataSource fds = new FileDataSource(filename);
				// �õ��������?����bodypart
				mbp.setDataHandler(new DataHandler(fds));
				// �õ��ļ���ͬ������bodypart
				mbp.setFileName(fds.getName());
				mp.addBodyPart(mbp);
			}
			// ���߼����е�����Ԫ��
			file.removeAllElements();

			// add mail text body
			MimeBodyPart mbpc = new MimeBodyPart();
			if (isHtmlMail == true) {
				mbpc.setContent(
						"<meta http-equiv='Content-Type' content='text/html; charset=gbk' />"
								+ TextContent, "text/html;charset=GB2312");
			} else {
				mbpc.setText(TextContent);
			}

			mp.addBodyPart(mbpc);
			// multipart���뵽�ż�
			msg.setContent(mp);

			msg.saveChanges();

			System.out.println(new Date() + " sending a mail by Mail Server:"
					+ SMTPServer + " .....");

			// �����ż�
			Transport transport = session.getTransport("smtp");
			transport.connect((String) props.get("mail.smtp.host"), sasluser,
					saslpass);
			if ("".equalsIgnoreCase(sasluser.trim())) { //
				transport.send(msg);
			} else {
				transport.sendMessage(msg, InternetAddress.parse(to));
				if (!"".equalsIgnoreCase(cc.trim())) { // if the cc is not null
														// ,sent mail
					transport.sendMessage(msg, InternetAddress.parse(cc));
				}
				if (!"".equalsIgnoreCase(bcc.trim())) { // if the cc is not null
														// ,sent mail
					transport.sendMessage(msg, InternetAddress.parse(bcc));
				}
			}

			System.out.println("Send mail completed!");
		} catch (MessagingException mex) {
			mex.printStackTrace();
			Exception ex = null;
			if ((ex = mex.getNextException()) != null) {
				ex.printStackTrace();
			}
			return false;
		}
		return true;
	}

}


3 EmailTest.java 


package com.cpcnet.test;

import com.mc.common.javasendmail.SendMailObject;

public class EmailTest {
	

	public static void main(String[] args) {
		String email_from = "****@qq.com";
		String email_to = "***@**-***.net";
		String smtp_host = "1s2m9t4p2.9h0k2.0n1et";
		String email_cc = "";
		String email_reject_to = "";
		String email_subject = "test";
		String sbEmailBody = "test";
		
//		SendMailObject mail=new SendMailObject(sync.smtp_host);		
		SendMailObject mail=new SendMailObject(smtp_host);	
		
//		mail.setMailInfo(sync.email_from, sync.email_to, sync.email_cc,"",sync.email_reject_to,sync.email_subject,true,sbEmailBody.toString());
		mail.setMailInfo(email_from, email_to, email_cc,"",email_reject_to,email_subject,true,sbEmailBody.toString());

		mail.startSendMail();
		mail = null;

	}

}


你可能感兴趣的:(java邮件发送)