gpt4 book ai didi

java - 无法使用 SMTP 在 java 中发送电子邮件,它被我办公室的防火墙阻止了。我们可以通过其他方法发送邮件吗?

转载 作者:行者123 更新时间:2023-11-30 11:02:34 24 4
gpt4 key购买 nike

有2个java文件和一个xml文件。无法使用 smtp 发送电子邮件。请帮助:

Springmail.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.office365.com" />
<property name="port" value="587" />

<property name="protocol" value="smtps" />

<property name="username" value="xxxxx@xyz.com" />
<property name="password" value="xxxxxx" />

<property name="javaMailProperties">
<props>
<prop key="mail.smtps.auth">true</prop>
<prop key="mail.smtps.starttls.enable">true</prop>

<prop key="mail.smtp.ssl.enable">true</prop>
<prop key="mail.transport.protocol">smtps</prop>

<!-- <prop key="mail.transport.protocol.rfc822">smtps</prop> -->

<prop key="mail.debug">true</prop>

<prop key="mail.smtps.quitwait">false</prop>

<prop key="mail.smtps.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtps.socketFactory.fallback">false</prop>

<prop key="mail.smtp.timeout">30000</prop>
</props>
</property>

</bean>

<bean id="mailMail" class="com.mkyong.common.MailMail">
<property name="mailSender" ref="mailSender" />
<property name="simpleMailMessage" ref="customeMailMessage" />
</bean>

<bean id="customeMailMessage"
class="org.springframework.mail.SimpleMailMessage">

<property name="from" value="xxxxx@xyz.com" />
<property name="to" value="xxxxx@xyz.com" />
<property name="subject" value="Testing Subject" />
<property name="text">
<value>
<![CDATA[
Dear %s,
Mail Content : %s
]]>
</value>
</property>
</bean>

</beans>

邮件邮件.java有2个java文件和一个xml文件。无法使用 smtp 发送电子邮件。请帮忙

package com.mkyong.common;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailParseException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailMail
{
private JavaMailSender mailSender;
private SimpleMailMessage simpleMailMessage;

public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
this.simpleMailMessage = simpleMailMessage;
}

public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}

public void sendMail(String dear, String content) {

MimeMessage message = mailSender.createMimeMessage();

try{
MimeMessageHelper helper = new MimeMessageHelper(message, true);

helper.setFrom(simpleMailMessage.getFrom());
helper.setTo(simpleMailMessage.getTo());
helper.setSubject(simpleMailMessage.getSubject());
helper.setText(String.format(
simpleMailMessage.getText(), dear, content));

FileSystemResource file = new FileSystemResource(
"C:\\vivek\\BDD_13thMay\\cucumber.json");

helper.addAttachment(file.getFilename(), file);


}catch (MessagingException e) {
throw new MailParseException(e);
}
mailSender.send(message);


}


}

应用程序.java

package com.mkyong.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Mail.xml");

MailMail mm = (MailMail) context.getBean("mailMail");
mm.sendMail("Yong Mook Kim", "This is text content");
}
}

控制台显示以下详细信息:有2个java文件和一个xml文件。无法使用 smtp 发送电子邮件。请帮忙

Jun 09, 2015 12:59:19 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6041f871: display name [org.springframework.context.support.ClassPathXmlApplicationContext@6041f871]; startup date [Tue Jun 09 12:59:19 IST 2015]; root of context hierarchy
Jun 09, 2015 12:59:20 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Spring-Mail.xml]
Jun 09, 2015 12:59:21 PM org.springframework.context.support.ClassPathXmlApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@6041f871]: org.springframework.beans.factory.support.DefaultListableBeanFactory@4ce1bafc
Jun 09, 2015 12:59:21 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4ce1bafc: defining beans [mailSender,mailMail,customeMailMessage]; root of factory hierarchy
DEBUG: JavaMail version 1.4.4
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.office365.com", port 587, isSSL true
Exception in thread "main" org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.office365.com, port: 587;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:419)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:342)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:338)
at com.mkyong.common.MailMail.sendMail(MailMail.java:47)
at com.mkyong.common.App.main(App.java:13)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.office365.com, port: 587;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
at javax.mail.Service.connect(Service.java:295)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389)
... 4 more
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:625)
at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:160)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:205)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
... 7 more

最佳答案

我可以使用 telnet 连接到 587 端口上的 office365.com,另一端有一个邮件服务器。

(在 linux 上,我运行“telnet office365.com 587”...)

显然您无法连接。或者至少,您正在运行的 Java 应用程序无法连接。

最可能的解释是防火墙阻止了传出连接:

  • 检查您计算机的防火墙规则。

  • 如果您是在家中执行此操作,请检查您的路由器/调制解调器的防火墙规则。如果失败,请与您的 ISP 联系。

  • 如果您是在工作单位(或学校)执行此操作,请咨询您的网络管理员。

关于java - 无法使用 SMTP 在 java 中发送电子邮件,它被我办公室的防火墙阻止了。我们可以通过其他方法发送邮件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30725672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com