文件一:MyAuthenticator.java用途: 返回一个PasswordAuthentication 类的对象。import javax.mail.PasswordAuthentication; import javax.mail.Authenticator; public class MyAuthenticator extends Authenticator { public MyAuthenticator() { super(); } public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("用户名","用户密码"); } }
文件二:MailExample.java用途:发送邮件import java.util.Properties; import javax.mail.*; import javax.mail.internet.*;public class MailExample { public static void main (String args[]) throws Exception { String host = args[0]; String from = args[1]; String to = args[2]; // Get system properties Properties props = System.getProperties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth","true"); // Setup mail server Authenticator auth = new MyAuthenticator(); Session session = Session.getDefaultInstance(props, auth); //Session session = Session.getDefaultInstance(props, null); // Get session MimeMessage message = new MimeMessage(session); // Define message // Set the from address message.setFrom(new InternetAddress(from)); // Set the to address message.setRecipient(Message.RecipientType.TO,new InternetAddress(to)); // Set the subject message.setSubject("javamail测试邮件2"); // Set the content message.setText("嘿嘿,这是我的测试邮件!"); // Send message //message.saveChanges(); // implicit with send() Transport transport = session.getTransport("smtp"); //transport.connect(host, "用户名", "用户密码"); //transport.sendMessage(message, message.getAllRecipients()); //transport.close(); transport.send(message); transport.close(); } }
很简单,看看就知道了,主要是MyAuthenticator类和MailExample.java文件中的以下几条语句:Properties props = System.getProperties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth","true"); // Setup mail server Authenticator auth = new MyAuthenticator(); Session session = Session.getDefaultInstance(props, auth);
老兄你怎麽知道的呀,我javamail的doc看了半天也没找到
www.jguru.com我是在jguru的论坛上找到的
xbf老兄,我用了你的例子都不行啊!Weblogic整天说“Access to default session denied” 可否发一个可用的java源程序给我!不胜感激! [email protected]
[email protected]
import javax.mail.Authenticator;
public class MyAuthenticator extends Authenticator {
public MyAuthenticator() {
super();
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("用户名","用户密码");
}
}
import javax.mail.*;
import javax.mail.internet.*;public class MailExample {
public static void main (String args[]) throws Exception {
String host = args[0];
String from = args[1];
String to = args[2]; // Get system properties
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
// Setup mail server
Authenticator auth = new MyAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
//Session session = Session.getDefaultInstance(props, null);
// Get session
MimeMessage message = new MimeMessage(session);
// Define message // Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
// Set the subject
message.setSubject("javamail测试邮件2");
// Set the content
message.setText("嘿嘿,这是我的测试邮件!");
// Send message
//message.saveChanges(); // implicit with send()
Transport transport = session.getTransport("smtp");
//transport.connect(host, "用户名", "用户密码");
//transport.sendMessage(message, message.getAllRecipients());
//transport.close();
transport.send(message);
transport.close();
}
}
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
// Setup mail server
Authenticator auth = new MyAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
[email protected]