1. Java Mail Introduction
2. JAF Introduction
3. First: JavaMail case analysis
Package com.csdn.itcast;
Import java.util.Properties;
Import javax.mail.Address;
Import Javax.mail.Message;
Import javax.mail.MessagingException;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.AddressException;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage; public class Demo1 {/** * @param args * @throws messagingexception * @throws addressexception * * Development when prompted not to find certain drivers, to see if you forgot to import certain packages/public static void main (string[] args) throws Addressexception, Messagingexception
{//5. You need to pass in a props attribute when instantiating the session properties props = new properties ();
Set Login authentication Mode Props.setproperty ("Mail.smtp.auth", "true");
Set transport protocol Props.setproperty ("Mail.transport.protocol", "SMTP");
4. When you create a satellite, you need to pass in a session sessions session = Session.getinstance (props);
Session.setdebug (TRUE); 1. Create a message information, such as creating a satellite; MimeMessage is a subclass of a message because it is an abstract class, so here instantiate its subclass msg = new MimeMessage (SessiON); Set the content of the message and sender, note: Here the sender is set, solid you can fill in the sender's mailbox, the fake Msg.settext ("Hello.")
");
Msg.setfrom (New InternetAddress ("yang@163.com"));
2. Create a tool for sending mail, like creating a rocket transport transport = Session.gettransport ();
Transport.connect ("smtp.sina.com", "Itcast_test", "123456"); * * 3. Rocket-launched satellite; the SendMessage () method is a method of transport object invocation, and send () is a method of transport direct invocation, that is, static method *; The latter is a method of merging the transport connection, which is suitable for sending a message , because if multiple connections are sent, the former is reversed/transport.sendmessage (msg, new address[] {new InternetAddress ("Itcast_test@soh
U.com ")});
Transport.send (MSG, new address[]{new//InternetAddress ("Itcast_test@sohu.com")});
6. Close resource Transport.close ();
} 4. Second: A complicated case package com.csdn.itcast;
Import java.util.Properties;
Import javax.mail.Address;
Import Javax.mail.Authenticator;
Import Javax.mail.Message;
Import javax.mail.PasswordAuthentication;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;Import Javax.mail.internet.MimeMessage.RecipientType;
Import Javax.mail.search.ReceivedDateTerm; public class Demo2 {/** * @param args */public static void main (string[] args) throws exception{Properties
props = new Properties ();
Props.setproperty ("Mail.smtp.auth", "true");
Props.setproperty ("Mail.transport.protocol", "SMTP");
Props.setproperty ("Mail.host", "smtp.sina.com"); The following is written in the policy mode: This is the time to create a session directly into the face of a feedback reader (new authenticator anonymous internal class), to set the login name and login password Session sessions =
Session.getinstance (Props,new Authenticator () {protected passwordauthentication getpasswordauthentication () {
return new Passwordauthentication ("Itcast_test", "123456");
}
});
Session.setdebug (TRUE);
msg = new MimeMessage (session);
Msg.setfrom (New InternetAddress ("itcast_xxx@sina.com"));
Msg.setsubject ("Chinese theme");
Here the Internetaddress.parse () method is the same as the new address[] {} used in the previous case, and parse () can pass in multiple parameters to return an array of internetaddress type Msg.setrecipients (Recipienttype.to,interNetaddress.parse ("itcast_test@sina.com,itcast_test@sohu.com")); Msg.setcontent ("<span style= ' color:red ') > You're a fool."
</span> "," text/html;charset=utf-8 ");
Transport.send (msg);
}
}