关闭

java发送email

标签: emailjavastringinputhtmltable
817人阅读 评论(0) 收藏 举报
分类:

MailForm.html

< html >
< body >
  
< form  action ="sendMail.jsp"  method ="post" >
    
< table  cellspacing ="2"  cellpadding ="2"  border ="1" >
      
< tr >
        
< td > To: </ td >
        
< td >
          
< input  type ="text"  name ="to"  size ="30"  maxlength ="30" >
        
</ td >
      
</ tr >
      
< tr >
        
< td > From: </ td >
        
< td >
          
< input  type ="text"  name ="from"  size ="30"  maxlength ="30" >
        
</ td >
      
</ tr >
      
< tr >
        
< td > Subject </ td >
        
< td >
          
< input  type ="text"  name ="subject"  size ="30"  maxlength ="30" >
        
</ td >
      
</ tr >
      
< tr >
        
< td  colspan ="2" >
          
< textarea  cols ="40"  rows ="10"  name ="body" ></ textarea >
        
</ td >
      
</ tr >
      
< tr >
        
< td >
          
< input  type ="submit"  name ="submit"  value ="Submit" >
          
< input  type ="Reset" >
        
</ td >
      
</ tr >
    
</ table >
  
</ form >
</ body >
</ html >


sendMail.jsp

<html>
  
<head>
    
<title>JSP JavaMail Example </title>
  
</head>

<body>

<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>

<%
    
String host = "yourmailhost";
    
String to = request.getParameter("to");
    
String from = request.getParameter("from");
    
String subject = request.getParameter("subject");
    
String messageText = request.getParameter("body");
    
boolean sessionDebug = false;

    Properties props 
= System.getProperties();
    props.put(
"mail.host", host);
    props.put(
"mail.transport.protocol""smtp");

    Session mailSession 
= Session.getDefaultInstance(props, null);

    mailSession.setDebug(sessionDebug);

    Message msg 
= new MimeMessage(mailSession);

    msg.setFrom(
new InternetAddress(from));
    InternetAddress[] address 
= {new InternetAddress(to)};
    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSubject(subject);
    msg.setSentDate(
new Date());
    msg.setText(messageText);

    Transport.send(msg);
    
    out.println(
"Mail was sent to " + to);
    out.println(
" from " + from);
    out.println(
" using host " + host + ".");

%>
    
</table>
  
</body>
</html>
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:62682次
    • 积分:914
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:11篇
    • 译文:1篇
    • 评论:6条
    文章分类
    最新评论