Java 被防火墙阻止的邮件
我的服务器的防火墙有问题。我正试图使用sendmail从我的服务器发送电子邮件,但当防火墙被激活时,这是不可能的。我在日志中发现以下错误: 无法连接到主机,端口:mail.infomaniak.ch,25;超时-1 当我的防火墙被禁用时,一切正常,我的邮件被发送。所以我试着在防火墙的输出端打开端口25,但是没有任何改变,我总是遇到同样的问题 这是我的密码:Java 被防火墙阻止的邮件,java,linux,sendmail,firewall,Java,Linux,Sendmail,Firewall,我的服务器的防火墙有问题。我正试图使用sendmail从我的服务器发送电子邮件,但当防火墙被激活时,这是不可能的。我在日志中发现以下错误: 无法连接到主机,端口:mail.infomaniak.ch,25;超时-1 当我的防火墙被禁用时,一切正常,我的邮件被发送。所以我试着在防火墙的输出端打开端口25,但是没有任何改变,我总是遇到同样的问题 这是我的密码: public void envoyerMailSMTP(ArrayList<String> to, ArrayList<S
public void envoyerMailSMTP(ArrayList<String> to, ArrayList<String> cc, ArrayList<String> bcc, String from, String subject, String body, String attachment, String attachmentName) throws Exception {
this.catalog = (Catalog)lookupEjb("CatalogLocal", true);
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
//props.put("mail.smtp.port", "587");
props.put("mail.smtp.host", this.catalog.getFirst(this.catalog.findByKey("SMTP_HOST_NAME")).getCatalogValue());
props.put("mail.smtp.auth", "true");
Session mailSession = Session.getDefaultInstance(props);
// uncomment for debugging infos to stdout
// mailSession.setDebug(true);
try {
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
// Ajout de l'expéditeur du mail
if(from != null && !from.isEmpty()){
message.setFrom(new InternetAddress(from));
}
// Ajout de(s) destinataire(s) du mail
for(String s : to){
message.addRecipient(Message.RecipientType.TO, new InternetAddress(s));
}
// Ajout de(s) destinataire(s) du mail en cc
if(cc != null && cc.size() > 0){
for(String s : cc){
message.addRecipient(Message.RecipientType.CC, new InternetAddress(s));
}
}
// Ajout de(s) destinataire(s) du mail en bcc
if(bcc != null && bcc.size() > 0){
for(String s : bcc){
message.addRecipient(Message.RecipientType.BCC, new InternetAddress(s));
}
}
// Set Subject: header field
message.setSubject(subject);
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText(body);
// Create a multipart message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
messageBodyPart.setDataHandler(new DataHandler(source));
// Set the name of the attached file
messageBodyPart.setFileName(attachmentName);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
transport.connect(this.catalog.getFirst(this.catalog.findByKey("SMTP_HOST_NAME")).getCatalogValue(),
this.catalog.getFirst(this.catalog.findByKey("SMTP_AUTH_USER")).getCatalogValue(),
this.catalog.getFirst(this.catalog.findByKey("SMTP_AUTH_PWD")).getCatalogValue());
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
}catch (MessagingException ex){
System.out.println(ex.getMessage());
System.out.println(ex.getStackTrace());
}
}
public void envoyerMailSMTP(ArrayList to、ArrayList cc、ArrayList bcc、String from、String subject、String body、String attachment、String attachmentName)引发异常{
this.catalog=(catalog)lookupEjb(“CatalogLocal”,true);
Properties props=新属性();
props.put(“mail.transport.protocol”、“smtp”);
//props.put(“mail.smtp.port”,“587”);
props.put(“mail.smtp.host”、this.catalog.getFirst(this.catalog.findByKey(“smtp\u host\u NAME”)).getCatalogValue();
props.put(“mail.smtp.auth”,“true”);
会话mailSession=Session.getDefaultInstance(props);
//取消对标准输出调试信息的注释
//mailSession.setDebug(true);
试一试{
传输=mailSession.getTransport();
mimessage message=新mimessage(mailSession);
//一份关于邮件的经验
if(from!=null&!from.isEmpty()){
message.setFrom(新的InternetAddress(from));
}
//邮政局局长
for(字符串s:to){
message.addRecipient(message.RecipientType.TO,新的Internet地址);
}
//邮件目的地抄送
如果(cc!=null&&cc.size()>0){
for(字符串s:cc){
message.addRecipient(message.RecipientType.CC,新的Internet地址);
}
}
//以密件抄送邮件的目的地
如果(bcc!=null&&bcc.size()>0){
用于(字符串s:bcc){
message.addRecipient(message.RecipientType.BCC,新的Internet地址);
}
}
//设置主题:标题字段
message.setSubject(主题);
//创建消息部分
BodyPart messageBodyPart=新的MimeBodyPart();
//填写信息
messageBodyPart.setText(body);
//创建多部分消息
Multipart Multipart=新的MimeMultipart();
//设置文本消息部分
multipart.addBodyPart(messageBodyPart);
//第二部分是附件
messageBodyPart=新的MimeBodyPart();
数据源=新文件数据源(附件);
setDataHandler(新的DataHandler(源));
//设置附加文件的名称
messageBodyPart.setFileName(attachmentName);
multipart.addBodyPart(messageBodyPart);
//发送完整的消息部分
message.setContent(多部分);
transport.connect(this.catalog.getFirst(this.catalog.findByKey(“SMTP\u主机名”)).getCatalogValue(),
this.catalog.getFirst(this.catalog.findByKey(“SMTP\u AUTH\u USER”)).getCatalogValue(),
this.catalog.getFirst(this.catalog.findByKey(“SMTP_AUTH_PWD”)).getCatalogValue();
transport.sendMessage(message,message.getRecipients(message.RecipientType.TO));
transport.close();
}捕获(消息例外){
System.out.println(例如getMessage());
System.out.println(例如getStackTrace());
}
}
我还试图修改我的代码以使用587端口,并在防火墙上打开它,但效果并不好
我有一个Linux服务器,我使用ufw作为防火墙
有人能帮我吗?如果默认情况下阻止传出流量,则需要启用从所有源端口到目标端口25的连接。SMTP从主机上的随机端口连接到服务器上的端口25,因此您不能只打开端口25。如果是非root用户进行连接,则可以只打开端口1024-65545,因为非root用户无法打开小于1024的端口 您可以通过以下方式允许传出连接:
ufw default allow outgoing
您是否尝试直接从命令行使用sendmail?你说的
是什么意思。。。但是它更好
?比什么更好,以什么方式更好?是的,我以前试过,邮件在命令行中发送得不太好,然后我会说问题不在于Java代码,而在于系统或防火墙配置。我尝试通过如下方式打开端口25:sudo ufw allow out 25但不起作用仅允许端口25出站不起作用-您必须打开所有端口,因为连接是从服务器上的随机端口连接到服务器上的端口25目标服务器。