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
*/