java send mail, set body style, configure sender information

java send mail, set body style, configure sender information

Note: javamail believes that it will send the mail only if it matches the format of the mailbox. As for whether the other party can receive it, it doesn’t care.

The complete code is attached

import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
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;

/**
 * Title: MailSend
 *
 * @author 清城
 */
public class MailSendQc {

    //mian方法  可以直接用于测试
    public static void main(String[] args) throws Exception {

        String sendZw = "尊敬的老王:<br>您好!感谢您信赖*****有限公司,您的*************我们将尽快给您处理,会尽快给您答复。给您带来的不便,请您谅解,。如有任何疑问,可致电189****0557。<br>地址:******大厦24楼2403";
        sendEmail("邮件标题: 业务正在处理", sendZw,"95****[email protected]");
        //sendEmail("邮件标题: 业务正在处理", sendZw,"26****[email protected]");
    }

    /**
     * 发送文本邮件
     *
     * @param mailTitle   邮件标题
     * @param sendContent 邮件内容
     * @Description:
     * @author
     * @date 2019年9月25日
     */
    public static void sendEmail(String mailTitle, String sendContent, String tomail) {
        /**
         * 参数配置--装信封写地址
         */
        String EmailAccount = "tj*****[email protected]";// 发件人的邮箱
        String EmailPassword = "DWY********DZX";//客户端授权码***
        String EmailSMTPHost = "smtp.163.com";// 邮件SMTP协议
        String ReceiveMailAccount = tomail;//收件人邮箱

        Properties prop = new Properties();// 配置参数类
        prop.setProperty("mail.transport.protocol", "smtp");// 参数一:使用电子邮件协议smtp
        prop.setProperty("mail.smtp.host", EmailSMTPHost); // 参数二:协议所在服务器
        prop.setProperty("mail.smtp.auth", "true");// 参数三:需要请求认证 不然553报错
        /**
         * 根据配置创建会话对象, 用于和邮件服务器交互---检查地址是否能对打个电话问问
         */
        final String smtpPort = "465";
        prop.setProperty("mail.smtp.port", smtpPort);
        prop.setProperty("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        prop.setProperty("mail.smtp.socketFactory.fallback", "false");
        prop.setProperty("mail.smtp.socketFactory.port", smtpPort);
        Session session = null;
        Transport transport = null;
        try {
            session = Session.getDefaultInstance(prop);// 将参数与会话结合
            // session.setDebug(true);// 这个是开启debug 方便查看
            /**
             * 邮件封装
             */
            MimeMessage message = createMimeMessage(session, EmailAccount,
                    ReceiveMailAccount, sendContent, mailTitle);
            /**
             * 邮递员
             */
            System.out.println("发件人邮箱:" + EmailAccount);
            System.out.println("发件人的密码:" + EmailPassword);

            transport = session.getTransport();
            transport.connect(EmailAccount, EmailPassword);// 敲门开门
            transport.sendMessage(message, message.getAllRecipients());// 送到对方手里
            System.err.println("发送完毕");
        } catch (NoSuchProviderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (transport != null) {
                try {
                    transport.close();
                } catch (MessagingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public static MimeMessage createMimeMessage(Session session,
                                                String SendAccount, String ReceiveAccount, String sendContent, String mailTitle) throws Exception {
        MimeMessage message = new MimeMessage(session);// 创建邮件写信
        message.setFrom(new InternetAddress(SendAccount, "清城测试", "utf-8"));// 写发件人
        message.setRecipient(MimeMessage.RecipientType.TO,
                new InternetAddress(ReceiveAccount));
        message.setSubject(mailTitle, "UTF-8");//标题
        message.setContent(sendContent, "text/html;charset=UTF-8");//内容
        message.setSentDate(new Date());
        message.saveChanges();
        return message;
    }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49462255/article/details/109332664