A small example of JavaMail sending mail, javamail sending mail

Source: Internet
Author: User

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 "));
}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.