java-腾讯企业邮箱发送邮件

package com.jfj.platform.utils;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.MimeMessage;


public class MailUtil {

    private static  String  username="账号";   //"此处是邮箱地址"

    private static  String  password="密码";  //""

    private static  String  phone="";

    private static JavaMailSenderImpl mailSender = createMailSender();

    private static JavaMailSenderImpl createMailSender() {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost("smtp.exmail.qq.com");
        sender.setPort(25);
        sender.setUsername(username);
        sender.setPassword(password);
        sender.setDefaultEncoding("Utf-8");
        Properties p = new Properties();
        p.setProperty("mail.smtp.timeout", "600000");
        p.setProperty("mail.smtp.auth", "false");
        sender.setJavaMailProperties(p);
        return sender;
    }



    /**发送html
     *
     * @param to
     * @param html
     * @throws MessagingException
     * @throws UnsupportedEncodingException
     */
    public static   void sendMail(String to, String html) throws MessagingException,UnsupportedEncodingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 设置utf-8或GBK编码,否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom(username, "****平台");
        messageHelper.setTo(to);
        messageHelper.setSubject("注册回执");
        messageHelper.setText(html, true);
//      messageHelper.addAttachment("", new File(""));//附件
        mailSender.send(mimeMessage);
    }



    public static boolean emailSend(String name,String companyName,String companyPhone,String password,String companyKey,String email){
        String content="\n" +
                "\n" +
                "\n" +
                "    \n" +
                "    \n" +
                "    \n" +
                "    Document\n" +
                "    \n" +
                "\n" +
                "\n" +
                "    
\n" + "

注册回执

\n" + "
\n" + "

尊敬的:"+name+"

\n" + "

很荣幸通知您,您所在的企业:“"+companyName+"装饰公司”,已经通过****平台审核,希望在接下来的服务过程中,能让您满意,如果在使用过程中有任何问题,请拨打:"+phone+",将有专属的客服与您联系,并耐心解答您的疑问。

\n" + "

请妥善保管好您企业的登录地址和账号,不要外传或泄露。

\n" + "

账号:"+companyPhone+"
\n" + " 密码:"+password+"
\n" + " 网址:http://"+companyKey+".company.deliverhome.cn

\n" + "
\n" + "
\n" + "

\n" + " \n" + " 关注*****公众号,了解更多产品咨询及优惠活动\n" + "

\n" + "
\n" + "
\n" + "\n" + "\n" + ""; try { sendMail(email,content); } catch (MessagingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return true; } public static void main(String[] args) { emailSend("小孟","花儿","18866668888","123456789","wahaha","[email protected]"); } }

 

java-腾讯企业邮箱发送邮件_第1张图片

因为是带图片的,所有被屏蔽到垃圾邮箱了。

 

部署到阿里服务器才发现有问题(服务调用超时)阿里端口25是关闭的。只有大公司才能申请开启。换另外一种方法

package com.jfj.platform.utils;


import com.sun.mail.util.MailSSLSocketFactory;
import com.yxpf.common.core.utils.YxpfUtils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;


public class EmailSendUtil {

    private static  String  phone="010-0000-0000";
    private static String account = "";// 登录账户
    private static String password = "";// 登录密码
    private static String host = "smtp.exmail.qq.com";// 服务器地址
    private static String port = "465";// 端口
    private static String protocol = "smtp";// 协议
    //初始化参数
    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);
        // 使用smtp身份验证
        properties.put("mail.smtp.auth", "true");
        // 使用SSL,企业邮箱必需 start
        // 开启安全协议
        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);
            }
        });
        // 使用SSL,企业邮箱必需 end
        // TODO 显示debug信息 正式环境注释掉
        session.setDebug(true);
        return session;
    }
    // @param sender 发件人别名
    // @param subject 邮件主题
    //@param content 邮件内容
    //@param receiverList 接收者列表,多个接收者之间用","隔开
    //@param fileSrc 附件地址
    public static 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));// 发件人,可以设置发件人的别名
            // 收件人,多人接收
            InternetAddress[] internetAddressTo = new InternetAddress().parse(receiverList);
            mimeMessage.setRecipients(Message.RecipientType.TO, internetAddressTo);
            // 主题
            mimeMessage.setSubject(subject);
            // 时间
            mimeMessage.setSentDate(new Date());
            // 容器类 附件
            MimeMultipart mimeMultipart = new MimeMultipart();
            // 可以包装文本,图片,附件
            MimeBodyPart bodyPart = new MimeBodyPart();
            // 设置内容
            bodyPart.setContent(content, "text/html; charset=UTF-8");
            mimeMultipart.addBodyPart(bodyPart);
            if(YxpfUtils.isNotEmpty(fileSrc)){
               // 添加图片&附件
                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();
        }
    }


    public static boolean emailSend(String name,String companyName,String companyPhone,String password,String companyKey,String email){
        String content="\n" +
                "\n" +
                "\n" +
                "    \n" +
                "    \n" +
                "    \n" +
                "    Document\n" +
                "    \n" +
                "\n" +
                "\n" +
                "    
\n" + "

注册回执

\n" + "
\n" + "

尊敬的:"+name+"

\n" + "

很荣幸通知您,您所在的企业:“"+companyName+"装饰公司”,已经通过****平台审核,希望在接下来的服务过程中,能让您满意,如果在使用过程中有任何问题,请拨打:"+phone+",将有专属的客服与您联系,并耐心解答您的疑问。

\n" + "

请妥善保管好您企业的登录地址和账号,不要外传或泄露。

\n" + "

账号:"+companyPhone+"
\n" + " 密码:"+password+"
\n" + " 网址:http://"+companyKey+".*********.cn

\n" + "
\n" + "
\n" + "

\n" + " \n" + " 关注****公众号,了解更多产品咨询及优惠活动\n" + "

\n" + "
\n" + "
\n" + "\n" + "\n" + ""; send("***平台","注册回执",content,email,""); return true; } }

发一版阿里就OK了

你可能感兴趣的:(java)