import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendMail {
// Send a simple, single part, text/plain e-mail
public void sendSimple(String toAddress, String body){
String DEFAULT_ENCODING = "UTF-8";
// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
String to = toAddress;
String from = "your_email_here";
// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
String host = "your_email_server_host_here";
// Create properties, get Session
Properties props = new Properties();
// If using static Transport.send(),
// need to specify which host to send it to
props.put("mail.smtp.host", host);
// To see what is going on behind the scene
props.put("mail.debug", "true");
Session session = Session.getInstance(props);
try {
// Instantiatee a message
MimeMessage msg = new MimeMessage(session);
//Set message attributes
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("your_subject_here", DEFAULT_ENCODING);
msg.setSentDate(new Date());
// Set message content
msg.setText(body, DEFAULT_ENCODING);
//Send the message
Transport.send(msg);
}
catch (MessagingException mex) {
// Prints all nested (chained) exceptions as well
mex.printStackTrace();
}
}
}
Link download JavaMail component: here
Note: if you use JavaMail on Web application, please copy mail.jar to lib folder of Web Application Server
Không có nhận xét nào:
Đăng nhận xét