JavaMail学习笔记-1(简单纯文本的邮件发送)

 
简单的纯文本邮件发送:
1.本人安装了一个CMailServer5.3来测试邮件的收发。
2.下载接下来需要用到的的jar包,有如下几个:
3.启动Eclipse,建立一个标准的web项目,并在web-info/lib下引入jar包。
4.编写一个邮件输入页面:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
    
< head >
        
< title > JavaMail1.html </ title >
    
</ head >

    
< body >
        
< form action ="SEND1" method ="post" >
             From:
< input type ="text" name ="from" >< br >
             TO:
< input type ="text" name ="to" >< br >
             Subject:
< input type ="text" name ="subject" >< br >
             Context:
< textarea rows ="4" cols ="30" name ="context" ></ textarea >< br >
            
< input type ="submit" value ="Send" >
        
</ form >
    
</ body >
</ html >
5.编写sevlet : SEND1.java
package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SEND1 extends HttpServlet ... {

    
public SEND1() ...{
        
super();
     }


    
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException ...{

         response.setContentType(
"text/html");
         PrintWriter out
= response.getWriter();
         request.setCharacterEncoding(
"gb2312");
         String from
= request.getParameter("from");
         String to
= request.getParameter("to");
         String subject
= request.getParameter("subject");
         String context
= request.getParameter("context");
        
        
// 确定要发送的邮件服务器的地址
         String mailserver = "codedestiny-pc";
        
        
try ...{
            
// 设置邮件的传输协议
             Properties prop = System.getProperties();
             prop.put(
"mail.smtp.host", mailserver);
            
            
// 建立邮件发送的连接
             Session session = Session.getDefaultInstance(prop, null);
            
            
// 创建发送的信息的载体
             Message msg = new MimeMessage(session);
        
            
// 设置相关的邮件属性
             msg.setFrom(new InternetAddress(from));
            
            
// 点到点的发送
             msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            
            
/**//*
              * 群发 msg.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress(to),new InternetAddress(to)}); //借助循环的标准发送
             
*/

             msg.setSubject(subject);
             msg.setSentDate(
new Date());
             msg.setText(context);
            
            
// 发送
             Transport.send(msg);
            
         }
catch (Exception e) ...{
             e.printStackTrace();
         }


         out.print(
"send ok");
         out.flush();
         out.close();
     }


}

6.测试。

你可能感兴趣的:(邮件,学习,笔记,文本,javamail)