JavaMail——进阶

来源:互联网 发布:1个域名指向多个ip 编辑:程序博客网 时间:2024/05/29 15:44

这里使用Transport静态方法发送邮件,不可以在发送时临时设置发送人的用户名密码,这要移到 Session.getInstance 参数中。同时smtp服务器的地址也没有设置,在props中设置。

public static void main(String[] args) throws Exception {Properties props=new Properties();props.setProperty("mail.smtp.auth", "true");//表明会通过用户名密码验证props.setProperty("mail.transport.protocol", "smtp");//设置传输协议。props.setProperty("mail.host", "smtp.aliyun.com");  //设置发送smtp服务器的地址Session  session=Session.getInstance(props,new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("wanhao6@aliyun.com", "password");}});//每次都返回一个新的对象//Session.getDefaultInstance()每次都返回相同的对象,不推荐,或许发邮件和收邮件会有影响Message msg=new MimeMessage(session);msg.setFrom(new InternetAddress("wanhao6@aliyun.com"));//设置发送人msg.setSubject("中文主题");//设置主题msg.setContent("<a href=\"http://www.baidu.com\">点我</a>","text/html;charset=utf-8");  msg.setRecipients(RecipientType.TO, InternetAddress.parse("wanhao6@aliyun.com,2235623837@qq.com"));//这样写会使每个受件者看到所有收件人Transport.send(msg);}


由于我们在文件内容中可能有特殊的数据,我们可以把数据进行编码。

常用编码方式  base64  quote-printable  

若是内容大部分为中文,使用base64编码,若内容大部分为英文,则使用 quote-printable  .


可以给邮件中添加附件,在邮件内容中添加图片,下面代码把邮件另存在桌面上

public static void main(String[] args) throws Exception {Session session=Session.getInstance(new Properties());MimeMessage msg=new MimeMessage(session);Multipart multipart=new MimeMultipart("mixed");msg.setContent(multipart);msg.setSubject("挑战书");msg.setFrom(new InternetAddress("\""+MimeUtility.encodeText("万豪")+"\""+" <wanhao6@aliyun.com>"));msg.setReplyTo(new Address[]{new InternetAddress("<2235623837@qq.com>")});//显示友好名称,编码是为了使汉字正确显示msg.setRecipient(RecipientType.TO, new InternetAddress(MimeUtility.encodeText("张三   <2235623837@qq.com>")));MimeBodyPart  content=new MimeBodyPart();MimeBodyPart  attch1=new MimeBodyPart();MimeBodyPart  attch2=new MimeBodyPart();multipart.addBodyPart(attch2);multipart.addBodyPart(attch1);multipart.addBodyPart(content);DataSource ds=new FileDataSource("C:\\Users\\Administrator\\Desktop\\第一个附件.txt");DataHandler dataHandler1=new DataHandler(ds);attch1.setDataHandler(dataHandler1);attch1.setFileName(MimeUtility.encodeText("附件1.txt"));DataSource ds2=new FileDataSource("C:\\Users\\Administrator\\Pictures\\a.jpg");DataHandler dataHandler2=new DataHandler(ds2);attch2.setDataHandler(dataHandler2);attch2.setFileName("a.jpg");Multipart bodymp=new MimeMultipart("related");content.setContent(bodymp);MimeBodyPart htmlPart=new MimeBodyPart();MimeBodyPart gifPart=new MimeBodyPart();bodymp.addBodyPart(gifPart);bodymp.addBodyPart(htmlPart);DataSource gifds=new FileDataSource("resource\\logo.gif");DataHandler gifdh=new DataHandler(gifds);gifPart.setDataHandler(gifdh);gifPart.setHeader("Content-Location", "http://www.ecfun.cn/images/user-img.png");//①htmlPart.setContent("<img src='http://www.ecfun.cn/images/user-img.png'>你真的很厉害吗?大家都这样说,我想和你比试一下","text/html;charset=utf-8");msg.saveChanges();OutputStream os=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\1.eml");msg.writeTo(os);os.close();}


在①中一般要把 gifPart 中的链接对应的图片,与FileDataSource();中的图片保持一致。因为保存在本地显示的是DataSource中关联的文件对应的图片,而发送过去的邮件中却显示链接对应的图片。

接下来便可以把邮件发送给某个用户了。

public static void main(String[] args) throws Exception {Properties props=new Properties();props.setProperty("mail.smtp.auth", "true");//表明会通过用户名密码验证props.setProperty("mail.transport.protocol", "smtp");//设置传输协议。props.setProperty("mail.host", "smtp.aliyun.com");  //设置发送smtp服务器的地址Session  session=Session.getInstance(props,new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("username", "password");}});//每次都返回一个新的对象//Session.getDefaultInstance()每次都返回相同的对象,不推荐,或许发邮件和收邮件会有影响FileInputStream fis=new FileInputStream("C:\\Users\\Administrator\\Desktop\\1.eml");Message msg=new MimeMessage(session,fis);//收件地址要与 原来文件中msg.setRecipient中地址一致Transport.send(msg,new Address[]{new InternetAddress("2235623837@qq.com")});}




0 0
原创粉丝点击