A small example of JavaMail sending mail, javamail sending mail
Package Demo1;
Import java. util. Properties;
Import javax. mail. Address;
Import javax. mail. Message;
Import javax. mail. Session;
Import javax. mail. Transport;
Import javax. mail. internet. InternetAddress;
Import javax. mail. internet. MimeMessage;
Public class JavaMailDemo1 {
Public static void main (String args []) throws Exception
{
Properties props = new Properties ();
Props. setProperty ("mail. smtp. auth", "true ");
Props. setProperty ("mail. transport. protocol", "smtp ");
Session session = Session. getInstance (props );
Session. setDebug (true );
Message msg = new MimeMessage (session );
Msg. setText ("how are you? ");
Msg. setSubject ("hello ");
Msg. setFrom (new InternetAddress ("z_p_u18064117557@163.com "));
Transport transport = session. getTransport ();
Transport. connect ("smtp.163.com", 25, "z_p_u18064117557 ","**********");
Transport. sendMessage (msg, new Address [] {new InternetAddress ("z_p_u18064117557@163.com ")});
// Transport. send (msg );
Transport. close ();
}
}
Package Demo1;
Import java. util. Properties;
Import javax. mail. Message;
Import javax. mail. Message. RecipientType;
Import javax. mail. Authenticator;
Import javax. mail. PasswordAuthentication;
Import javax. mail. Session;
Import javax. mail. Transport;
Import javax. mail. internet. InternetAddress;
Import javax. mail. internet. MimeMessage;
Public class JavaMailDemo2 {
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.sohu.com ");
Session session = Session. getInstance (props, new Authenticator (){
Protected PasswordAuthentication getPasswordAuthentication ()
{
Return new PasswordAuthentication ("z_p_u ","*********");
}
});
Session. setDebug (true );
Message msg = new MimeMessage (session );
Msg. setFrom (new InternetAddress ("z_p_u@sohu.com "));
Msg. setSubject ("hello ");
// Msg. setText ("hello ");
Msg. setContent ("Msg. setRecipients (RecipientType. TO, InternetAddress. parse ("z_p_u@sohu.com "));
Transport. send (msg );
}
}
Package Demo1;
Import java. io. File;
Import java. io. FileInputStream;
Import java. util. Properties;
Import javax. activation. DataHandler;
Import javax. activation. DataSource;
Import javax. activation. FileDataSource;
Import javax. mail. Authenticator;
Import javax. mail. Message;
Import javax. mail. Message. RecipientType;
Import javax. mail. PasswordAuthentication;
Import javax. mail. Session;
Import javax. mail. Transport;
Import javax. mail. internet. InternetAddress;
Import javax. mail. internet. MimeBodyPart;
Import javax. mail. internet. MimeMessage;
Import javax. mail. internet. MimeMultipart;
Import javax. mail. internet. MimeUtility;
Public class JavaMailDemo3 {
Public static void main (String [] args) throws Exception {
Properties props = new Properties ();
Props. setProperty ("mail. smtp. auth", "true ");
Props. setProperty ("mail. transport. proptocol", "smtp ");
Props. setProperty ("mail. host", "smtp.sohu.com ");
Session session = Session. getInstance (props, new Authenticator (){
Protected PasswordAuthentication getPasswordAuthentication ()
{
Return new PasswordAuthentication ("z_p_u ","********");
}
});
Session. setDebug (true );
Message msg = new MimeMessage (session );
MimeMultipart mp = new MimeMultipart ("related ");
MimeBodyPart html = new MimeBodyPart ();
MimeBodyPart gif = new MimeBodyPart ();
Mp. addBodyPart (gif );
Mp. addBodyPart (html );
Html. setContent ("
// Gif. setContent ("
DataSource ds = new FileDataSource (new File ("img/3.png "));
DataHandler dh = new DataHandler (ds );
Gif. setDataHandler (dh );
Gif. setHeader ("Content-Location", "http://www.bkjia.com/uploads/allimg/150110/0T40455P-0.jpg ");
Gif. setFileName (MimeUtility. encodeText ("image .png "));
Msg. setFrom (new InternetAddress (MimeUtility. encodeText ("China") + "<z_p_u@sohu.com> "));
Msg. setContent (mp );
Msg. setRecipient (RecipientType. BCC, new InternetAddress ("z_p_u@sina.cn "));
Msg. saveChanges ();
Transport. send (msg, InternetAddress. parse ("z_p_u@sohu.com "));
}
}