用JAVAMAIL 发邮件

需要 mail.jar activation.jar 两个JAR包,经过测试WINDOWS 下 ECLIPSE 和UNIX下测试,均发送成功

<%@ page contentType="text/html;charset=GBK" language="java" pageEncoding="GBK"%><%@ page import="java.net.URL,javax.activation.*,javax.mail.internet.*,java.io.*,java.util.*,javax.mail.*,javax.mail.internet.*,javax.activation.* " %>

<%

try
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.sohu.com");
props.put("mail.smtp.auth", "true");
Session msession = Session.getInstance(props,new Authenticator() {public PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("username", "password");}});
Message msg = new MimeMessage(msession);
msg.setFrom(new InternetAddress("fromaddr"));
InternetAddress[] tos = InternetAddress.parse("toaddr");
msg.setRecipients(Message.RecipientType.TO, tos);
InternetAddress[] toscc = InternetAddress.parse("ccaddr");
msg.setRecipients(Message.RecipientType.CC, toscc);

InternetAddress[] tosbcc = InternetAddress.parse("bccaddr");
msg.setRecipients(Message.RecipientType.BCC, tosbcc);
msg.setSubject("testMail");
msg.setText("abcd");
Transport.send(msg);
}
catch(Exception e)
{
e.printStackTrace();
}

%>

你可能感兴趣的:(java,eclipse,windows,.net,unix)