-1

我对这个 smtp 东西很陌生..我遇到了一个问题..我使用 smatermail 作为我的服务器邮件来接收和发送电子邮件...在我的客户端我有一个文本框可以更改发送电子邮件的端口。 ..当我使用端口 25 时它可以..但是当我更改到另一个端口时,即使是 smatermail eq 8025 中的端口,它也不想发送电子邮件..它就像服务器运行但不监听除 25 之外的其他端口..

它在日志中显示错误

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: 192.168.56.30, 25; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:345)
    at javax.mail.Service.connect(Service.java:226)
    at javax.mail.Service.connect(Service.java:175)
    at javax.mail.Transport.send0(Transport.java:253)
    at javax.mail.Transport.send(Transport.java:124)
    at com.mastersam.analyst.compass.engine.mail.Mailer$SenderWorker.run(Mailer.java:150)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:297)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
    ... 7 more

这是客户端代码

package com.mastersam.analyst.compass.engine.mail;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mastersam.analyst.compass.model.ApplicationSettingModel;

/**
 * Sends email updates. Multiple recipient must be semicolon-separated.
 */
public class Mailer {
    private static Log log = LogFactory.getLog(Mailer.class);

    private static Mailer self = new Mailer();

    public static Mailer instance() {
    return self;
    }

    /**
     * Sends email alerts to recepient(s).
     * 
     * @param to
     *            (Semicolon-separated if multiple) recipients address
     * @param subject
     * @param content
     *            Must be in textual form.
     */
    public void send(String[] to, String subject, String content)
        throws MessagingException, UnsupportedEncodingException {

        ApplicationSettingModel setting = ApplicationSettingModel.ThreadLocalSetting
        .get();
    if (setting == null) {
        throw new RuntimeException(
            "ApplicationSetting is not bind to current thread.");
    }

    SenderWorker worker = new SenderWorker(setting, to, subject, content);
    worker.start();
    }

    /**
     * Extract name form email address. E.g simon@example.com will return simon.
     */
    private static String getPersonalName(String add) {
    int ind = add.indexOf("@");
    return add.substring(0, ind);
    }

    /**
     * Init SMTP Authenticator
     */
    private static class SMTPAuthenticator extends Authenticator {
    PasswordAuthentication auth;

    public SMTPAuthenticator(String username, String password) {
        auth = new PasswordAuthentication(username, password);
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return auth;
    }

    }

    /**
     * Mailer internal class to configure and send SMTP mail
     */
    private static class SenderWorker extends Thread {
    private ApplicationSettingModel setting;
    private String[] to;
    private String subject;
    private String content;

    public SenderWorker(ApplicationSettingModel setting, String[] to,
        String subject, String content) {
        this.setting = setting;
        this.to = to;
        this.subject = subject;
        this.content = content;
    }

    /**
     * configure and return mail message
     * 
     * @param session
     *            javax.mail.Session
     * @return return configured mail message
     * @throws UnsupportedEncodingException
     * @throws MessagingException
     */
    private Message getMessage(Session session)
        throws UnsupportedEncodingException, MessagingException {
        Address[] toRecp = new Address[to.length];
        for (int i = 0; i < toRecp.length; i++) {
        toRecp[i] = new InternetAddress(to[i], getPersonalName(to[i]));
        }
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(setting.getSmtpMail(),
            getPersonalName(setting.getSmtpMail())));

        msg.setRecipients(Message.RecipientType.TO, toRecp);
        msg.setSubject(subject);
        msg.setText(content);
        Date d = new Date();
        msg.setSentDate(d);
        return msg;
    }

    public void run() {
        Properties authProps = new Properties();
        Properties normProps = new Properties();
        Session session = null;
        boolean requireAuth = setting.getSmtpMode().trim()
            .equalsIgnoreCase("Authenticated");
        boolean sent = false;
        authProps.put("mail.smtp.host", setting.getSmtp());
        normProps.put("mail.smtp.host", setting.getSmtp());

        if (requireAuth) {
        authProps.put("mail.smtp.auth", "true");
        session = Session.getInstance(authProps, new SMTPAuthenticator(
            setting.getSmtpUser(), setting.getSmtpPass()));
        try {
            Transport.send(getMessage(session));
            sent = true;
        } catch (Exception e) {
            log.error("run", e);
        }
        }

        if (!sent) {
        session = Session.getInstance(normProps, null);
        try {
            Transport.send(getMessage(session));
        } catch (Exception e) {
            log.error("run", e);
        }
        }
    }
    }
}

..我试图找到使端口成为默认值的代码在哪里..有人可以帮助我解决这个问题以及我需要在代码中进行的任何更改..谢谢你对我的英语感到抱歉..我还在学习它..

4

1 回答 1

1

从您可以看到的异常中,您的代码仍在尝试连接到端口 25:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: 192.168.56.30, 25; timeout -1

那是因为您在发送邮件时根本没有指定端口,所以使用了默认的端口 (25)。如果你想在不同的端口上发送邮件,你应该像主机值一样明确指定它:

authProps.put("mail.smtp.host", setting.getSmtp());
normProps.put("mail.smtp.host", setting.getSmtp());
authProps.put("mail.smtp.port", "8025");
normProps.put("mail.smtp.port", "8025");
于 2013-10-17T01:22:42.397 回答