JavaMail——进阶

这里使用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() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("[email protected]", "password");
			}
		}					
		);//每次都返回一个新的对象
		//	Session.getDefaultInstance()每次都返回相同的对象,不推荐,或许发邮件和收邮件会有影响
		
		Message msg=new MimeMessage(session);
		msg.setFrom(new InternetAddress("[email protected]"));//设置发送人
		msg.setSubject("中文主题");			//设置主题
		msg.setContent("<a href=\"http://www.baidu.com\">点我</a>","text/html;charset=utf-8");  
		msg.setRecipients(RecipientType.TO, InternetAddress.parse("[email protected],[email protected]"));//这样写会使每个受件者看到所有收件人
		
		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("万豪")+"\""+" <[email protected]>"));
		msg.setReplyTo(new Address[]{new InternetAddress("<[email protected]>")});
		//显示友好名称,编码是为了使汉字正确显示
		msg.setRecipient(RecipientType.TO, new InternetAddress(MimeUtility.encodeText("张三   <[email protected]>")));
		
		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() {
			@Override
			protected 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("[email protected]")});
	}




发布了334 篇原创文章 · 获赞 37 · 访问量 30万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章