Java sends mail through Tencent Enterprise Mailbox

Java Send mail through Tencent Enterprise Mailbox

  1. 企业邮箱 requires ssl

    @private static String account = "Enterprise Email Account"; // Login Account
    Private static String password = "Enterprise Email Password"; // Login Password
    Private static String host = "smtp.exmail.qq.com";// server address
    Private static String port = "465";// port
    Private static String protocol = "smtp";// protocol
    //Initialization parameters
    Public static Session initProperties() {
        Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol", protocol);
        properties.setProperty("mail.smtp.host", host);
        properties.setProperty("mail.smtp.port", port);
        // Use smtp authentication
        Properties.put("mail.smtp.auth", "true");
        / / Use SSL, corporate mailbox must start
        // Turn on the security protocol
        MailSSLSocketFactory mailSSLSocketFactory = null;
        Try {
            mailSSLSocketFactory = new MailSSLSocketFactory();
            mailSSLSocketFactory.setTrustAllHosts(true);
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        Properties.put("mail.smtp.enable", "true");
        Properties.put("mail.smtp.ssl.socketFactory", mailSSLSocketFactory);
        Properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        Properties.put("mail.smtp.socketFactory.fallback", "false");
        Properties.put("mail.smtp.socketFactory.port", port);
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            Protected PasswordAuthentication getPasswordAuthentication() {
                Return new PasswordAuthentication(account, password);
            }
        });
        / / Use SSL, corporate mailbox required end
        // TODO displays the debug information. The official environment is commented out.
        session.setDebug(true);
        Return session;
    }
    
    @param sender sender alias
    @param subject mail subject
    @param content Email content
    @param receiverList Receiver list, separated by "," between multiple recipients
    @param fileSrc attachment address
    Public void send(String sender, String subject, String content, String receiverList, String fileSrc) {
        Try {
            Session session = initProperties();
            MimeMessage mimeMessage = new MimeMessage(session);
            mimeMessage.setFrom(new InternetAddress(account, sender));// Sender, you can set the sender's alias
            // recipient, multi-person reception
            InternetAddress[] internetAddressTo = new InternetAddress().parse(receiverList);
            mimeMessage.setRecipients(Message.RecipientType.TO, internetAddressTo);
            // theme
            mimeMessage.setSubject(subject);
            // time
            mimeMessage.setSentDate(new Date());
            // container class attachment
            MimeMultipart mimeMultipart = new MimeMultipart();
            // can wrap text, images, attachments
            MimeBodyPart bodyPart = new MimeBodyPart();
            / / Set the content
            bodyPart.setContent(content, "text/html; charset=UTF-8");
            mimeMultipart.addBodyPart(bodyPart);
            // Add image & attachments
            bodyPart = new MimeBodyPart();
            bodyPart.attachFile(fileSrc);
            mimeMultipart.addBodyPart(bodyPart);
            mimeMessage.setContent(mimeMultipart);
            mimeMessage.saveChanges();
            Transport.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }