日期:2014-05-20 浏览次数:21121 次
<%@page import="java.util.*"%>
<%@page import="javax.mail.*"%>
<%@page import="javax.mail.internet.*"%>
<%
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.example.com");
Session s = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(s);
InternetAddress from = new InternetAddress(" you@example.comThis e-mail address is being protected from spam bots, you need JavaScript enabled to view it ");
message.setFrom(from);
InternetAddress to = new InternetAddress(" you@example.comThis e-mail address is being protected from spam bots, you need JavaScript enabled to view it ");
message.addRecipient(Message.RecipientType.TO, to);
message.setSubject("Test from JavaMail.");
message.setText("Hello from JavaMail!");
Transport.send(message);
%>
<html>
<p align="center">
A Message has been sent. <br>
Check your inbox.
</p>
<p align="center">
<a href="sendmail.jsp">Click here to send another!</a>
</p>
</html>
------解决方案--------------------
/**
*用apache commons-email 发送邮件
*依赖jar:commons-email.jar,activation.jar,mail.jar
*/
public static void sendMutiMessage() {
MultiPartEmail email = new MultiPartEmail();
String[] multiPaths = new String[] { "D:/1.jpg", "D:/2.txt" };
List<EmailAttachment> list = new ArrayList<EmailAttachment>();
for (int j = 0; j < multiPaths.length; j++) {
EmailAttachment attachment = new EmailAttachment();
//判断当前这个文件路径是否在本地 如果是:setPath 否则 setURL;
if (multiPaths[j].indexOf("http") == -1) {
attachment.setPath(multiPaths[j]);
} else {
try {
attachment.setURL(new URL(multiPaths[j]));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
list.add(attachment);
}
try {
// 这里是发送服务器的名字:
email.setHostName("smtp.126.com");
// 编码集的设置
email.setCharset("utf-8");
// 收件人的邮箱
email.addTo("slimes@126.com");
// 发送人的邮箱
email.setFrom("yuhan0@126.com");
// 如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码
email.setAuthentication("yuhan0", "******");
email.setSubject("这是一封测试邮件");
// 要发送的信息
email.setMsg("<b><a href=\"http://www.baidu.com\">邮件测试内容</a></b>");
for (int a = 0; a < list.size(); a++) //添加多个附件
{
email.attach(list.get(a));
}
// 发送
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
}
------解决方案--------------------
http://blog.csdn.net/daijope/article/details/6567134
------解决方案--------------------
看看这个吧
http://blog.csdn.net/lenolong/article/details/3970110