我正在使用javaMailSender发送邮件。
Java邮件生产者:
// Publishing the message to the JMS queue.
queueClinicalMessageServiceTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
MapMessage mapMessage = session.createMapMessage();
mapMessage.setStringProperty(ActivemqConstants.KEY_EMAIL_ADDRESS, email);
mapMessage.setStringProperty(ActivemqConstants.KEY_PASSWORD, password);
mapMessage.setStringProperty(ActivemqConstants.KEY_REG_MSG_TYPE,
ActivemqConstants.VALUE_SEND_CLI_REG_EMAIL);
return mapMessage;
}
});
Java邮件使用者:
try {
logger.debug("JMSMessageID = {}; MSDeliveryMode = {}; JMSRedelivered = {}; JMSQueue = {}.",
msg.getJMSMessageID(), msg.getJMSDeliveryMode(), msg.getJMSRedelivered(),
msg.getStringProperty(ActivemqConstants.KEY_REG_MSG_TYPE));
if (msg instanceof MapMessage) {
MapMessage mapMsg = (MapMessage) msg;
String email = mapMsg.getStringProperty(ActivemqConstants.KEY_EMAIL_ADDRESS);
String password = mapMsg.getStringProperty(ActivemqConstants.KEY_PASSWORD);
Map<String, String> model = new HashMap<String, String>();
model.put("password", password);
emailSender.sendMail(messageSource.getMessage("mail.username", null, null, null), email,
"Subject", model,
"com/utils/RegistrationEmail.ftl");
}
} catch (JMSException ex) {
logger.error("JMSException:", ex);
throw new RuntimeException(ex);
}
public void sendMail(final String from, final String to, final String subject, final Object model,
final String template) throws MailException {
MimeMessagePreparator mimeMessagePreparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setTo(to);
mimeMessageHelper.setSubject(subject);
String text = FreeMarkerTemplateUtils
.processTemplateIntoString(freemarkerConfiguration.getTemplate(template, "UTF-8"), model);
// boolean true is for html
mimeMessageHelper.setText(text, true);
}
};
try {
logger.info("Attempting to send email via Gmail now");
javaMailSender.send(mimeMessagePreparator);
} catch (Exception e) {
logger.debug("MailException occured while sending the mail for ftl model:::{}::: MailException object::{}",
model, e);
try {
logger.info("Attempting to send email via Gmail now");
alternateJavaMailSender.send(mimeMessagePreparator);
} catch (MailException ae) {
logger.error(
"MailException occured while sending the mail for ftl model:::{}::: MailException object::{}",
model, ae);
throw ae;
}
}
}
邮件配置
mail.username=XXX@gmail.com
mail.password=YYY
mail.host=smtp.gmail.com
mail.smtp.port=25
mail.smtp.auth=true
当我在本地系统上运行相同的代码时,我能够成功发送所有邮件(一次发送20到30)。但是在生产(AWS)服务器上,从20到30封邮件中仅接收3到4封邮件。并在生产服务器上获取以下异常:
10:51:15.667 [userRegEmailDMLC-1] DEBUG [net.abc.xyz.consumer.utils.EmailSender]-MailException occured while sending the mail for ftl model:::{password=AppleGoldDiamond}::: MailExce
ption object::{}
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 25; tim
eout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:432)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:362)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:350)
at net.abc.xyz.consumer.utils.EmailSender.sendMail(EmailSender.java:97)
at net.abc.xyz.consumer.mdp.user.userRegEmailServiceListener.onMessage(userRegEmailServiceListener.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy72.onMessage(Unknown Source)
at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:746)
at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:684)
at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:651)
at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:317)
at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:255)
at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1166)
at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1158)
at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:1055)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 25; timeout -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2053)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
at javax.mail.Service.connect(Service.java:364)
at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:501)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:421)
... 27 common frames omitted
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:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:312)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2019)
... 31 common frames omitted
可能是问题所在,因为我在生产上遇到了错误,但在本地遇到了错误。
答案 0 :(得分:0)
由于安全原因,默认情况下,EC2在发送邮件方面有严格的限制。您必须发送支持请求(以root用户身份登录帐户,然后按照link进行操作),这是确保邮件流合法的责任。
尽管如此,建议您use SES instead 。
答案 1 :(得分:0)
您可能会发现使用MailChimp有用