一直在用javax.mail发邮件,但是我的邮件显示的都是邮件地址,像其它服务邮箱,京东,淘宝之类的都显示的是名称。很奇怪。
百度之,网上没有类似。
百度之好久,发现有人说是需要用固定的格式填充From属性。
// 设置收件人,寄件人
String nick = javax.mail.internet.MimeUtility.encodeText("显示名称");
messageHelper.setFrom(new InternetAddress(nick + " <service@163.com>"));
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("from@163.com");
mailInfo.setPassword("password");// 您的邮箱密码
String nick = "";
try {
nick = javax.mail.internet.MimeUtility.encodeText("CSDN服务账号");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
mailInfo.setFromAddress(nick + "<from@163.com>");
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("to@qq.com",content);
}
}