java发送邮件代码笔记

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

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;

public class SendMailByJavaMail {

	public static void main(String[] args) throws Exception {
		setMail3();
	}
	
	public static void setMail1() throws Exception {
		Properties props = new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol", "smtp");
//		props.setProperty("mail.host", "smtp.163.com");
		Session session = Session.getInstance( props);
		session.setDebug(true);
//		session.setPasswordAuthentication(url, pw)
		Message message = new MimeMessage(session);
		message.setSubject("hello");
		message.setFrom(new InternetAddress("[email protected]"));
		message.setText("aaaaa");
//		message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
		Transport transport = session.getTransport();
		transport.connect("smtp.163.com", 25, "test", "test");
		transport.sendMessage(message, new Address[] {new InternetAddress("[email protected]")});
		transport.close();
	}

	public static void setMail2() throws Exception {
		Properties props = System.getProperties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.host", "192.168.1.135");
		Session session = Session.getInstance(props, new Authenticator(){
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("hzy", "hzy");
			}
		});
		session.setDebug(true);
		Message message = new MimeMessage(session);
		message.setSubject("hello");
//		message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
		message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("[email protected],[email protected]"));
		message.setFrom(new InternetAddress("[email protected]"));
//		message.setText("aaaaa");
		message.setContent("<span style='color:red'>hello 中国</span>","text/html;charset=gbk");
		Transport.send(message);
	}
	
	public static void setMail3() throws Exception {
//		SimpleEmail email = new SimpleEmail();//如果发送普通的邮件,使用这个类就可以了
		MultiPartEmail email = new MultiPartEmail();//如果要发送带附件的邮件,需使用这个类
//		HtmlEmail email = new HtmlEmail();//可以发送html类型的邮件
		
		email.setHostName("192.168.1.135");//指定要使用的邮件服务器
		email.setAuthentication("hzy", "hzy");//使用163的邮件服务器需提供在163已注册的用户名、密码
		email.setCharset("UTF-8");
		try {
			email.setFrom("[email protected]");//设置发件人
			email.addTo("[email protected]");//设置收件人
			email.setSubject("测试邮件");//设置主题
			email.setMsg("测试邮件测试邮件测试邮件");//设置邮件内容
			
			
			File file = new File("C:\\testEmail.txt");//要发送的附件
			
			EmailAttachment attachment = new EmailAttachment();
			attachment.setPath(file.getPath());
			attachment.setName(file.getName());
			attachment.setDescription("附件描述");
			attachment.setDisposition(EmailAttachment.ATTACHMENT);//附件的类型
			email.attach(attachment);
			
			email.send();
			System.out.println("发送成功");
		} catch (EmailException e) {
			e.printStackTrace();
		}

	}
}

你可能感兴趣的:(java,apache,html)