欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java发邮件并显示昵称,java发邮件昵称,import java.

来源: javaer 分享于  点击 3325 次 点评:225

java发邮件并显示昵称,java发邮件昵称,import java.


import java.io.UnsupportedEncodingException;import java.util.Date;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;    public class TextMessage {          //发送信件邮箱的用户名及密码          static String username="lileiqx@163.com";          static String password="***************";          public static void main(String [] args)throws Exception{              String from="lileiqx@163.com";              String to="lileiqx@163.com";              String subject="test";              String body="test!!!";              Properties props = System.getProperties();              // 创建信件服务器              props.put("mail.smtp.host", "smtp.163.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 (from,nick));              //设置收信人              msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));              //设置发送日期              msg.setSentDate(new Date());              //设置邮件主题              msg.setSubject(subject);              //设置邮件正文              msg.setText(body);              Transport.send(msg);          }      }//该片段来自于http://byrx.net
相关栏目:

用户点评