attachmail.jsp

来自「jsp动态网站开发与实例(第3版) 源码」· JSP 代码 · 共 46 行

JSP
46
字号
	<%@page contentType="text/html"%>
	<%@page pageEncoding="ms950"%>
	<%@page import ="java.util.*,java.io.*"%>
	
	<%@page import ="javax.mail.*"%>
	<%@page import ="javax.mail.internet.*"%>
	<%@page import ="javax.activation.*"%>
	<html>
	    <head><title>JSP Page</title></head>
	    <body>
	    <% 
	        Properties theProperties = System.getProperties() ; 
	        theProperties.put("mail.host","127.0.0.1") ; 
	        theProperties.put("mail.transport.protocol","smtp") ; 
	    
	        Session theSession =
	 		 Session.getDefaultInstance(theProperties,null) ; 
	        theSession.setDebug(false) ;     
	        
	        MimeMessage theMessage = new MimeMessage(theSession) ;        
	        theMessage.setFrom
	 		(new InternetAddress("coffee@fj.com") ) ;         
	        theMessage.setRecipients
	 		(Message.RecipientType.TO,"tea@fj.com") ; 
	        
	        theMessage.setSubject("JavaMail 测试邮件") ;        
	        theMessage.setText("JSP与JavaMail测试邮件内容..." , "Ms950") ; 
	        
	        MimeBodyPart theMimeBodyPart = new MimeBodyPart() ;
	        String theFileName = "C:\\images.jpg" ; 
	        FileDataSource theFileDataSource= 
	 				new FileDataSource(theFileName) ; 
	        theMimeBodyPart.setDataHandler
	 				(new DataHandler(theFileDataSource)) ; 
	        theMimeBodyPart.setFileName
	 				(theFileDataSource.getName()) ; 
	        Multipart theMultipart = new MimeMultipart();
	        theMultipart.addBodyPart(theMimeBodyPart) ; 
	        theMessage.setContent(theMultipart) ;        
	        
	        Transport.send(theMessage) ; 
	        out.println("成功发送邮件") ;  
	   %>
	</body>
	</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?