package com.mail;
import java.net.MalformedURLException;
import java.net.URL;
import javax.mail.MessagingException;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
/**
* author:pangxinhua date: 2010-10-29
*/
public class TestMail {
public static void main(String[] args) throws EmailException,
MessagingException, MalformedURLException {
multipartEmail();
}
public static void multipartEmail() throws EmailException, MalformedURLException {
/*设置附件start---------------*/
EmailAttachment attachment = new EmailAttachment();
//附件路径
attachment.setPath("C://Documents and Settings//pangxinhua//桌面//monitor.sh");
//设置为附件
attachment.setDisposition(EmailAttachment.ATTACHMENT);
//描述
//attachment.setDescription("Picture of John");
//设置文件名(默认原名)
attachment.setName("monitor.sh");
/*设置附件结束*/
// Create the email message
MultiPartEmail email = new MultiPartEmail();
//服务器
email.setHostName("smtpcom.263xmail.com");
//验证
email.setAuthentication("automail@into4g.com", "into123456");
//发件人
email.setFrom("pangxinhua@into4g.com", "pangxinhua");
//收件人
email.addTo("pangxinhua@into4g.com", "pangxinhua");
//编码
email.setCharset("UTF-8");
//主题
email.setSubject("The picture");
//正文
email.setMsg("Here is the picture you wanted");
//添加附件
email.attach(attachment);
//发送
email.send();
}
public static void htmlEmail() throws EmailException, MalformedURLException {
HtmlEmail m = new HtmlEmail();
m.setAuthentication("automail@into4g.com", "into123456");
m.setHostName("smtpcom.263xmail.com");
m.setFrom("pangxinhua@into4g.com", "pangxinhua");
m.addTo("pangxinhua@into4g.com", "pangxinhua");
// m.setMsg("java mail test");
// m.setSubject("subject");
// //设置主题的字符集为UTF-8
m.setCharset("UTF-8");
m.setSubject("测试邮件主题");
URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid = m.embed(url, "Apache logo");
// set the html message
m.setHtmlMsg("<html>The apache logo - <img src=/"cid:" + cid
+ "/"></html>");
// set the alternative message
m.setTextMsg("Your email client does not support HTML messages");
// send the email
m.send();
}
public static void simpleEmail() throws EmailException {
SimpleEmail m = new SimpleEmail();
m.setAuthentication("automail@into4g.com", "into123456");
m.setHostName("smtpcom.263xmail.com");
m.setFrom("pangxinhua@into4g.com", "pangxinhua");
m.addTo("pangxinhua@into4g.com", "pangxinhua");
m.setCharset("UTF-8");
m.setSubject("测试邮件主题");
m.setMsg("test mail");
// m.buildMimeMessage();
//设置内容的字符集为UTF-8,先buildMimeMessage才能设置内容文本
// m.getMimeMessage().setText("测试邮件内容","UTF-8");
// m.sendMimeMessage();
m.send();
System.out.println("Send email successful!");
}
}