我在使用弹簧邮件向gmail帐户发送邮件时遇到了问题。 我从stackoverflow中提到了大部分帖子并试过了。但没有运气。仍然坚持下去。请建议我的代码中缺少的内容。
Windows -8: Java 7
EmailServiceImpl.java:
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import com.test.mail.data.EmailData;
public class EmailServiceImpl {
private JavaMailSender mailSender;
public JavaMailSender getMailSender() {
return mailSender;
}
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void sendMail(EmailData emailData) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
try {
helper.setFrom(emailData.getFrom());
helper.setTo(emailData.getTo());
helper.setSubject(emailData.getSubject());
helper.setText(emailData.getBody());
} catch (MessagingException e) {
e.printStackTrace();
}
mailSender.send(message);
}
}
应用context.xml中
<bean id="emailSvcImpl"
class="com.test.mail.services.impl.EmailServiceImpl">
<property name="mailSender" ref="mailSender" />
</bean>
<!-- via TLS -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<!-- <property name="port" value="25" /> --> <!-- Also checked with port number : 25 -->
<property name="port" value="587" />
<property name="protocol" value="smtp" />
<property name="username" value="fromaccount@gmail.com" />
<property name="password" value="fromaccountpwd" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.debug">true</prop>
<prop key="mail.smtp.timeout">8500</prop>
</props>
</property>
</bean>
<!-- Also Tried to send gmail via SSL -->
<!--
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="465" />
<property name="protocol" value="smtps" />
<property name="username" value="fromaccount@gmail.com" />
<property name="password" value="fromaccountpwd" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtps.auth">true</prop>
<prop key="mail.smtps.starttls.enable">true</prop>
<prop key="mail.smtps.debug">true</prop>
<prop key="mail.smtps.timeout">8500</prop>
</props>
</property>
</bean> -->
Maven依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.11.RELEASE</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.2</version>
</dependency>
cmd提示:已使用&#39; ping smtp.gmail.com&#39;命令ping,能够得到答复,没有数据丢失。
例外:
org.apache.cxf.interceptor.Fault: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
尝试使用SSL和TLS发送邮件,但使用不同的端口号发送相同的例外
答案 0 :(得分:4)
Google已更改政策,您需要允许应用访问您的帐户。
我昨天遇到了一个问题,并通过google.com收到了这封电子邮件
您可以切换到Google制作的应用程序(如Gmail)来访问您的帐户(推荐)或更改https://www.google.com/settings/security/lesssecureapps设置,以便您的帐户不再受现代安全标准的保护。
一旦你完成了所有,你可以验证属性,这是我使用的,它在我的最终完美地工作
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
props.put("mail.smtp.socketFactory.port", "465"); //SSL Port
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory"); //SSL Factory Class
props.put("mail.smtp.auth", "true"); //Enabling SMTP Authentication
props.put("mail.smtp.port", "465"); //SMTP Port
我希望您使用正确的值。
答案 1 :(得分:1)
您的网络上似乎有防火墙阻止您连接。您可能需要configure JavaMail to work through your proxy server。
仅供参考,connection debugging tips are also in the JavaMail FAQ。