Preface
Please note: The test email address used by the following code is 126 mailbox, which is available in actual testing. Note that during personal testing, pay attention to the email content sent. The email server reviews the email contents and the email server is relatively strict. If you are not careful, you will be judged as spam, and then you will be returned with an error code:
Example code
import java.util.Date;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Message;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;public class SendMailUtil { private static final String USERNAME = "User name"; // Sender's user name private static final String PASSWORD = "password"; // Sender's password private static final String HOSTNAME = "smtp.126.com"; // Smtp server address private static final String FROMADDRESS = USERNAME + "@126.com"; // Sender's email private static final String FROMUSERNAME = "CNCD-code Chinese blog"; // The sender's name can be written as a private static final String EMAILTITLE = "CNCD-code Chinese blog"; // The email title private static final String EMAILCONTENT = "Welcome to use CNCD-code Chinese blog to activate email, please click the link on the right to complete the account activation:/n";// The email content public static void main(String[] args) { try { sendEmail("[email protected]", "User name: guopengfei, password: guopengfei"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * Send email* @param sendAddress The email address to be sent* @param content The content sent* @throws Exception */ public static void sendEmail(String sendAddress, String content) throws Exception { // Create a connection property. Properties props = new Properties(); // props.put("mail.smtp.host", HOSTNAME); // Set the server address of smtp is smtp.126.com props.put("mail.smtp.auth", "true"); // Set the smtp server to be authenticated. props.put("mail.transport.protocol", "stmp"); Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(USERNAME, PASSWORD); } }); Message message = new MimeMessage(session); InternetAddress from = new InternetAddress(FROMADDRESS); // Sender address from.setPersonal(MimeUtility.encodeText(FROMUSERNAME)); // Sender name message.setFrom(from); // InternetAddress to = new InternetAddress("[email protected]"); // // Recipient address// Create email: message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(sendAddress)); // Cc to multiple people's email address// message.setRecipient(Message.RecipientType.TO, to); // Only cc to your email message.setSubject(MimeUtility.encodeText(EMAILTITLE)); // Email title message.setSentDate(new Date()); MimeMultipart msgMultipart = new MimeMultipart("mixed"); // Specify as a mixed relationship message.setContent(msgMultipart); // Email content MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent( "<body><div style='width: 1000px;height: 300px;margin: 0px auto;margin-bottom:20px;border:1px solid #92B0DD;background-color: #FFFFFf;'><h3>This is an email sent automatically by the system, please do not reply!</h3><br/>"+ content+"</div></body>", "text/html;charset=UTF-8"); // TODO The order of assembly is very important. You must first assemble the text field, then assemble the file msgMultipart.addBodyPart(htmlPart); // Assemble the attachment// MimeBodyPart file = new MimeBodyPart(); // FileDataSource file_datasource = new FileDataSource( // "D://report_data2.txt"); // DataHandler dh = new DataHandler(file_datasource); // file.setDataHandler(dh); // // One feature of the attachments that distinguishes embedded content is that it has file names, which must be encoded to prevent Chinese garbled code // file.setFileName(MimeUtility.encodeText(dh.getName())); // msgMultipart.addBodyPart(file); message.saveChanges(); // The process of sending email: 95188 Transport transport = session.getTransport("smtp"); // Create a connection transport.connect(HOSTNAME, 25, USERNAME, PASSWORD); // Connect to the server// Service name, port, send mail username// (Don't @ back), password transport.sendMessage(message, message.getAllRecipients()); // Send message transport.close(); // Close System.out.println("Send Completed"); }}Note that for friends who use this code for the first time, it is recommended that you do not change the rest except for the username and password, otherwise it is easy to be judged as spam by NetEase email and not sent. Generally, when it is judged as spam, the error code returned by NetEase is as follows:
com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 126 smtp2,DMmowACXoTzMk8VXVdKnEA--.1778S2 1472566222, please see http://mail.163.com/help/help_spam_16.htm?ip=117.114.147.13&hostid=smtp2&time=1472566222 at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829) at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1634) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:889) at SendMailUtil.sendEmail(SendMailUtil.java:88) at SendMailUtil.main(SendMailUtil.java:29)
It means that you can go to the link in the error return code to view the help information. You can enter the link and find that the above 554 error code represents the meaning. This situation is usually judged as spam.
Secondly, do not run (send) the emails in the above code frequently during testing. If they are too frequent, they will easily be judged as spam. OK, the above is the entire content of this article. I hope it will be helpful to everyone's study and work.