JavaMail email development, javamail mail

Source: Internet
Author: User

JavaMail email development, javamail mail

1. Only text emails

The code example is as follows:

Package com. lyh. sendemail; import java. util. properties; import javax. mail. message; import javax. mail. session; import javax. mail. transport; import javax. mail. internet. internetAddress; import javax. mail. internet. mimeMessage; // send the public class MessageDemo1 {public static void main (String [] args) throws Exception {Properties props = new Properties (); // key value: configure the parameter. Configure props when actually sending the email. setProperty ("mail. transport. protocol "," smtp "); // specifies the protocol for sending mails. The parameter is the props specified in the specification. setProperty ("mail. host "," smtp.163.com "); // specifies the address of the sender server. The parameter is specified in the Specification // props. setProperty ("mail. debug "," true "); // debug mode of mail sending. The parameter is the props specified in the specification. setProperty ("mail. smtp. auth "," true "); // request the server for identity authentication. The parameter is related to the specific JavaMail implementation. Session session = Session. getInstance (props); // The Environment used to send the email to configure the session. setDebug (true); MimeMessage message = new MimeMessage (session); // set the mail header message. setFrom (new InternetAddress ("xxx@163.com"); message. setRecipients (Message. recipientType. TO, "xxx@qq.com"); message. setSubject ("This is second message"); // sets the body message. setContent ("

2. emails with images

A. Complex email encapsulation model

Code example

Package com. lyh. sendemail; import java. util. properties; import javax. activation. dataHandler; import javax. activation. fileDataSource; import javax. mail. message; import javax. mail. session; import javax. mail. transport; import javax. mail. internet. internetAddress; import javax. mail. internet. mimeBodyPart; import javax. mail. internet. mimeMessage; import javax. mail. internet. mimeMultipart; // send the public class MessageDem email. O2 {public static void main (String [] args) throws Exception {Properties props = new Properties (); // key value: configuration parameter. Configure props when actually sending the email. setProperty ("mail. transport. protocol "," smtp "); // specifies the protocol for sending mails. The parameter is the props specified in the specification. setProperty ("mail. host "," smtp.163.com "); // specifies the address of the sender server. The parameter is specified in the Specification // props. setProperty ("mail. debug "," true "); // debug mode of mail sending. The parameter is the props specified in the specification. setProperty ("mail. smtp. auth "," true "); // request the server for identity authentication. The parameter is related to the specific JavaMail implementation. Session session = Session. getInstance (props); // The Environment used to send the email to configure the session. setDebug (true); MimeMessage message = new MimeMessage (session); // set the mail header message. setFrom (new InternetAddress ("xxx@163.com"); message. setRecipients (Message. recipientType. TO, "xxx@qq.com"); message. setSubject ("This is second message"); // you can specify the text part MimeBodyPart textPart = new MimeBodyPart. setContent ("aaa  aaa", "text/html"); // MimeBodyPart imagePart = new MimeBodyPart (); imagePart. setContentID ("mm"); // Add files on the disk to the part and use the JAF framework DataHandler dh = new DataHandler (new FileDataSource ("src/0.jpg"); imagePart. setDataHandler (dh); MimeMultipart mp = new MimeMultipart (); mp. addBodyPart (textPart); mp. addBodyPart (imagePart); mp. setSubType ("related"); // message related to the subtype. setContent (mp); message. saveChanges (); // send the email Transport ts = session. getTransport (); ts. connect ("xxx@163.com", "123456"); // The password is the authorization code not the logon password of the mailbox ts. sendMessage (message, message. getAllRecipients (); // object, using the instance method }}

3. emails with text, images, and attachments

Code example:

Package com. lyh. sendemail; import java. util. properties; import javax. activation. dataHandler; import javax. activation. fileDataSource; import javax. mail. message; import javax. mail. session; import javax. mail. transport; import javax. mail. internet. internetAddress; import javax. mail. internet. mimeBodyPart; import javax. mail. internet. mimeMessage; import javax. mail. internet. mimeMultipart; import javax. mail. internet. mi MeUtility; // send the public class MessageDemo3 {public static void main (String [] args) throws Exception {Properties props = new Properties (); // key value: configuration parameter. Configure props when actually sending the email. setProperty ("mail. transport. protocol "," smtp "); // specifies the protocol for sending mails. The parameter is the props specified in the specification. setProperty ("mail. host "," smtp.163.com "); // specifies the address of the sender server. The parameter is specified in the Specification // props. setProperty ("mail. debug "," true "); // debug mode of mail sending. The parameter is the props specified in the specification. setProperty ("mail. smtp. auth "," true "); // request the server for identity authentication. The parameter is related to the specific JavaMail implementation. Session session = Session. getInstance (props); // environment configuration used for sending emails // session. setDebug (true); MimeMessage message = new MimeMessage (session); // set the mail header message. setFrom (new InternetAddress ("xxx@163.com"); message. setRecipients (Message. recipientType. TO, "xxxqq.com"); message. setSubject ("this is a complex email"); // set the body // make the text part MimeBodyPart textPart = new MimeBodyPart (); textPart. setContent ("Beauty  aaa", "text/html; charset = UTF-8 "); // picture part: MimeBodyPart imagePart = new MimeBodyPart (); imagePart. setContentID ("mm"); // Add files on the disk to the part and use the JAF framework DataHandler dh = new DataHandler (new FileDataSource ("src/0.jpg"); imagePart. setDataHandler (dh); MimeMultipart mp = new MimeMultipart (); mp. addBodyPart (textPart); mp. addBodyPart (imagePart); mp. setSubType ("related"); // MimeBodyPart textImagePart = new MimeBodyPart (); // Add MimeMultipart to MimeBodyPart to send the attachment textImagePart. setContent (mp); // create the attachment part MimeBodyPart attachmentPart = new MimeBodyPart (); dh = new DataHandler (new FileDataSource ("src/account .rar"); String filename = dh. getName (); attachmentPart. setDataHandler (dh); // manually set the file name to prevent garbled characters. Use MimeUtility in javaMail to encode the attachmentPart. setFileName (MimeUtility. encodeText (filename); // The final MimeMultipart finalMp = new MimeMultipart (); finalMp. addBodyPart (attachmentPart); finalMp. addBodyPart (textImagePart); finalMp. setSubType ("mixed"); message. setContent (finalMp); message. saveChanges (); // send the email Transport ts = session. getTransport (); ts. connect ("xxx@163.com", "123456"); // The password is the authorization code not the logon password of the mailbox ts. sendMessage (message, message. getAllRecipients (); // object, using the instance method }}

 

 

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.