javaMail发送邮件设置发件人中文昵称
public class TextMessage {//发送信件邮箱的用户名及密码static String username="272138576";static String password="********";public static void main(String [] args)throws Exception{String from="272138576@qq.com";String to="zousy999@qq.com";String subject="test";String body="test!!!";Properties props = System.getProperties();// 创建信件服务器props.put("mail.smtp.host", "smtp.qq.com");props.put("mail.smtp.auth", "true");props.put("mail.transport.protocol", "smtp");// 得到默认的对话对象Authenticator a = new Authenticator() {public PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}};//创建Session实例Session session = Session.getDefaultInstance(props, a);//创建MimeMessage实例对象MimeMessage msg=new MimeMessage(session);//设置发信人//msg.setFrom(new InternetAddress(from));//设置自定义发件人昵称String nick="";try {nick=javax.mail.internet.MimeUtility.encodeText("我的昵称");} catch (UnsupportedEncodingException e) {e.printStackTrace();} msg.setFrom(new InternetAddress(nick+" <"+from+">"));//设置收信人msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));//设置发送日期msg.setSentDate(new Date());//设置邮件主题msg.setSubject(subject);//设置邮件正文msg.setText(body);Transport.send(msg);}}?接收邮件效果图:
?