JavaMail Create a message and send a message summary

Source: Internet
Author: User

(Note: This article is a reference to Thanks for the wonderful description of the blogger)

One, pre-preparation

1, Import Mail.jar

Two, the operation steps:

1, create the Properties object, and set the related attributes, including:, Mail.transport.protocol, Mail.smtp.auth.

2, create the Session object, set the network environment to send the message, that is, import the Properties object.

3. Gets the transport object from the session object for sending mail. The transport object implements the mail-sending protocol, such as SMTP

4. Connect to mail server using username and password

5, Create message instance (MimeMessage is used here because the content of the message is more complex)

5.1 Create MimeBodyPart instances (including: text, pictures, attachments) and set the content (SetContent (), Setdatahandler ()) and ID or name.

5.2 Create a Relationship description instance Mimemultipart, add the elements added, and set the relationship type (related, mixed)

5.3 Set the relationship description instance to the content of the message.

5.4 Save changes, saveChanges ();

6, Send mail: Transport.sendmessage ();

1  PackageJason.mail;2 3 Importjava.util.Properties;4 5 ImportJavax.activation.DataHandler;6 ImportJavax.activation.FileDataSource;7 ImportJavax.mail.Message;8 Importjavax.mail.Session;9 ImportJavax.mail.Transport;Ten Importjavax.mail.internet.InternetAddress; One ImportJavax.mail.internet.MimeBodyPart; A ImportJavax.mail.internet.MimeMessage; - ImportJavax.mail.internet.MimeMultipart; -  the  Public classSimplesendmail { -      Public Static voidMain (string[] args)throwsexception{ -Properties prop =NewProperties (); -Prop.setproperty ("", ""); +Prop.setproperty ("Mail.transport.protocol", "SMTP"); -Prop.setproperty ("Mail.smtp.auth", "true"); +          A         //5 Steps to send a message using JavaMail at         //1. Create a session, that is, the network environment for sending mail -Session session =session.getinstance (prop); -          -         //turn on the session debug mode, so you can see the status of sending email -Session.setdebug (true); -          in         //2. Get transport object: get through session -Transport ts =Session.gettransport (); to          +         //3. Connect to mail server with username and password -Ts.connect ("", "liujun4719", "jason./1234"); the          *         //4. Create a message $Message message =Createmail (session);Panax Notoginseng          -         //5. Send mail the         inti = 1; +          while(i++ <= 6){ A ts.sendmessage (Message, message.getallrecipients ()); the         } + ts.close (); -          $     } $      -      -      Public StaticMimeMessage Createmail (Session session)throwsexception{ the          -         //Create a messageWuyiMimeMessage message =NewMimeMessage (session); the          -         //set up basic information for a message WuMessage.setfrom (NewInternetAddress ("[Email protected]")); -Message.setrecipient (Message.RecipientType.TO,NewInternetAddress ("[Email protected]")); AboutMessage.setsubject ("Mail with attachments and pictures"); $          -         //Body -MimeBodyPart Text =NewMimeBodyPart (); -Text.setcontent ("Hello", "Text/html;charset=utf-8"); A          +         //Image theMimeBodyPart image =NewMimeBodyPart (); -Image.setdatahandler (NewDataHandler (NewFiledatasource ("/home/liuxiangjun/doc/mailtest/mailtestpic.png"))); $Image.setcontentid ("MailTest"); the          the         //Accessories theMimeBodyPart attach =NewMimeBodyPart (); theDataHandler DH =NewDataHandler (NewFiledatasource ("/home/liuxiangjun/doc/mailtest/")); - Attach.setdatahandler (DH); in Attach.setfilename (Dh.getname ()); the          the         //describe relationships: body and picture AboutMimemultipart MP1 =NewMimemultipart (); the Mp1.addbodypart (text); the Mp1.addbodypart (image); theMp1.setsubtype ("related"); +          -          the         //describe relationships: Body and attachmentsBayiMimemultipart MP2 =NewMimemultipart (); the Mp2.addbodypart (attach); the          -         //BodyPart representing the body -MimeBodyPart content =NewMimeBodyPart (); the content.setcontent (MP1); the Mp2.addbodypart (content); theMp2.setsubtype ("Mixed"); the          - message.setcontent (MP2); the message.savechanges (); the          the         returnmessage;94          the     } the  the}
View Code

JavaMail Create a message and send a message summary

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: 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.