SMTP Authentication 
Author: nphelps 
In Reply To: SMTP Authentication  Nov 12, 2000 11:42 PM   Reply 6 of 18   
 
 
I looked all over the place for this answer, and it seemed I
would never find it. I did though with some help from Sun.
Here is the answer.Sending E-mail Using the JavaMail SMTP Provider To A SMTP Server
That Requires AuthenticationThere are two ways.1. Implement an Authenticator and pass it into your Session.Properties properties = new Properties();
properties.put("mail.smtp.host", "yoursmtpserver");
properties.put("mail.smtp.auth", "true");// inner class
private class SmtpAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication
("yourusername", "yourpassword");
}
}Session session = Session.getDefaultInstance(properties, new
SmtpAuthenticator());Message message = new Message(session)
message.setRecipients(Message.RecipientType.TO, new
InternetAddress("[email protected]"));
message.setFrom(new InternetAddress("[email protected]"));
message.setSubject("Message Subject");
message.setSentDate(new Date());
message.setText("This is the message text.");Transport.send(message);
2. Explicitly manage the Transport object and call its
connection method passing it the hostname, username, and
password.Properties properties = new Properties();
properties.put("mail.smtp.host", "yoursmtpserver");
properties.put("mail.smtp.auth", "true");Session session = Session.getDefaultInstance(properties, null);Message message = new Message(session)
message.setRecipients(Message.RecipientType.TO, new
InternetAddress("[email protected]"));
message.setFrom(new InternetAddress("[email protected]"));
message.setSubject("Message Subject");
message.setSentDate(new Date());
message.setText("This is the message text.");Transport transport = session.getTransport("smtp");
transport.connect
("yoursmtpserver", "yourusername", "yourpassword");
message.saveChanges();
transport.sendMessage(message, message.getAllRecipients());
transport.close();
Notes
It is important to note that you MUST include the
property "mail.smtp.auth" and set it to true (as done in the
above code) to turn authentication on. If this propety is not
set to the string value of "true" then the transport will not
even try to iniciate authentication. Secondly, in the managed
Transport example(number 2), you MUST use the saveChanges()
method as well as using the sendMessage() method in place of the
send() method--the send() method will not work.
 
 
Re: SMTP Authentication 
Author: 123456po 
In Reply To: SMTP Authentication  Oct 14, 2002 12:53 PM   Reply 18 of 18   
 
 
this has been very helpful. thanks for sharing it. There's might be a little typo in your code. I copied, modified and run the code. the following is a tested and working one after fixed the typo.// define host, from, recipent, user, and pass.Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");Session session = Session.getDefaultInstance(properties, null);Message message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipent));
message.setFrom(new InternetAddress(from));
message.setSubject("Message Subject");
message.setSentDate(new Date());
message.setText("This is the message text.");Transport transport = session.getTransport("smtp");
transport.connect(host, user, pass);
message.saveChanges();
transport.sendMessage(message, message.getAllRecipients());
transport.close();
 
/////////////////////////////////////////////////////////////////////////////////////////////////////////// 
一个发送MAIL的BEANpackage homepage; import java.io.IOException; 
import java.io.OutputStream; 
import java.util.Date; 
import java.util.Hashtable; 
import javax.activation.DataHandler; 
import javax.activation.FileDataSource; 
import javax.mail.*; 
import javax.mail.internet.*; 
/** 
* Title: JavaMail system 
* Description: 
* Copyright: Copyright (c) 2002 
* Company: home 
* @author coolwen.com 
* @version 1.0 
*/ 
public class SmtpAuthenticator extends javax.mail.Authenticator{ 
//SMTP身份验证 
protected javax.mail.PasswordAuthentication getPasswordAuthentication() 

return new javax.mail.PasswordAuthentication("coolwen.com","password"); 

} package homepage; import java.io.IOException; 
import java.io.OutputStream; 
import java.util.Date; 
import java.util.Hashtable; 
import javax.activation.DataHandler; 
import javax.activation.FileDataSource; 
import javax.mail.*; 
import javax.mail.internet.*; 
import java.util.*; 
import java.io.File; 
/** 
* Title: JavaMail system 
* Description: 
* Copyright: Copyright (c) 2002 
* Company: home 
* @author coolwen.com 
* @version 1.0 
*/ public class SendMail{ private String smtp="smtp.163.com",from="[email protected]"; 
//发送一个邮件 
public boolean SendMail(String subject,String body,String to,String filename){ 
try { SmtpAuthenticator sa=new SmtpAuthenticator(); 
Properties props=System.getProperties(); 
props.put("mail.smtp.auth","true"); 
props.put("mail.smtp.host",smtp); Session sess=Session.getInstance(props,sa); 
sess.setDebug(true); Message msg = new MimeMessage(sess); 
msg.setDataHandler(new DataHandler(body,"text/html; charset=gb2312")); 
if(filename.equals("") || filename == null||filename.equals("null")) 

msg.setFrom(new InternetAddress(from)); 
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false)); 
msg.setSubject(subject); 
}else{ 
msg.setFrom(new InternetAddress(from)); 
msg.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to)); 
msg.setSubject(subject); 
MimeBodyPart mbp1 = new MimeBodyPart(); 
mbp1.setText(body); 
MimeBodyPart mbp2 = new MimeBodyPart(); 
//File file=new File(filename); 
FileDataSource fds = new FileDataSource(filename); 
mbp2.setDataHandler(new DataHandler(fds)); 
mbp2.setFileName(fds.getName()); 
Multipart mp = new MimeMultipart(); 
mp.addBodyPart(mbp1); 
mp.addBodyPart(mbp2); 
msg.setContent(mp); 

msg.setSentDate(new Date()); 
//msg.setText(body); 
Transport.send(msg); 
return true; 
} catch (Exception e) { 
return false; 



在JSP中使用 <%@page contentType="text/html; charset=gb2312" language="java"%> 
<jsp:useBean id="send" scope="page" class="homepage.SendMail" /> 
<% 
if(send.SendMail("中国","中国","[email protected]","D:\\myGUI.java")) 
out.println("ok"); 
else 
out.println("no"); 
%>