Java mail example (HTML content) using org. Apache. commons. Mail

Source: Internet
Author: User
Tags mail example
Package mail; import java. Io. unsupportedencodingexception;
Import java.net. malformedurlexception;
Import java.net. url; import javax. Mail. Internet. mimeutility; import org. Apache. commons. Mail. emailattachment;
Import org. Apache. commons. Mail. emailexception;
Import org. Apache. commons. Mail. htmlemail;
Import org. Apache. commons. Mail. multipartemail;
Import org. Apache. commons. Mail. simpleemail; public class send3 {
Public send3 (){}
// Required jar package 5: commons-email-1.2.jar commons-email-1.2-javadoc.jar
// Commons-email-1.2-sources.jar mail. Jar activation. jar.
Public static void main (string [] ARGs ){
Send ();
} // The POP3 and SMTP server address corresponding to the QQ mailbox is POP3: pop.qq.com;
// SMTP: smtp.qq.com. Refer

Public static void send (){

Try {
Htmlemail email = new htmlemail ();
Email. settls (true );
Email. sethostname ("smtp.qq.com"); // The Name Of The sending server.
Email. setauthentication ("449333333@qq.com", "Mailbox password ");

Email. addto ("lu@yahoo.com.cn", "REC"); // recipient
Email. setfrom ("449333333@qq.com"); // sender
Email. setsubject ("Java mail test"); // Title
Email. setcharset ("gb2312"); // sets charset
Email. setmsg ("this is a test email sent by the Java program with an attachment. "); // Content

// Embed the image and get the content ID
URL;
Url = new URL ("http://img.baidu.com/img/image/ilogob.gif ");

String cid = Email. Embed (URL, "Baidu logo ");
Email. sethtmlmsg ("<HTML> <a href = 'HTTP: // www.baidu.com '> Baidu.com </a> + "\"> <Br> <p> Haha </P>
Email. Send (); // send
System. Out. println ("email sent successfully ");
} Catch (emailexception e ){
E. printstacktrace ();
} Catch (malformedurlexception e ){
E. printstacktrace ();
}
}
}
/*
* Some mailboxes need to enable the POP3/SMTP service, such as the QQ mailbox in this example. During the test, a prompt (sent from the QQ mailbox) is displayed, and the mailbox settings are displayed, enable
*/

Related Article

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.