I read on several sites that, when using the JavaMail API, to set the property mail.smtp.ssl.enable to true. I have some code as follows:
props.put("mail.smtp.host", "exchangemail1.example.com");
props.put("mail.from", "myemail@example.com");
props.put("mail.smtp.starttls.enable", "true");
// I tried this by itself and also together with ssl.enable)
props.put("mail.smtp.ssl.enable", "true");
Session session = Session.getInstance(props, null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO, "me.at@example.com");
// also tried @gmail.com
msg.setSubject("JavaMail ssl test");
msg.setSentDate(new Date());
msg.setText("Hello, world!\n");
props.put("mail.smtp.auth", "false");
Transport trnsport;
trnsport = session.getTransport("smtp");
trnsport.connect();
msg.saveChanges();
trnsport.sendMessage(msg, msg.getAllRecipients());
trnsport.close();
Run Code Online (Sandbox Code Playgroud)
This …
问题来自主题.我正在使用Spring 3.0.3.RELEASE和velocity 1.6.4中的JavaMailSenderImpl从模板进行邮件准备.
当我从我的webapp收件人发送带有克罗地亚字符的电子邮件时收到"?" 在正常的克罗地亚人的立场.如果我为邮件调试调试模式,从日志中我可以看到Content-type设置为:
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
Run Code Online (Sandbox Code Playgroud)
如何将其设置为:
Content-Type: text/html; charset=utf-8?
Run Code Online (Sandbox Code Playgroud)
我正在开发这个webapp时使用gmail进行邮件发送.
以下是我在spring的servlet xml conf文件中的设置:
<bean id="userAuthorizationManager" class="com.mypackage.manage.SimpleUserAuthorizationManagerImpl">
<property name="mailSender" ref="mailSender" />
<property name="velocityEngine" ref="velocityEngine" />
<property name="from" value="address" />
<property name="authorizationAddress" value="some text" />
<property name="subject" value="some text" />
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="465" />
<property name="username" value="user" />
<property name="password" value="pass" />
<property name="protocol" value="smtps" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.connectiontimeout">5000</prop>
<prop key="mail.smtp.sendpartial">true</prop>
<prop key="mail.smtp.userset">true</prop> …Run Code Online (Sandbox Code Playgroud) JavaMail是否支持通过服务器推送通知新电子邮件?
如果是,那么该文件在哪里?如果没有,是否有可以执行此操作的库?
我正在尝试使用Glassfish 3.1提供的jar文件在Java EE 6中使用JavaMail设置简单的日志记录测试.关于这个问题似乎有很多问题,但我还没有找到任何有帮助的答案.我的测试代码如下所示:
import java.util.logging.Logger;
public class MyClass {
private static final Logger LOGGER = Logger.getLogger("MyClass");
public static void main(String[] args) {
LOGGER.severe("This is a test");
}
}
Run Code Online (Sandbox Code Playgroud)
我的logging.properties文件包含以下内容:
com.sun.mail.util.logging.MailHandler.mail.smtp.host={my mail hub FQDN}
com.sun.mail.util.logging.MailHandler.mail.smtp.port=25
com.sun.mail.util.logging.MailHandler.mail.to={me}
com.sun.mail.util.logging.MailHandler.mail.from={support address}
com.sun.mail.util.logging.MailHandler.level=WARNING
com.sun.mail.util.logging.MailHandler.verify=local
com.sun.mail.util.logging.MailHandler.subject=Application Error
com.sun.mail.util.logging.MailHandler.formatter=java.util.logging.SimpleFormatter
Run Code Online (Sandbox Code Playgroud)
我使用以下方法构建类:
javac -cp $AS_INSTALL/glassfish/modules/javax.mail.jar:$AS_INSTALL/install/lib/external/jaxb/activation.jar:. MyClass.java
Run Code Online (Sandbox Code Playgroud)
然后我运行程序使用:
java -cp $AS_INSTALL/glassfish/modules/javax.mail.jar:$AS_INSTALL/install/lib/external/jaxb/activation.jar:. -Djava.util.logging.config.file=logging.properties MyClass
Run Code Online (Sandbox Code Playgroud)
这会导致以下错误:
Sep 22, 2011 4:19:25 PM MyClass main
SEVERE: This is a test
java.util.logging.ErrorManager: 3: SEVERE: no object DCH for MIME type multipart/mixed;
boundary="----=_Part_1_26867996.1316722766145"
javax.activation.UnsupportedDataTypeException: no …Run Code Online (Sandbox Code Playgroud) 我正在阅读来自emailServer的电子邮件并将其保存在数据库中.我正在使用以下代码从电子邮件服务器上的文件夹("INBOX")中读取邮件并检索与其对应的邮件但我得到了
"java.lang.ArrayIndexOutOfBoundsException: message number (621) out of bounds (620)
at com.sun.mail.imap.MessageCache.getMessage(MessageCache.java:123)
at com.sun.mail.imap.MessageCache.getMessageBySeqnum(MessageCache.java:153)
at com.sun.mail.imap.IMAPFolder.getMessageBySeqNumber(IMAPFolder.java:2795)
at com.sun.mail.imap.IMAPFolder.getMessagesByUID(IMAPFolder.java:1924)"
Run Code Online (Sandbox Code Playgroud)
我正在使用javax.mail.1.4.4,这个问题主要是在收件箱被淹没时出现的.
使用的代码:
folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
// messageUID is uid of last message I saved in DB
Message messages[] = ((UIDFolder) folder).getMessagesByUID(messageUID + 1, UIDFolder.LASTUID);
Run Code Online (Sandbox Code Playgroud)
我做了一些研究,发现一旦打开就为文件夹设置了messagecache,我们假设它被设置为520(文件夹的大小).如果在设置了消息缓存后到达任何消息,则在最后一个消息序列中,num超过了消息缓存的总大小,并引发异常.
任何人都可以让我知道如何获取文件夹中最后一条消息的UId的绝对值或如何获取文件夹锁定,以便在设置缓存后,文件夹不会更新文件夹的大小.
我正在使用Java Mail API,我正在尝试通过Gmail的SMTP发送电子邮件.我的程序如何工作:java.util.Scanner类用于获取用户输入 - 我要求用户在邮件发送类中使用各种参数; 执行以下操作:
Message mailMessage = new MimeMessage(session);
mailMessage.setFrom(new InternetAddress("example@example.com"));
mailMessage.setRecipients(Message.RecipientType.TO,InternetAddress.parse(mail.getTo()));
mailMessage.setSubject(mail.getSubject());
mailMessage.setText(mail.getMessage());
Transport.send(mailMessage);
Run Code Online (Sandbox Code Playgroud)
只要我使用ASCII符号/字符,一切都有效.但每当我想使用"特定国家"字符时 - 比如[õäöü] - 我会看到一堆奇怪的符号......
到目前为止我使用的技术(对我来说不起作用):
setHeader("Content-Type", "text/plain; charset=UTF-8");
setHeader("Content-Encoding","ISO-8859-9");
setContent(message, "text/plain; charset=iso-8859-2");
Run Code Online (Sandbox Code Playgroud)
注意:执行System.out.println()以显示要发送的消息时,IDE中的所有内容都会正确显示.
编辑:ex发送邮件正文时[õäöü]它在Gmail中显示[ä""?].
编辑:何时mailMessage.setText(MimeUtility.encodeText(mail.getMessage(), "UTF-8", "Q"));使用,然后Gmail中的输出如下:
"=?UTF-8?Q?=C3=A4=E2=80=9E=E2=80=9D=EF=BF=BD;=0D=0A?="
Run Code Online (Sandbox Code Playgroud)
另一个编辑:有趣的是,当我这样做时:mailMessage.setText(strVar + "õäöü", "ISO-8859-1");
它实际上在我的电子邮件中很好地附加了"õäöü"(但字符串的第一部分[strVar]仍然充满了?和[]的).
我试图从spring应用程序发送邮件.我已经包含了所有jar文件,但是它显示了这个异常:
javax.mail.MessagingException: Exception reading response;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1611)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1369)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
at javax.mail.Service.connect(Service.java:310)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
at javax.mail.Transport.send(Transport.java:118)
at org.symb.leavemanagement.mail.SampleMail.sentMail(SampleMail.java:65)
at org.symb.leavemanagement.controller.AdminController.processRegistratingUser(AdminController.java:196)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:774)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at …Run Code Online (Sandbox Code Playgroud) 我的目标:使用普通/文本,文本/ html和附件通过SMTP发送交易电子邮件.
我的代码:用JavaMail实现
我的问题:它在hotmail或outlook上看起来很好.但是在gmail上,如果它是带有.txt附件的电子邮件,它就不会正确显示邮件正文(如果附件是图像,它可以正常工作)
任何帮助将受到高度赞赏.
这是我的原始SMTP输出:
Subject: ALTERNATIVE | TXT | HTML |ATT.ATTACHMENT | Thu Jun 13 17:48:04 EDT
2013
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----=_Part_0_21791733.1371160084561"
------=_Part_0_21791733.1371160084561
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Body message in text format!
------=_Part_0_21791733.1371160084561
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
Body message in <b>html</b> format! Sent on Thu Jun 13 17:48:04 EDT 2013<br> to: me@gmail.com<br> to: me@mijo.com<br> cc: me@hotmail.com<br> cc: rafael.santos.test@hotmail.com
------=_Part_0_21791733.1371160084561
Content-Type: text/plain; charset=us-ascii; name=email_attachment.txt
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=email_attachment.txt
This is …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用JavaMail API向Gmail帐户发送电子邮件.我已经完成了以下代码.我想将邮件发送到多个配方.但是它无法正常工作.它给出了一个例外,例如"无法连接到SMTP主机.发送失败"
package com.cts.email;
import java.util.Properties;
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;
public class SendEmail {
Properties emailProperties;
Session mailSession;
MimeMessage emailMessage;
public static void main(String args[]) throws MessagingException, javax.mail.MessagingException {
SendEmail javaEmail = new SendEmail();
Session session=javaEmail.setMailServerProperties();
javaEmail.createEmailMessage(session);
// javaEmail.sendEmail();
}
public Session setMailServerProperties() {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() { …Run Code Online (Sandbox Code Playgroud) 这是一个特殊的问题。目的是通过SMTP发送Office365的邮件。
我能够始终如一地从本地笔记本电脑发送邮件。
但是,当部署在我们服务器上(位于防火墙之后)时,它不会成功。注意:smtp.office365.com可以在服务器上访问并确认端口587的端口。这是在我的本地计算机上成功运行的属性。
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.connectiontimeout", MAIL_TIMEOUT);
props.put("mail.smtp.timeout", MAIL_TIMEOUT);
props.put("mail.debug", true);
this.session = Session.getInstance(props);
session.setDebug(true);
Transport transport = session.getTransport();
transport.connect("smtp.office365.com", 587, email, pass);
Run Code Online (Sandbox Code Playgroud)
但是在服务器上失败。这是服务器调试日志:
DEBUG: setDebug: JavaMail version 1.6.2
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.office365.com", port 587, isSSL false
220 PN1PR0101CA0017.outlook.office365.com Microsoft ESMTP MAIL Service ready at Fri, 28 Jun 2019 06:39:41 +0000
DEBUG SMTP: connected to host "smtp.office365.com", port: …Run Code Online (Sandbox Code Playgroud)