使用Java Mail发送邮件小结

使用Java Mail发送邮件需要用到mail.jar和activation.jar(非必须),发送一个邮件的基本步骤是:

  1. 初始化一个Properties类,将邮件服务器相关属性以key,value的形式添加进去
  2. 根据Properties实例创建Session
  3. 根据Session创建Message,并且在Message中添加邮件的From,to,body, attachment等等
  4. 使用Transport.send(message)发送邮件

代码如下:

package com.jingshou.mail;

import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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 MailUtil {
	private static String host="smtp.gmail.com";
	private static String port = "465";
	private static String from = "[email protected]";
	private static final String USER = "gmailaccount";
	private static final String PASS = "gmailpassword";
	private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
		
	public static void main(String[] args) throws AddressException, MessagingException {
		String[] attachment = {"E:\\me.JPG"};
		sendMail("8131****@qq.com", "Test Subject", "Test Content from Java with attachment", attachment);
		System.out.println("Email Send successfully");

	}
	
	public static void sendMail(String to, String sub, String body, String[] attaches) throws AddressException, MessagingException{
		Properties props = System.getProperties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.put("mail.smtp.socketFactory.fallback", "false");
		props.put("mail.smtp.port", port);
		props.put("mail.smtp.auth", "true");
		
		// Create session by username and password
		Session session = Session.getDefaultInstance(props, new Authenticator(){
			@Override
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(USER, PASS);
				}
		});
		
		// Initialize Message
		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress(from));
		// Using message.setRecipients for To list
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
		message.setSubject(sub);
		message.setText(body);
		
		// Add attchment
		MimeBodyPart mbp = new MimeBodyPart();
		mbp.setText(body);
		Multipart  mpart = new MimeMultipart();
		mpart.addBodyPart(mbp);
		
		if (attaches != null) {
			for (String fileName : attaches){
				File file = new File(fileName);
				if (file.exists()) {
					mbp = new MimeBodyPart();
					DataSource source = new FileDataSource(file);
					mbp.setDataHandler(new DataHandler(source));
					mbp.setFileName(file.getName());
					mpart.addBodyPart(mbp);
					System.out.println("The attached file name is: " + file.getName());
				}
				
			}
		}
		message.setContent(mpart);
		
		// Sent by Transort.send()
		Transport.send(message);	
	}

}

运行程序发现发出邮件From始终是自己Gmail的帐号,而不是自己指定的地址

 本文出自"lijingshou"博客,转载请务必保留此出处http://lijingshou.iteye.com/blog/2017611

猜你喜欢

转载自lijingshou.iteye.com/blog/2017611