<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "java.util.*,java.io.*"%>
<%@ page import = "javax.mail.*" %>
<%@ page import = "javax.mail.internet.*" %>
<%@ page import = "javax.activation.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Send Mail</title>
</head>
<body>
<% Properties theProperties = System.getProperties();
theProperties.put("maik.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("[email protected]"));
theMessage.setRecipients(Message.RecipientType.TO, "[email protected]");
theMessage.setSubject("javaMail测试");
theMessage.setText("hello world", "GB2312");
Transport.send(theMessage);
out.println("成功发送邮件");
%>
</body>
</html>显示Transport.send(theMessage);错误  为什么?

解决方案 »

  1.   

    你这里发送之前,connect都没有调用,总之很乱的,写邮件干嘛要写在jsp里面,写在java文件里面多好。
      

  2.   

    也发一份给我把 [email protected]
      

  3.   


    package com.shuttle.util;import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.Properties;import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;import org.apache.log4j.Logger;
    public class MailSender {
    private static Logger logger = Logger.getLogger(MailSender.class); private String hostName; private String password; private String username; private String subject; private String from; private List<String> to; private String msg; public MailSender() { } public void setHostName(String hostName) {
    this.hostName = hostName;
    } public String getFrom() {
    return from;
    } public void setFrom(String from) {
    this.from = from;
    } public String getMsg() {
    return msg;
    } public void setMsg(String msg) {
    this.msg = msg;
    } public String getSubject() {
    return subject;
    } public void setSubject(String subject) {
    this.subject = subject;
    } public List getTo() {
    return to;
    } public void addTo(String to) {
    if (this.to == null) {
    this.to = new ArrayList<String>();
    }
    this.to.add(to);
    } public String getHostName() {
    return hostName;
    } public void setAuthentication(String username, String password) {
    this.username = username;
    this.password = password;
    } public void send() throws Exception {
    // TODO Auto-generated method stub try {
    Properties prop = new Properties();
    prop.put("mail.host", this.hostName);
    prop.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(prop);
    session.setDebug(false); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(this.from));
    for (String item : this.to) {
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
    item));
    }
    msg.setSubject(this.subject);
    msg.setSentDate(new Date());
    msg.setText(this.msg);
    msg.saveChanges(); Transport transport = session.getTransport("smtp"); transport.connect(hostName, username, password);
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close(); } catch (Exception e) {
    logger.error(e.getMessage(), e);
    throw e;
    } }

    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub try {
    MailSender ms = new MailSender();
    ms.setHostName("smtp.163.com");
    ms.setFrom("[email protected]");
    ms.addTo("[email protected]");
    ms.setAuthentication("username", "password");
    ms.setSubject("ok");
    ms.setMsg("helloworld");
    ms.send(); } catch (Exception e) {
    e.printStackTrace();
    } }}