javaMail发送邮件设置发件人中文昵称

public class TextMessage {  
    //发送信件邮箱的用户名及密码  
    static String username="272138576";  
    static String password="********";  
    public static void main(String [] args)throws Exception{  
        String from="1164764881@qq.com";  
        String to="lfsenior@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);  
    }  
}  

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值