package com.get.one; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.* ; import java.util.* ; import javax.activation.* ; public class test7 { // Sender's email and password (replace with your own email and password) public static String myEmailAccount = "***@qq.com " ; public static String myEmailPassword = "******" ; public static class MailUtils { private String host = ""; // smtp server private String from = ""; // sender address private String to = ""; // recipient address private String affix = ""; // attachment address private String affixName = ""; // Attachment name private String user = ""; // User name private String pwd = ""; // Password private String subject = ""; // Mail header private String msg = ""; // content of email public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public void setAddress(String from, String to, String subject) { this.from = from; this.to = to; this.subject = subject; } public void setAffix(String affix, String affixName) { this.affix = affix; this.affixName = affixName; } public void send(String host, String user, String pwd) { this.host = host; this.user = user; this.pwd = pwd; Properties props = new Properties(); // Set the properties of the mail server that sends emails (using Netease's smtp server here) props.put("mail.smtp.host" , host); // Requires authorization, that is, the verification of username and password, so that To pass the verification (must have this one) props.put("mail.smtp.auth", "true"); // Requires authentication props.put("mail.smtp.port", "465" ); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put( "mail.smtp.socketFactory.fallback", "false" ); // Build a session with the props object just set Session session = Session.getDefaultInstance(props); // With this sentence, the process information can be displayed at the console during the process of sending mail for debugging. // Use (you can see the process of sending mail on the console (console)) session.setDebug( true ) ; // Define message object with session as parameter MimeMessage message = new MimeMessage(session); try { // Load sender address message.setFrom( new InternetAddress(from)); // Load recipient address message.addRecipient(Message .RecipientType.TO, new InternetAddress(to)); // load title message.setSubject(subject); // Add various parts of the mail to the multipart object, including text content and attachments Multipart multipart = new MimeMultipart(); // Set the text content of the email BodyPart contentPart = new MimeBodyPart(); contentPart.setText(msg); multipart.addBodyPart(contentPart); if(!affix.equals("")) { // 添加附件 BodyPart messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(affix); // Add the content of the attachment messageBodyPart.setDataHandler( new DataHandler(source)); // Add the title of the attachment // This is very important, through the following Base64 encoding conversion, you can guarantee your Chinese Attachment header name will not become garbled when sending sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); messageBodyPart.setFileName("=?GBK?B?" + enc.encode(affixName.getBytes()) + "?="); multipart.addBodyPart(messageBodyPart); }; // Put the multipart object into the message message.setContent(multipart); // Save the mail message.saveChanges(); // Send the mail Transport transport = session.getTransport("smtp" ); // Connect to the server's mailbox transport. connect(host, user, pwd); // Send the mail out transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (Exception e) { e.printStackTrace (); } } public static void main(String[] args) { MailUtils cn = new MailUtils(); // Set sender address, recipient address and email header cn.setAddress("***@qq.com", "***@qq.com", "A with Attachment's JavaMail mail" ); // Set the text content to be sent cn.setMsg("This is the content" ); // Set the location and title of the attachment to be sent // cn.setAffix("C:/kms10.log ", "kms10.log"); /** * Set the account and password of the smtp server and mailbox * Mailbox must enable POP3/SMTP service and IMAP/SMTP service * Because the program belongs to third-party login, the login password must use the authorization code */ // Note: [The authorization code is different from your usual login password] cn.send("smtp.qq.com" , myEmailAccount, myEmailPassword); } } }