First of all, you need to download two JAR packages.
One is mail.jar, download address: http://www.javasoft.com/products/javaMail/
The other is ActiVation.jar package, address: http://java.sun.com/beans/glasgow/jaf.html
And set to ClassPath,
Basic code is as follows
Package groupsms;
Import java.util.properties; import java.util.date; import javax.mail. *; import javax.mail.internet. *; import javax.activation. *; // Import sendmail.email_authertatorBean;
/ ** * * @Author SWEATER * @version 1.0, 09/01/04 * @since jdk1.4.2 * * Using JavaMail, send email * * / public class sendmail {public string to; public string from; public string Subject ; public String mailhost; public String content; public String user; public String password; public String cc; public String bcc; public SendMail (String to, String from, String subject, String mailhost, String content, String user, String password, String CC, String BCC) {this.to = TO; this.From = from; this.subject = Subject; this.mailhost = mailhost; this.content = Content; this.user = user; this.password = password; this. CC = cc; this.bcc = bcc; / * system.out.println (user); system.out.println (password); system.out.println (Subject); system.out.println; system. Out.println (to); system.out.println (from); * /} public int sendmailBysmtp () {int flag = -1; // mime mail object mimeMessage mimeMsg = null; // message session object session session SSION = NULL; try {proties = system.getProperties (); // Get system properties PrOPS.PUT ("mail.smtp.host", mailhost); // Set SMTP host Props.Put ("mail.smtp.auth "" True "); // Set authentication as true, if authentication must be set to true
// Get mail session object // session = Session.getDefaultInstance (props, null); // Note Session.getDefaultInstance method of this line of the second parameter session = Session.getDefaultInstance (props, new Email_Autherticatorbean (user, password) ); // Create MIME mail object MIMEMSG = new mimeMessage (session); // Set the sender mimsg.setFrom (new internetdress (from)); // Set the recipient IF (to! = Null) {mimsg.setriPients (Message .Recipienttype.to, internetaddress.parse (to));} // Setting Cc Cc i (cc! = Null) {mimeMsg.seTrecipients (message.recipienttype.cc, internetaddress.parse (cc));} // Setting Dark delivery person if (bcc! = Null) {mimemsg.seTrecipients (message.recipienttype.bcc, internetdress.parse (bcc));} // set mail topic mimeMsg.setsubject (Subject, "GBK"); // Setup mail Content MIMEMSG.SETTEXT (Content, "GBK"); // Send Date MIMEMSG.SetSentDate (New Date ()); // Send Mail Transport.send (MIMEMSG); FLAG = 120; //system.out.println (" Email Send! ");} catch (messagingException msginge) {flag = 121; system.out.println (msginge.getMessage ());} Return flag;}
class Email_Autherticatorbean extends javax.mail.Authenticator {private String m_username = null; private String m_userpass = null; public void setUsername (String username) {m_username = username;} public void setUserpass (String userpass) {m_userpass = userpass;}
Public email_autherticatorbean () {super ();
public Email_Autherticatorbean (String username, String userpass) {super (); setUsername (username); setUserpass (userpass);} // This method must be, which is required in the public identity is automatically invoked when the verification PasswordAuthentication getPasswordAuthentication () { Return New PasswordAuthentication (m_username, m_userpass);}} public static void main (String [] args) {sendmail sm = new sendmail ("sswt @ 163.com, sswt @ 163.com", "sswt@163.com", "Sswt@163.com", "SMTP.163.com", "SMTP.163.com", "SSWT", "Password", "Sswt@163.com", "sswt@163.com"); }