記錄一個格式:發郵件時顯示郵件聯繫人名稱(暱稱)

一直在用javax.mail發郵件,但是我的郵件顯示的都是郵件地址,像其它服務郵箱,京東,淘寶之類的都顯示的是名稱。很奇怪。

百度之,網上沒有類似。

百度之好久,發現有人說是需要用固定的格式填充From屬性。

// 設置收件人,寄件人
String nick = javax.mail.internet.MimeUtility.encodeText("顯示名稱");
messageHelper.setFrom(new InternetAddress(nick + " <[email protected]>"));
messageHelper.setTo(toMail);
messageHelper.setSubject(subject);

這樣即可,前面的名稱就會正常顯示出來。

附發郵件的代碼:

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.5.0-b01</version>
        </dependency>

這是以前寫的代碼,源碼已經找不到的,只有jar還在,現反編譯出來,保存下。

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package myEmail;

import java.util.Properties;

public class MailSenderInfo {
    private String mailServerHost;
    private String mailServerPort = "25";
    private String fromAddress;
    private String toAddress;
    private String userName;
    private String password;
    private boolean validate = false;
    private String subject;
    private String content;
    private String[] attachFileNames;

    public MailSenderInfo() {
    }

    public Properties getProperties() {
        Properties p = new Properties();
        p.put("mail.smtp.host", this.mailServerHost);
        p.put("mail.smtp.port", this.mailServerPort);
        p.put("mail.smtp.auth", this.validate?"true":"false");
        return p;
    }

    public String getMailServerHost() {
        return this.mailServerHost;
    }

    public void setMailServerHost(String mailServerHost) {
        this.mailServerHost = mailServerHost;
    }

    public String getMailServerPort() {
        return this.mailServerPort;
    }

    public void setMailServerPort(String mailServerPort) {
        this.mailServerPort = mailServerPort;
    }

    public boolean isValidate() {
        return this.validate;
    }

    public void setValidate(boolean validate) {
        this.validate = validate;
    }

    public String[] getAttachFileNames() {
        return this.attachFileNames;
    }

    public void setAttachFileNames(String[] fileNames) {
        this.attachFileNames = fileNames;
    }

    public String getFromAddress() {
        return this.fromAddress;
    }

    public void setFromAddress(String fromAddress) {
        this.fromAddress = fromAddress;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getToAddress() {
        return this.toAddress;
    }

    public void setToAddress(String toAddress) {
        this.toAddress = toAddress;
    }

    public String getUserName() {
        return this.userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getSubject() {
        return this.subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContent() {
        return this.content;
    }

    public void setContent(String textContent) {
        this.content = textContent;
    }
}


//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package myEmail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class MyAuthenticator extends Authenticator {
    private String username;
    private String password;

    public MyAuthenticator(String username, String password) {
        this.username = username;
        this.password = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(this.username, this.password);
    }

    String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}



//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package myEmail;

import java.util.Date;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import myEmail.MailSenderInfo;
import myEmail.MyAuthenticator;

public class SimpleMailSender {
    public SimpleMailSender() {
    }

    public boolean sendTextMail(MailSenderInfo mailInfo) {
        MyAuthenticator authenticator = null;
        Properties pro = mailInfo.getProperties();
        if(mailInfo.isValidate()) {
            authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
        }

        Session sendMailSession = Session.getDefaultInstance(pro, authenticator);

        try {
            MimeMessage ex = new MimeMessage(sendMailSession);
            InternetAddress from = new InternetAddress(mailInfo.getFromAddress());
            ex.setFrom(from);
            InternetAddress to = new InternetAddress(mailInfo.getToAddress());
            ex.setRecipient(RecipientType.TO, to);
            ex.setSubject(mailInfo.getSubject());
            ex.setSentDate(new Date());
            String mailContent = mailInfo.getContent();
            ex.setText(mailContent);
            Transport.send(ex);
            return true;
        } catch (MessagingException var9) {
            var9.printStackTrace();
            return false;
        }
    }

    public boolean sendHtmlMail(MailSenderInfo mailInfo) {
        MyAuthenticator authenticator = null;
        Properties pro = mailInfo.getProperties();
        if(mailInfo.isValidate()) {
            authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
        }

        Session sendMailSession = Session.getDefaultInstance(pro, authenticator);

        try {
            MimeMessage ex = new MimeMessage(sendMailSession);
            InternetAddress from = new InternetAddress(mailInfo.getFromAddress());
            ex.setFrom(from);
            InternetAddress to = new InternetAddress(mailInfo.getToAddress());
            ex.setRecipient(RecipientType.TO, to);
            ex.setSubject(mailInfo.getSubject());
            ex.setSentDate(new Date());
            MimeMultipart mainPart = new MimeMultipart();
            MimeBodyPart html = new MimeBodyPart();
            html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
            mainPart.addBodyPart(html);
            ex.setContent(mainPart);
            Transport.send(ex);
            return true;
        } catch (MessagingException var10) {
            var10.printStackTrace();
            return false;
        }
    }
}


package mail;

import myEmail.MailSenderInfo;
import myEmail.SimpleMailSender;

import java.io.UnsupportedEncodingException;

public class SendEmail {
    public static void send(String email, String content) {
        // 這個類主要是設置郵件
        MailSenderInfo mailInfo = new MailSenderInfo();
        mailInfo.setMailServerHost("smtp.163.com");
        mailInfo.setMailServerPort("25");
        mailInfo.setValidate(true);
        mailInfo.setUserName("[email protected]");
        mailInfo.setPassword("password");// 您的郵箱密碼
        String nick = "";
        try {
            nick = javax.mail.internet.MimeUtility.encodeText("CSDN服務賬號");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        mailInfo.setFromAddress(nick + "<[email protected]>");
        mailInfo.setToAddress(email);
        mailInfo.setSubject("計算結果");
        mailInfo.setContent(content + "\n此郵件爲系統自動發送,回覆無效!");
        // 這個類主要來發送郵件
        SimpleMailSender sms = new SimpleMailSender();
        sms.sendTextMail(mailInfo);// 發送文體格式
        // sms.sendHtmlMail(mailInfo);// 發送html格式
    }
    public static void main(String[] args){
        String content="我是一封測試郵件";
        send("[email protected]",content);
    }
}
發佈了91 篇原創文章 · 獲贊 73 · 訪問量 68萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章