Java 发送邮件拒绝发送到除25以外的其他端口(默认)
我对smtp非常陌生。我遇到了一个问题。我使用SmateMail作为我的服务器邮件来接收和发送电子邮件。在我的客户端,我有一个文本框,可以更改发送电子邮件的端口。当我使用端口25时,它可以。但是当我更改到另一个端口时,即使是SmateMail eq 8025中的端口,它也不想发送电子邮件。它就像服务器一样运行但不侦听除25以外的其他端口 它在日志中显示错误Java 发送邮件拒绝发送到除25以外的其他端口(默认),java,email,smtp,Java,Email,Smtp,我对smtp非常陌生。我遇到了一个问题。我使用SmateMail作为我的服务器邮件来接收和发送电子邮件。在我的客户端,我有一个文本框,可以更改发送电子邮件的端口。当我使用端口25时,它可以。但是当我更改到另一个端口时,即使是SmateMail eq 8025中的端口,它也不想发送电子邮件。它就像服务器一样运行但不侦听除25以外的其他端口 它在日志中显示错误 com.sun.mail.util.MailConnectException: Couldn't connect to host, port
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);
}
}
}
}
}
package com.mastersam.analyst.compass.engine.mail;
导入java.io.UnsupportedEncodingException;
导入java.util.Date;
导入java.util.Properties;
导入javax.mail.Address;
导入javax.mail.Authenticator;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.PasswordAuthentication;
导入javax.mail.Session;
导入javax.mail.Transport;
导入javax.mail.internet.InternetAddress;
导入javax.mail.internet.mimessage;
导入org.apache.commons.logging.Log;
导入org.apache.commons.logging.LogFactory;
导入com.mastersam.analyst.compass.model.ApplicationSettingModel;
/**
*发送电子邮件更新。多个收件人必须以分号分隔。
*/
公共类邮递员{
私有静态日志Log=LogFactory.getLog(Mailer.class);
private static Mailer self=new Mailer();
公共静态邮件程序实例(){
回归自我;
}
/**
*向收件人发送电子邮件警报。
*
*@param to
*(如果有多个收件人,则用分号分隔)收件人地址
*@param主题
*@param内容
*必须是文本形式。
*/
public void send(字符串[]收件人、字符串主题、字符串内容)
抛出MessagingException,不支持DencodingException{
ApplicationSettingModel设置=ApplicationSettingModel.ThreadLocalSetting
.get();
if(设置==null){
抛出新的运行时异常(
“应用程序设置未绑定到当前线程。”);
}
SenderWorker工作者=新的SenderWorker(设置、收件人、主题、内容);
worker.start();
}
/**
*从电子邮件地址中提取姓名simon@example.com西蒙会回来的。
*/
私有静态字符串getPersonalName(字符串添加){
int ind=add.indexOf(“@”);
返回add.substring(0,ind);
}
/**
*初始化SMTP验证器
*/
私有静态类SMTPAuthenticator扩展了验证器{
密码认证;
公共SMTPAuthenticator(字符串用户名、字符串密码){
auth=新密码身份验证(用户名、密码);
}
受保护的密码身份验证getPasswordAuthentication(){
返回auth;
}
}
/**
*用于配置和发送SMTP邮件的Mailer内部类
*/
私有静态类SenderWorker扩展线程{
私有应用设置模型设置;
私有字符串[]到;
私有字符串主题;
私有字符串内容;
public SenderWorker(应用程序设置模型设置,字符串[]到,
字符串主题、字符串内容){
这个。设置=设置;
这个;
this.subject=主语;
this.content=内容;
}
/**
*配置并返回邮件消息
*
*@param会话
*javax.mail.Session
*@return配置的邮件消息
*@抛出不支持的DencodingException
*@MessaginException
*/
私有消息getMessage(会话)
抛出不支持的DencodingException、MessaginException{
地址[]toRecp=新地址[to.length];
for(int i=0;i
…我试图找到使端口成为默认端口的代码的位置..有人能在这件事上帮助我吗,以及我需要对代码进行的任何更改..谢谢你n对不起我的英语..我仍在学习..从你可以看到的例外情况来看,你的代码仍在尝试连接到端口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");
用于主机阻止端口的公用程序。这与phpowh wokeyh thnks bro无关,提醒我…是的,我知道,但客户端可以更改系统提供的文本框中的端口..如何将这些输入包含在代码中..因此您需要从文本框中传输此值