JavaMail Study (send with Attachment)

Source: Internet
Author: User

Used to think that JavaMail is a very tall thing, so on the Internet to find the knowledge about JavaMail, in fact, did not think so tall, I just learned to send the mail part, then I would like to paste the code I wrote, specific implementation code has a detailed explanation

The first is to send a regular message

//Send regular mail     Public Static voidSendtextmail ()throwsmessagingexception {//first step. Get session (Note: This is the class under Javax.mail)        /** public static Session getinstance (java.util.Properties props, Authenticator Authenticator) * * Props need to specify two key values, one is to specify the server hostname, and the other is to specify whether authentication is required! This setting must verify that true * Authenticator is an interface that represents the authenticator, which is the client's identity login. We need to implement this interface ourselves to implement this interface requires the use of accounts and passwords*/Properties Props=NewProperties (); Props.setproperty ("Mail.host", "smtp.sohu.com"); Props.setproperty ("Mail.smtp.auth", "true"); Authenticator Auth=NewAuthenticator () {@Overrideprotectedpasswordauthentication getpasswordauthentication () {return NewPasswordauthentication ("lishun1005",                        "leason841553484");        }        }; Session Session=session.getinstance (props, auth); //Step Two: Create a MimeMessage objectMimeMessage msg =NewMimeMessage (session); Msg.setfrom (NewInternetAddress ("[email protected]");//Set Sendermsg.setrecipients (recipienttype.to,"[Email protected]");//send, can be sent to many people, the equivalent of massMsg.setrecipients (RECIPIENTTYPE.BCC, "[email protected]");//Dark Send (only to one person)Msg.setrecipients (recipienttype.cc, "[email protected]");//cc Person:Msg.setsubject ("Sent to Wang Nima");//set the message header;Msg.setcontent ("Regular mail", "text/plan;charset=utf-8");//set the encoding for body and body//Send mailtransport.send (msg); }

The second one is sent with an attachment

//Mail with Attachments     Public Static voidSendtextandfilemail ()throwsmessagingexception, IOException {Properties props=NewProperties (); Props.setproperty ("Mail.host", "smtp.sohu.com"); Props.setproperty ("Mail.smtp.auth", "true"); Authenticator Auth=NewAuthenticator () {@Overrideprotectedpasswordauthentication getpasswordauthentication () {return NewPasswordauthentication ("lishun1005",                        "leason841553484");        }        }; Session Session=session.getinstance (props, auth); MimeMessage msg=NewMimeMessage (session); Msg.setfrom (NewInternetAddress ("[email protected]");//Set SenderMsg.setrecipients (recipienttype.to, "[Email protected]"); Msg.setsubject ("Sent to Wang Nima"); /** When sending a message containing attachments, the message body is a multi-part form! 1. Create a multipart part content! Mimemultipart * Mimemultipart is a collection that is used to load multiple body parts!         2. We need to create two main body parts, one is text content and the other is an attachment. * Main part is called MimeBodyPart 3. Set the Mimemultipart to mimemessage content! This content is found in the online learning materials, I am interested in their own to understand again*/Mimemultipart List=NewMimemultipart ();//create multi-part content//Create MimeBodyPartMimeBodyPart part1 =NewMimeBodyPart (); //set the contents of the textPart1.setcontent ("Mail with Attachments", "Text/html;charset=utf-8"); //add text to the collectionList.addbodypart (part1); //Create MimeBodyPartMimeBodyPart Part2 =NewMimeBodyPart (); Part2.attachfile (NewFile ("d:/Wang Nima. jpg"));//set the contents of an attachmentPart2.setfilename (Mimeutility.encodetext ("Wang Nima. jpg"));//sets the name of the file displayed, where Encodetext is used to handle the Chinese garbled problemList.addbodypart (part2); Msg.setcontent (list);//set it to the message as the content of the message. //Send mailtransport.send (msg); }

JavaMail Study (send with Attachment)

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.