javamail send mail (Simple Mail qq-mail)

/*
* <dependency >
<groupId >com.sun.mail </groupId >
<artifactId >javax.mail </artifactId >
<version >1.5.4 </version >
</dependency >
*/

/ * The above is the need to add maven dependencies * /
Package com.weiling.wl_erp.util;

import org.springframework.beans.factory.annotation.Value;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

/ **
* Author: Wang Huaipeng
* Date: 2019/9/26
* /
public class MailUtil {
// sendEmailAccount: Sender E-mail

// receiveMailAccount: the recipient's mailbox 

// sendEmailPwd: Sender E-mail password

// PS: Some mail server in order to increase the security of the password itself mailboxes, SMTP client to set up an independent code (some mailbox called "Authorization Code"),
// separate password to open the mailbox, the mailbox here this password is necessary to use a separate password (authorization code).

 // Open Mailbox -> above to find Settings  -> Accounts -> find (POP3 / IMAP / SMTP / Exchange / CardDAV / CalDAV service) -> open pop3 -> authorization code;

public static String sendEmailAccount = "[email protected]";
public static String receiveMailAccount = "[email protected]";

public static String sendEmailPwd = "E-mail Authorization Code";

// sender's mail server address

public static String emailProtocolType = "smtp";
public static String sendEmailSMTPHost = "smtp.qq.com";
public static String smtpPort = "465";
public static String sslSocketFactory = "javax.net.ssl.SSLSocketFactory";

public static String prt;

// sender mailbox SMTP server address must be accurate, different e-mail addresses on different servers, the general format is: smtp.xxx.com
// 163 mailboxes SMTP server address is: smtp.163.com

// recipient's mailbox (replace with your own knowledge of effective mailbox)


static void main public (String [] args) throws Exception {
// 1. Create parameters, parameters for connection to the mail server configuration
Properties props = new Properties (); // parameter configuration
props.setProperty ( "mail.transport. protocol ", emailProtocolType); protocol (the JavaMail Compliant) // use
props.setProperty (" mail.smtp.host ", sendEmailSMTPHost) ; SMTP server address of the sender's mailbox //
props.setProperty (" mail.smtp .auth "," true "); // need to request certification
props.put (" mail.smtp.ssl.enable "," to true ");
// ssl security certification
props.setProperty (" mail.smtp.port ", SMTPPORT);
// set SocketFactory
props.setProperty ( "mail.smtp.socketFactory.class", SSLSocketFactory);
// only process the SSL connection, for non-SSL connections without processing the
props.setProperty ( "mail.smtp.socketFactory .fallback ","false");
props.setProperty("mail.smtp.socketFactory.port", smtpPort);

// The configuration session object is created, and the mail server interaction for
the Session = Session.getInstance the session (The props);
session.setDebug (to true); // set debug mode, can view the detailed log transmission

// Create an email
MimeMessage message = createMimeMessage (session, sendEmailAccount , receiveMailAccount);

// 4. Session Gets messaging objects according to
Transport transport = session.getTransport ();

// 5. Use the email account and password to connect to the mail server, where certified mail must be consistent with the message in the mailbox of the sender, otherwise an error
//
// PS_01: determine the key to success in this way, if you connect to a server fails, the output corresponding reason for the failure of the console log,
// carefully review the reasons for the failure, some mail server returns an error code or viewing the wrong type of link, the error is given
to the appropriate mail server to help the site to view specific types of failures // the reason.
//
// PS_02: why the connection failure is usually the following points carefully check the code:
// (1) does not turn on SMTP mail service;
// (2) mailbox password is wrong, for example, some mailboxes opened independent password;
/ / (3) requires the mailbox server must use SSL secure connection;
// (4) requests too frequently or for other reasons, be denied service mail server;
// (5) to determine if the above points are correct, the mail server to find the site help.
//
// PS_03: look carefully log, take a good look log, read log, error reasons have been described in the log.
transport.connect (sendEmailAccount, sendEmailPwd);

6. // send the message, sent to all recipient addresses, message.getAllRecipients () to get all the recipients are added when you create a message object, Cc, Bcc
System.out.println (message + "====================" message.getAllRecipients + () + "======" + PRT);
transport.sendMessage (Message, message.getAllRecipients ());

7. // close the connection
transport.close ();
}

/ **
* Create a simple message contains only text
*
* @param the session and server interaction session
* @param sendMail mail sender
* @param receiveMail recipient's mailbox
* @return
* @throws Exception
* /
public static createMimeMessage the MimeMessage (the Session the session, sendMail String, String the receiveMail) throws Exception {
// 1. create a message
MimeMessage message = new MimeMessage (session) ;

// 2. From: Sender (nickname advertising suspects, to avoid being mistaken for junk mail servers as well as advertising returns a failure, modify the nickname)
message.setFrom (new new InternetAddress (sendMail, "setFrom", "UTF-8 "));

// 3. To: To (multiple recipients can be increased, CC, BCC)
// the CC: carbon copy, BCC: Bcc
message.setRecipient (MimeMessage.RecipientType.TO, new InternetAddress (receiveMail , "[email protected]", "UTF- 8"));

// 4. Subject: message subject (title advertising suspects, to avoid being mistaken for junk mail servers as well as advertising returns a failure, please change the title)
message.setSubject ( "slut you are right", "UTF-8") ;

// 5. Content: body of the message (you can use html tags) (content advertising suspects, to avoid being mistaken for junk mail servers advertising as well as failure to return, please send the modified contents)
message.setContent ( "I think yes," "text / html; charset = utf -8");

6. Set Time @ sender
message.setSentDate (new Date ());

7. // save settings
message.saveChanges ();

return message;
}
}

Guess you like

Origin www.cnblogs.com/wanghuaipeng/p/11595572.html