package org.lke.test.action;
import java.net.URL;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* @author lke
*
*/
public class MailOfAction extends ActionSupport {
/**
* 发送基本文本格式邮件
*
* @return
* @throws Exception
*/
public String simplemail() throws Exception {
SimpleEmail mail = new SimpleEmail();
mail.setHostName("smtp.163.com");
//授权
mail.setAuthentication("xxx@163.com", "密码");//第一个参数为自己的163邮箱,第二个是密码
//接收人
mail.addTo("157538651@qq.com", "陈鹏飞");
//可发送多人
// mail.addTo("收件人地址","SW");//可以随便添
//发件人
mail.setFrom("15211870275@163.com", "左岸右转");//这里可以随便填写。是给收件人看的。
//主题
mail.setSubject("测试邮件的发送");
//内容
mail.setMsg("成功了吧!!!终于搞定");
mail.setCharset("UTF-8");
mail.send();
return super.execute();
}
/**
* 发送文本格式,带附件邮件
*
* @return
* @throws Exception
*/
public String multiPartemail() throws Exception {
//定义附件(可以定义多个)
EmailAttachment attachment = new EmailAttachment();
//设置附件 要是你的d盘没有对应的文件,就组找一个图片。
attachment.setPath("D:\\1.jpg");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("我们的图片哦");
//定义附件(可以定义多个)
EmailAttachment attachment2 = new EmailAttachment();
//设置附件
attachment2.setPath("D:\\5.jpg");
attachment2.setDisposition(EmailAttachment.ATTACHMENT);
attachment2.setDescription("我们的图片哦2");
MultiPartEmail mail = new MultiPartEmail();
mail.setHostName("smtp.163.com");
//授权
mail.setAuthentication("xxxx@163.com", "密码");
//接收人
mail.addTo("xxx@qq.com", "收件人名字(可以随便填)"); //第一个参数一定要写准,是收件人的邮箱
//可发送多人
mail.addTo("xxxxx@qq.com", "收件人名字(可以随便填)");
//发件人
mail.setFrom("xxxx@163.com", "发件人名字");
//主题
mail.setSubject("Test message");
//内容
mail.setMsg("很漂亮的图片哦");
mail.setCharset("UTF-8");
mail.attach(attachment);
mail.attach(attachment2);
mail.send();
return super.execute();
}
/**
* 发送HTML格式带附件邮件
*
* @return
* @throws Exception
*/
public String htmlemail() throws Exception {
/************
参考:http://commons.apache.org/email/userguide.html
**************/
/**
HtmlEmail mail = new HtmlEmail();
mail.setHostName("smtp.163.com");
//授权
mail.setAuthentication("发件人邮箱地址", "密码");
//接收人
mail.addTo("收件人地址", "baozi");
//可发送多人
mail.addTo("收件人地址","SW");
//发件人
mail.setFrom("发件人地址", "xxx");
//主题
mail.setSubject("Test message");
// embed the image and get the content id
URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid = mail.embed(url, "Apache logo");
// set the html message
mail.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");
// set the alternative message
mail.setTextMsg("Your email client does not support HTML messages");
//内容
mail.setMsg("this is a simple test of commons_email");
mail.send();
*/
return super.execute();
}
}
- 1
- 2
前往页