The example of this article describes how JavaMail implements mail delivery. Share to everyone for your reference. Specifically as follows:
Download Activation.jar and Mail.jar
Configure Classpath to add the top two jar packs to the classpath.
JavaBean: "Sendemail.java"
Package CLS;
Import Java.util.Date;
Import java.util.Properties;
Import javax.mail.*;
Import javax.mail.internet.*;
public class SendEmail {String host;
String username;
String password;
String fromaddr;
String toaddr;
String subject;
String content;
Public SendEmail () {//variable initialization host = ' smtp.qq.com ';
Username = "";
Password = "";
FROMADDR = "@";
///Send mail public boolean sendMail () {Properties prop;
Session session;
MimeMessage msg;
try {prop = new Properties ();///Storage Connection Parameters Prop.put ("Mail.smtp.host", host);
Prop.put ("Mail.smtp.auth", "true");
Session = Session.getdefaultinstance (prop,null); Get the session msg = new mimemessage for a message (session);
Mail Message/Check email address is valid if (fromaddr = null | | fromaddr.equals ("")) {throw new Exception ("Send address Error");
} if (toaddr = null | | toaddr.equals ("")) {throw new Exception ("Destination address error");
//Set Source address Msg.setfrom (new internetaddress (FROMADDR)); Set Destination Address Msg.setRecipient (Message.recipienttype.to,new internetaddress (toaddr));
Set subject Msg.setsubject (subject); Multipart MP = new Mimemultipart ();
Message content MimeBodyPart mbpcontent = new MimeBodyPart (); Mbpcontent.setcontent (Content, "text/html");
Message Format Mp.addbodypart (mbpcontent);
Msg.setcontent (MP);
Msg.setsentdate (New Date ());
Send mail Transport transport = Session.gettransport ("SMTP");
Transport.connect ((String) prop.get ("Mail.smtp.host"), Username,password);
Transport.sendmessage (Msg,msg.getrecipients (MimeMessage.RecipientType.TO));
Transport.close ();
return true;
catch (Exception e) {System.out.println (e);
return false;
}//getter and setter public String GetPassword () {return password;
} public void SetPassword (String password) {this.password = password;
Public String GetHost () {return host;
public void Sethost (String host) {this.host = host; Public String GetUserName () {return username;
} public void Setusername (String username) {this.username = username;
Public String getfromaddr () {return this.fromaddr;
} public void Setfromaddr (String addr) {fromaddr = addr;
Public String gettoaddr () {return this.toaddr;
} public void Settoaddr (String addr) {toaddr = addr;
Public String Getsubject () {return subject;
public void Setsubject (String sub) {subject = sub;
Public String getcontent () {return content;
public void SetContent (String content) {this.content = content; }
}
Sendmail.jsp:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" Utf-8 "%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
I hope this article will help you with your Java programming.