发送邮件 设置邮件昵称

来源:互联网 发布:淘宝网店手机实名认证 编辑:程序博客网 时间:2024/05/16 22:40
[java] view plaincopy
  1. public class TextMessage {  
  2.     //发送信件邮箱的用户名及密码  
  3.     static String username="272138576";  
  4.     static String password="********";  
  5.     public static void main(String [] args)throws Exception{  
  6.         String from="272138576@qq.com";  
  7.         String to="zousy999@qq.com";  
  8.         String subject="test";  
  9.         String body="test!!!";  
  10.         Properties props = System.getProperties();  
  11.         // 创建信件服务器  
  12.         props.put("mail.smtp.host""smtp.qq.com");  
  13.         props.put("mail.smtp.auth""true");  
  14.         props.put("mail.transport.protocol""smtp");  
  15.         // 得到默认的对话对象  
  16.         Authenticator a = new Authenticator() {  
  17.             public PasswordAuthentication getPasswordAuthentication() {  
  18.                 return new PasswordAuthentication(username, password);  
  19.             }  
  20.         };  
  21.         //创建Session实例  
  22.         Session session = Session.getDefaultInstance(props, a);  
  23.         //创建MimeMessage实例对象  
  24.         MimeMessage msg=new MimeMessage(session);  
  25.         //设置发信人  
  26. //      msg.setFrom(new InternetAddress(from));  
  27.         //设置自定义发件人昵称  
  28.         String nick="";  
  29.         try {  
  30.             nick=javax.mail.internet.MimeUtility.encodeText("我的昵称");  
  31.         } catch (UnsupportedEncodingException e) {  
  32.             e.printStackTrace();  
  33.         }   
  34.         msg.setFrom(new InternetAddress(nick+" <"+from+">"));  
  35.         //设置收信人  
  36.         msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));  
  37.         //设置发送日期  
  38.         msg.setSentDate(new Date());  
  39.         //设置邮件主题  
  40.         msg.setSubject(subject);  
  41.         //设置邮件正文  
  42.         msg.setText(body);  
  43.         Transport.send(msg);  
  44.     }  
  45. }  
0 0
原创粉丝点击