Java can be used to send mail by copying sample code

You need to import javax.mail.jar    

Reference content https://www.cnblogs.com/ysocean/p/7666061.html


import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
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.MimeUtility;


{class Testmail public
public static void main (String [] args) throws Exception {
// e-mail recipient can add several recipients are [email protected] [email protected]
String receiveMailAccount = "shoujianren @ 163 .com ";
// String receiveMailAccountTwo =" [email protected] ";   

// sender's e-mail
String from = "[email protected]";

// specify the host to send mail to smtp.aliyun.com
String Host = "smtp.163.com"; // QQ mail server

// Get system property
the properties System.getProperties = properties ();

// set mail server
properties.setProperty ( "the mail.smtp.host", Host);

properties.put ( "mail.smtp.auth", "to true");
// Gets the default session object
the session session = Session.getDefaultInstance (the Properties, new new Authenticator () {
public PasswordAuthentication the getPasswordAuthentication ()
{
return new new PasswordAuthentication ( "[email protected]", "123456"); // sender's e-mail user name, here the sender's password is password authorization code after opening the mailbox the SMTP
}
});

the try {
// create default objects MimeMessage
= New new Message the MimeMessage the MimeMessage (the session);

// the Set the From: header header field
message.setFrom (the InternetAddress new new (from));

// the Set the To: header field header
// To: To (can add multiple To, Cc, Bcc)
message.addRecipient (Message.RecipientType.TO, the InternetAddress new new (receiveMailAccount));
// second recipient
//message.addRecipient(Message.RecipientType.TO,new InternetAddress ( receiveMailAccountTwo));

// Set Subject: header header field
//message.setSubject ( "Chengdu");
// set the message subject
message.setSubject ( "e-mail messages (including attachments)", "UTF-8");

the MimeBodyPart Image = new new the MimeBodyPart ();
// read local files
the DataHandler new new DH = the DataHandler (the FileDataSource new new ( "D: \\ aaa.jpg"));
// add to the image data is "node"
image.setDataHandler (DH);
// is "nodes" to set a unique number (in the text "node" will refer to the ID)
image.setContentID ( "mailTestPic");

// create text "node"
the MimeBodyPart the MimeBodyPart new new text = ();
// add picture here is the way the message content included in the entire picture, the image actually may be added in the form of a network link http
text.setContent ( "this is a picture <br/> <a href = 'http: //www.cnblogs.com/ ysocean / p / 7666061.html '> < img src =' cid: mailTestPic '/> </a> ", "Text / HTML; charset = UTF-. 8");

// (picture + text) and the relationship between the text image "nodes" (text and pictures "node" a synthetic hybrid "node")
= New new MimeMultipart mm_text_image MimeMultipart ();
mm_text_image.addBodyPart (text);
mm_text_image.addBodyPart (Image);
mm_text_image.setSubType ( "Related"); // association

// mixed picture + text "nodes" into a common package "nodes"
// eventually added to the Content Multipart messages are composed of a plurality of BodyPart, so what we need is BodyPart,
// above mailTestPic not BodyPart, all put into one package mm_text_image BodyPart
the MimeBodyPart text_image the MimeBodyPart new new = () ;
text_image.setContent (mm_text_image);

the MimeBodyPart the MimeBodyPart attachment new new = ();
// read local files
the DataHandler new new DH2 = the DataHandler (the FileDataSource new new ( "D: \\ aa.doc"));
// add to the data attachment "nodes"
attachment.setDataHandler (dh2);
// set the attachment file name (required coding)
attachment.setFileName (MimeUtility.encodeText (dh2.getName ()));

// Set the relationship (picture + text) and attachments (synthesis of a large mixing "node" / Multipart)
MimeMultipart new new MimeMultipart mm = ();
mm. addBodyPart (text_image);
mm.addBodyPart (attachment); // If there are multiple attachments, you can create multiple times to add
mm.setSubType ( "mixed");

// set the entire message of the relationship (the final mix "node "added to the content of the message as a message object)
message.setContent (mm);
// send a message
the Transport.send (message);
System.out.println (" message sent successfully from w3cschool.cc .... ");
} the catch (MessagingException MEX) {
mex.printStackTrace ();
}
}
}

Guess you like

Origin www.cnblogs.com/duoyan/p/11490043.html