您的位置:首页 > 运维架构 > Apache

Java发邮件:Java Mail与Apache Mail

2014-05-25 16:24 267 查看


一、邮件简介

一封邮件由很多信息构成,主要的信息如下,其他的暂时不考虑,例如抄送等:

1、收件人:收件人的邮箱地址,例如xxx@xx.com

2、收件人姓名:大部分的邮件显示时都会显示,例如loadfate 779554589@qq.com

3、发件人:发件人的邮箱地址

4、发件人姓名:

5、主题:邮件的标题

6、内容及附件:邮件的主要内容




二、使用Java发邮件的通用步骤

一般的项目中没有单独的邮件服务器,一般情况下都是使用别人的服务器。

1设置smtp服务器:不同的邮件服务器有不同的地址,例如:smtp.qq.com表示腾讯的smtp服务器。

2授权:使用该服务器的帐号和密码登录该服务器。

3创建邮件:创建一份包含所有信息的邮件,比如发件人、收件人、内容等。

4设置邮件的属性:为邮件的属性添加数据。

5发送邮件:因为封装不同,发送的方式不一致。


三、Java Mail与Apache Mail

Apache Mail是对Java Mail的封装,使用起来更加的简便,逻辑层次感更好。

使用Java Mail只需要导入一个jar包:mail.jar。

使用Apache Mail的时候需要导入两个jar包:mail.jar、commons-email-1.3.1.jar。


四、使用Java Mail发送邮件

public static void main(String[] args) throws Exception {
final String user = "779554589";
final String password = "";

String fromAddress = "779554589@qq.com";
String toAddress = "loadfate@163.com";
String subject = "邮件测试主题";
String content = "这是一个测试邮件<b>哈哈</b>";

//配置参数
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.qq.com");
// 方法一:使用transport对象发送邮件
{
//通过参数生成会话
Session session = Session.getInstance(props);
//启用调试模式
session.setDebug(true);
//创建一封邮件,并设置信息
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
message.setSubject(subject);
message.setText(content);
//创建传输
Transport transport = session.getTransport();
//连接smtp服务器
transport.connect(user, password);
//发送
transport.sendMessage(message, new InternetAddress[] { new InternetAddress(toAddress) });
transport.close();
}

// 方法二:使用Transport类静态方法发送邮件
{
//生成Session时以获取授权连接
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
session.setDebug(true);
//创建一封邮件,并设置信息
Message message = new MimeMessage(session);
message.setSubject(subject);
message.setFrom(new InternetAddress(fromAddress));
message.setRecipient(RecipientType.TO, new InternetAddress(toAddress));
message.setContent(content, "text/html;charset=utf-8");

//直接发送,message通过已经授权的Session生成
Transport.send(message);
}
}


五、使用Apache Mail发送邮件

public class ApacheMailTest {
// smtp服务器
private String hostName = "smtp.qq.com";
// 帐号与密码
private String userName = "779554589";
private String password = "这是个秘密";
// 发件人
private String fromAddress = "779554589@qq.com";
// 发件人姓名
private String fromName = "loadfate";

public static void main(String[] args) throws Exception {
// 收件人与收件人名字
String toAddress = "loadfate@163.com";
String toName = "loadfate";
ApacheMailTest test = new ApacheMailTest();
// 所有的异常都为处理,方便浏览

test.sendSimpleEmail(toAddress, toName);
test.sendHtmlEmail(toAddress, toName);
test.sendMultiPartEmail(toAddress, toName);
System.out.println("发送完成");
}

// 发送简单邮件,类似一条信息
public void sendSimpleEmail(String toAddress, String toName) throws Exception {
SimpleEmail email = new SimpleEmail();
email.setHostName(hostName);// 设置smtp服务器
email.setAuthentication(userName, password);// 设置授权信息
email.setCharset("utf-8");
email.setFrom(fromAddress, fromName, "utf-8");// 设置发件人信息
email.addTo(toAddress, toName, "utf-8");// 设置收件人信息
email.setSubject("测试主题");// 设置主题
email.setMsg("这是一个简单的测试!");// 设置邮件内容
email.send();// 发送邮件
}

// 发送Html内容的邮件
public void sendHtmlEmail(String toAddress, String toName) throws Exception {
HtmlEmail email = new HtmlEmail();
email.setHostName(hostName);
email.setAuthentication(userName, password);
email.setCharset("utf-8");
email.addTo(toAddress, toName, "utf-8");
email.setFrom(fromAddress, fromName, "utf-8");
email.setSubject("这是一个html邮件");
// 设置html内容,实际使用时可以从文本读入写好的html代码
email.setHtmlMsg("<div style='width:100px;height:200px;'>a</div>");
email.send();

}

// 发送复杂的邮件,包含附件等
public void sendMultiPartEmail(String toAddress, String toName) throws Exception {
MultiPartEmail email = null;
email = new MultiPartEmail();
email.setHostName(hostName);
email.setAuthentication(userName, password);
email.setCharset("utf-8");
email.addTo(toAddress, toName, "utf-8");
email.setFrom(fromAddress, fromName, "utf-8");
email.setSubject("这是有附件的邮件");
email.setMsg("<a href='#'>测试内容</a>");

// 为邮件添加附加内容
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("D:\\邮件.txt");// 本地文件
// attachment.setURL(new URL("http://xxx/a.gif"));//远程文件
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("描述信息");
// 设置附件显示名字,必须要编码,不然中文会乱码
attachment.setName(MimeUtility.encodeText("邮件.txt"));
// 将附件添加到邮件中
email.attach(attachment);
email.send();
}
}


六、附件

下载地址:http://pan.baidu.com/s/1kTv9QJl

如果失效请联系LZ

文件说明:

1、maildemo.zip :maildemo的源代码

2、mail.jar :Java
Mail的jar包

3、commons-email-1.3.1.jar :Apache Mail的jar包
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息