使用javamail发送邮件例子

需要的包:mail1.4.3.jar, activation.jar
1 验证类
package util.mail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class MailAuthenticator extends Authenticator{
	private String user;
	private String password;
	public MailAuthenticator(){}
	public MailAuthenticator(String user, String password){
		this.user=user;
		this.password=password;
	}
	//setter and getter
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	protected PasswordAuthentication getPasswordAuthentication() {
		// TODO Auto-generated method stub
		return new PasswordAuthentication(user, password);
	}
}


2 发送邮件类
package util.mail;
import java.util.Date;
import java.util.Properties;
import java.util.Set;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailSender {
    private String smtp;
    private String from;
    private String user;
    private String password;
    
    //constructor
    public MailSender(){}
    public MailSender(String smtp, String from, String user, String password){
    	this.smtp=smtp;
    	this.from=from;
    	this.user=user;
    	this.password=password;
    }
    
    //setter and getter
    public String getSmtp() {
		return smtp;
	}
	public void setSmtp(String smtp) {
		this.smtp = smtp;
	}
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	//send method
	public void send(String to, String cc, String bcc,String subject, String content, Set<String> attachments){
		Properties props=System.getProperties();
		props.put("mail.smtp.host", smtp);
		props.put("mail.smtp.auth", "true");
		MailAuthenticator auth = new MailAuthenticator(user, password);
		Session session=Session.getInstance(props, auth);
		MimeMessage msg = new MimeMessage(session);
		try {
			msg.setFrom(new InternetAddress(from));
			InternetAddress[] recipients = InternetAddress.parse(to);
			msg.setRecipients(Message.RecipientType.TO, recipients);
			if(cc!=null && !cc.equals("")){
				InternetAddress[] ccs = InternetAddress.parse(cc);
				msg.setRecipients(Message.RecipientType.CC, ccs);
			}
			if(bcc!=null && !bcc.equals("")){
				InternetAddress[] bccs = InternetAddress.parse(bcc);
				msg.setRecipients(Message.RecipientType.BCC, bccs);
			}
			InternetAddress[] reply = {new InternetAddress(from)};
		    msg.setReplyTo(reply);
			msg.setSubject(subject);
			Multipart mPart = new MimeMultipart();
			MimeBodyPart mBodyContent = new MimeBodyPart();
			mBodyContent.setContent(content, "text/html;charset=UTF-8");
			mPart.addBodyPart(mBodyContent);
			//add attachments
			if(attachments!=null){
				for(String filename:attachments){
					MimeBodyPart mBodyPart = new MimeBodyPart(); 
	                // attach the file to the message
	                FileDataSource fds = new FileDataSource("upload\\" + filename);
	                mBodyPart.setDataHandler(new DataHandler(fds));
	                mBodyPart.setFileName(filename);
	                mPart.addBodyPart(mBodyPart);
				}
			}
			
			msg.setContent(mPart);
			msg.setSentDate(new Date());
			//send mail
			Transport trans=session.getTransport("smtp");
			trans.connect(smtp, user, password);
			trans.send(msg);
			trans.close();
			
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	 
    public static void main(String[] args){
    	MailSender sender=new MailSender();
    	sender.setSmtp("your smtp host");
    	sender.setFrom("from email");
    	sender.setUser("user");
    	sender.setPassword("password");
        //多个收件人用逗号分隔
    	sender.send("r1,r2", null, null, "test", "test", null);
    }
}

你可能感兴趣的:(javamail)