<html>
<head>
<title>新建邮件</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="pragma" content="no-cache">
</head>
<body>
<form method="post" action="SendMailPlain.jsp">
<table width="75" border="0" align="center" cellspacing="1">
<tr bgcolor="#FFFFFF">
<td width="24%">收信人地址:</td>
<td width="76%">
<input name="to" type="text" id="to" value="[email protected]"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>主题:</td>
<td>
<input name="title" type="text" value="这是一封文本的测试邮件"></td>
</tr>
<tr>
<td height="107" colspan="2" bgcolor="#FFFFFF">
<textarea name="content" cols="50" rows="5">这是一封文本的测试邮件</textarea></td>
</tr>
<tr align="center">
<td colspan="2" bgcolor="#FFFFFF">
<input type="submit" name="tSubmit" value="发送">
<input type="reset" name="bReset" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>

解决方案 »

  1.   

    SendMailPlain.jsp
    <%@ page contentType="text/html;charset=GB2312"%>
    <%request.setCharacterEncoding("gb2312");%><!--中文处理代码-->
    <!--引入要用到的类库-->
    <%@ page import="java.util.*,javax.mail.*"%>
    <%@ page import="javax.mail.internet.*"%>
    <html>
    <head>
    <title>发送成功</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <meta http-equiv="Content-Language" content="zh-cn">
    <meta http-equiv="pragma" content="no-cache">
    </head>
    <body>
    <%
    try{
    //从html表单中获取邮件信息
    String tto=request.getParameter("to");
    String ttitle=request.getParameter("title");
    String tcontent=request.getParameter("content");
    Properties props=new Properties();//也可用Properties props = System.getProperties();
    props.put("mail.smtp.host","mail.EchoChina.net");//存储发送邮件服务器的信息
    props.put("mail.smtp.auth","true");//同时通过验证
    Session s=Session.getInstance(props);//根据属性新建一个邮件会话
    s.setDebug(true);
    MimeMessage message=new MimeMessage(s);//由邮件会话新建一个消息对象
    //设置邮件
    InternetAddress from=new InternetAddress("[email protected]");
    message.setFrom(from);//设置发件人
    InternetAddress to=new InternetAddress(tto);
    message.setRecipient(Message.RecipientType.TO,to);//设置收件人,并设置其接收类型为TO
    message.setSubject(ttitle);//设置主题
    message.setText(tcontent);//设置信件内容
    message.setSentDate(new Date());//设置发信时间
    //发送邮件
    message.saveChanges();//存储邮件信息
    Transport transport=s.getTransport("smtp");
    transport.connect("mail.163.net","[email protected]","password");//以smtp方式登录邮箱
    transport.sendMessage(message,message.getAllRecipients());//发送邮件,其中第二个参数是所有
    //已设好的收件人地址
    transport.close();
    %>
    <div align="center">
    <p><font color="#FF0000">发送成功!</font></p>
    <p><a href="http://mail.163.net">去收邮件</a><br>
    <br>
    <a href="index.htm">再发一封</a> </p>
    </div>
    <%
    }catch(MessagingException e){
    out.println(e.toString());
    e.printStackTrace();
    }
    %>
    </body>
    </html>
      

  2.   

    给你一个你参照一下,public void sendMail(
                    String host,
                    String from,
                    //Address[] toaddress,
                    String toaddress,                String mailsubject,
                    //Address[] ccaddress,
                    //Address[] bccaddress,
                    String ccaddress,
                    String bccaddress,                String content,
                    String account,
                    String password)
                    throws Exception {
                    boolean flag = false;
                    try {
                            Properties props = new Properties(); //获得系统属性
                            props.put("mail.smtp.host", host); //设置SMTP主机
                            props.put("mail.smtp.auth", "true"); //这样才能通过验证
                            if (props.getProperty("user.region") != null
                                    && props.getProperty("user.region").equals("TW")
                                    && props.getProperty("user.language") != null
                                    && props.getProperty("user.language").equals("zh")) {
                                    props.setProperty("file.encoding", "BIG5");
                            }
                            //获得邮件会话对象
                            Session session = Session.getDefaultInstance(props, null);
                            session.setDebug(true);
                            //创建MIME邮件对象
                            MimeMessage mimeMsg = new MimeMessage(session);
                            //设置发信人
                            mimeMsg.setFrom(new InternetAddress(from));
                            //设置收信人
                            if (toaddress != null) {
                                    mimeMsg.setRecipients(Message.RecipientType.TO, toaddress);
                            }                        //设置抄送人
                            if (ccaddress != null) {
                                    mimeMsg.setRecipients(Message.RecipientType.CC, ccaddress);
                            }                        //设置暗送人
                            if (bccaddress != null) {
                                    mimeMsg.setRecipients(Message.RecipientType.BCC, bccaddress);
                            }                        //设置邮件主题
                            String subject = MimeUtility.encodeText(mailsubject, "GBK", "B");
                            mimeMsg.setSubject(subject);
                            // 创建 Multipart 并放入每个 MimeBodyPart
                            Multipart mp = new MimeMultipart();
                            // 第一部分信息
                            MimeBodyPart mbp1 = new MimeBodyPart();
                            mbp1.setText(content, "GBK");
                            mp.addBodyPart(mbp1);                        mimeMsg.setContent(mp);                        //发送日期
                            mimeMsg.setSentDate(new Date());
                            //发送邮件
                            mimeMsg.saveChanges();
                            Transport transport = session.getTransport("smtp");
                            transport.connect(host, account, password);
                            //设置smtp认证
                            transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());                        transport.close();                        //Transport.send(mimeMsg);
                            flag = true;
                    } catch (SendFailedException e) {
                            //throw new OurException("邮件发送失败!");
                            System.out.println("发送邮件失败!");
                    } catch (MessagingException e) {
                            //throw new OurException("邮件发送失败!");
                            System.out.println("发送邮件失败!");
                    } catch (Exception e) {
                            //throw new OurException("邮件发送失败!");
                            System.out.println("发送邮件失败!");
                    } finally {
                            if (flag == false)
                                    //throw new OurException("发送邮件失败!");
                                    System.out.println("发送邮件失败!");
                    }
            }
      

  3.   

    example:sendMail("ftd-asia.com.cn", "[email protected]", email,
                       "回复:您好!我是昆山双叶软件科技有限公司!", "", "", leaveword, "bobshi", "12345678");
      

  4.   

    我只要一个简单的邮件发送程序...如题,却无法调试成功,我想知道这是为什么.是那些地方错了.那些地方要改.还有我的jmail是1.3版本的.我的JDK是1.4版本的..
      

  5.   

    楼主还少一个包:
    javax.activation.*
    可以单独在这里下载activation.jar,
    http://java.sun.com/products/javabeans/glasgow/jaf.html
    或者整个下载J2EE,它的j2ee.jar里已经包含JAF 和JavaMail了;
      

  6.   

    可以单独在这里下载activation.jar,
    http://java.sun.com/products/javabeans/glasgow/jaf.html
    或者整个下载J2EE,它的j2ee.jar里已经包含JAF 和JavaMail