Java example of sending emails (with attachments) using org. Apache. commons. Mail

Source: Internet
Author: User
Package mail; import java. Io. unsupportedencodingexception; import javax. Mail. Internet. mimeutility; import org. Apache. commons. Mail. emailattachment;
Import org. Apache. commons. Mail. emailexception;
Import org. Apache. commons. Mail. multipartemail;
Import org. Apache. commons. Mail. simpleemail; public class send2 {
Public send2 (){}
// 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
// Lu@yahoo.com.cn
// 449222222@qq.com
Public static void send (){

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

// Generate an attachment
Emailattachment attachment = new emailattachment ();
Attachment. setpath ("D:/1.jpg"); // specify the absolute path of the local attachment.
// You can also use the network
// Attachment. seturl (new URL ("http://www.apache.org/images/asf_logo_wide.gif"); attachment. setdisposition (emailattachment. Attachment );
Attachment. setdescription ("attachment description"); // attachment description
// The attachment name. If the attachment is a Chinese name, it will be garbled, attachment. setname (mimeutility. encodetext ("test "));
Attachment. setname (mimeutility. encodetext ("attachment name "));

Email. addto ("lushuaiyin@yahoo.com.cn", "REC"); // The recipient // 2nd parameters are nicknames, can not, the recipient is displayed in this area of the mailbox
// Recipient: REC <lushuaiyin@yahoo.com.cn>


Email. setfrom ("449020109@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

Email. Attach (Attachment); // Add an attachment

Email. Send (); // send
System. Out. println ("email sent successfully ");
} Catch (emailexception e ){
E. printstacktrace ();
} Catch (unsupportedencodingexception e ){
// Todo auto-generated Catch Block
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.