请问用java如何实现电子邮件客户端? 另外如何实现上传附件呢?

解决方案 »

  1.   

    简单点,JavaMail。
    虽然不是开源的,但是,用起来要简单多了。
      

  2.   

    javaMail是j2ee的一项重要组成,值得学习
      

  3.   

    推荐使用apache的common email,基于javamail,而且使用简单方便。
      

  4.   

    sendmail.jsp
    <%@ page contentType="text/html;charset=GB2312"%>
    <%
    request.setCharacterEncoding("gb2312");
    %>
    <%@ page import="java.util.*,javax.mail.*"%>
    <%@ page import="javax.mail.internet.*"%>
    <%@ page import="javax.activation.*"%>
    <!--要发送附件必须引入该库-->
    <%@ page import="java.net.*"%>
    <!--要用到URL类-->
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>发送成功</title>
    </head>
    <body bgcolor="#CFF1E1"> <%
    String host = (String) session.getAttribute("host");
    String user = (String) session.getAttribute("user");
    String password = (String) session.getAttribute("password");%>

    <% try {
    String tto = request.getParameter("to");
    String ttitle = request.getParameter("title");
    String emailtype = request.getParameter("emailtype");//获取email类型
    String tcontent = request.getParameter("content");
    String tfj1 = request.getParameter("fj1");
    String tfj2 = request.getParameter("fj2"); Properties props = new Properties();
    props.put("mail.smtp.host", "smtp."+host);
    props.put("mail.smtp.auth", "true");
    Session s = Session.getInstance(props);
    s.setDebug(true); MimeMessage message = new MimeMessage(s); //给消息对象设置发件人/收件人/主题/发信时间
    InternetAddress from = new InternetAddress(
    user+"@"+host);
    message.setFrom(from);
    InternetAddress to = new InternetAddress(tto);
    message.setRecipient(Message.RecipientType.TO, to);
    message.setSubject(ttitle);
    message.setSentDate(new Date()); Multipart mm = new MimeMultipart();//新建一个MimeMultipart对象用来存放多个BodyPart对象 //设置信件文本内容
    BodyPart mdp = new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
    mdp.setContent(tcontent, emailtype + ";charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
    mm.addBodyPart(mdp);//将含有信件内容的BodyPart加入到MimeMultipart对象中 //设置信件的附件1(自定义附件:直接将所设文本内容加到自定义文件中作为附件发送)
    DataHandler dh = new DataHandler(tfj1,
    "text/plain;charset=gb2312");
    //新建一个DataHandler对象,并设置其内容和格式/编码方式
    if (tfj1.length() > 0) {
    mdp = new MimeBodyPart();//新建一个存放附件的BodyPart mdp.setFileName("text.txt");//加上这句将作为附件发送,否则将作为信件的文本内容
    mdp.setDataHandler(dh);//给BodyPart对象设置内容为dh
    mm.addBodyPart(mdp);//将含有附件的BodyPart加入到MimeMultipart对象中
    }
    //设置信件的附件2(用本地上的文件作为附件)
    if (tfj2.length() > 0) {
    mdp = new MimeBodyPart();
    FileDataSource fds = new FileDataSource(tfj2);
    dh = new DataHandler(fds);
    int ddd = tfj2.lastIndexOf("\\");
    String fname = tfj2.substring(ddd + 1);//提取文件名
    //String ffname = new String(fname.getBytes("gb2312"),
    //"ISO8859-1");//处理文件名是中文的情况]
    String ffname = javax.mail.internet.MimeUtility.encodeText(fname);
    mdp.setFileName(ffname);//可以和原文件名不一致,但最好一样
    mdp.setDataHandler(dh);
    mm.addBodyPart(mdp);
    }
    message.setContent(mm);//把mm作为消息对象的内容
    message.saveChanges();
    Transport transport = s.getTransport("smtp");
    transport.connect("smtp."+host, user, password);
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
    %>

    <div align="center">
    <p>
    <font color="#FF6600">发送成功!</font>
    </p>
    <p>
    <a href="receivemail.jsp">去看看我的信箱</a>
    <br>
    <br>
    <a href="sendmail.html">再发一封</a>
    </p>
    </div>
    <%
    } catch (MessagingException e) {
    out.println(e.toString());
    }
    %>
    </body>
    </html>
    参考一下