Tag: javamail

如何使用POP3检索gmail子文件夹/标签?

下面的代码使用javamail API来访问gmail, String host = “pop.gmail.com”; int port = 995; Properties properties = new Properties(); properties.put(“mail.smtp.socketFactory.class”, “javax.net.ssl.SSLSocketFactory”); final javax.mail.Session session = javax.mail.Session.getInstance(properties); store = session.getStore(“pop3s”); store.connect(host, port, mCredentialaNme, mCredentialApss); // *************************************************************** Folder personalFolders[] = store.getDefaultFolder().list( “*” ); // *************************************************************** for (Folder object : personalFolders) { // *********************************** System.out.println( object.list(“*”) ); // *********************************** if ((object.getType() & […]

无法通过classLoader.getResourceAsStream()从WEB-INF文件夹中检索图像

中午我试图让我的应用程序通过javamail发送html +图像,我只设法发送html,但与图像我有一些问题。 我决定创建一个多部分消息,一切顺利,但后来我使用类加载器从WEB-INF / resources / images检索.png文件我得到一个NullPointerExcetion,我不知道为什么会这样? 这是我的EJB(3.0)的样子。 我很欣赏这一个我没有太多经验的ClassLoader类(不太了解它)。 @Stateless(name = “ejbs/EmailServiceEJB”) public class EmailServiceEJB implements IEmailServiceEJB { @Resource(name = “mail/myMailSession”) private Session mailSession; public void sendAccountActivationLinkToBuyer(String destinationEmail, String name) { // Destination of the email String to = destinationEmail; String from = “dontreply2thismessage@gmail.com”; try { Message message = new MimeMessage(mailSession); // From: is our […]

JavaMail SSL,无需认证信任证书

我有一个本地邮件服务器(hMailServer)与SSL(端口465)和一个自签名证书。 域名是“foobar.com” 我已经设置了我的Properties来启用ssl,禁用auth,并信任任何主机 props.put(“mail.smtp.auth”, “false”); props.put(“mail.smtp.ssl.enable”, “true”); props.put(“mail.smtp.ssl.trust”, “*”); 如果我通过静态调用将消息发送到Transport.send() ,则会发送电子邮件。 如果我尝试从会话中获取transport实例,那么我得到 javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 静态调用如何避免SSLHandshakeException? 这是我的测试代码: public static void main(String[] args) throws Exception { Properties props = new Properties(); props.put(“mail.smtp.host”, “127.0.0.1”); props.put(“mail.debug”, “false”); props.put(“mail.smtp.port”, “465”); props.put(“mail.smtp.timeout”, “60000”); props.put(“mail.smtp.auth”, “false”); props.put(“mail.smtp.sendpartial”, “true”); […]

JavaMail问题:无法向SMTP主机发送命令

我正在尝试做java邮件,我得到一个错误“无法向SMTP主机发送命令”。 任何帮助,将不胜感激。 如有可能,任何未来的问题解决方案。 确切的例外是 javax.mail.MessagingException: Can’t send command to SMTP host; nested exception is: java.net.SocketException: Connection closed by remote host at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2106) at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2093) at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:1184) at javax.mail.Transport.send0(Transport.java:197) at javax.mail.Transport.send(Transport.java:124) at TestEmail.main(TestEmail.java:45) at __SHELL17.run(__SHELL17.java:6) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at bluej.runtime.ExecServer$3.run(ExecServer.java:774) Caused by: java.net.SocketException: Connection closed by remote host at com.sun.net.ssl.internal.ssl.SSLSocketImpl.checkWrite(SSLSocketImpl.java:1186) […]

使用MS Exchange的JavaMail:服务器和客户端均不支持身份validation机制

我几天来一直试图从Grails应用程序发送邮件但没有成功。 我在用着: grails 1.3.7 邮件1.0插件 spring-security-core 1.2.6插件 tomcat 7.0.23 Specifficaly我正在尝试使用部署在Tomcat服务器上的应用程序发送邮件,该端口25没有身份validation,没有SSL。 我已经尝试从部署了应用程序的VMWare虚拟机发送带有telnet的消息并且它已经通过了。 这是我发送邮件的课程: public boolean sendMessage(String to, String msgSubject, String msgText) { String host = “mail.mydomain.com”; String username = “myuser@mydomain.com”; // your authsmtp username String password = “mypassword” // your authsmtp password String from = “myuser@mydomain.com”; Properties props = System.getProperties(); props.put(“mail.smtp.host”, host); props.put(“mail.smtp.user”, username); props.put(“mail.smtp.password”, password); […]

在JavaMail中使用message.getFrom()时,仅显示要显示的电子邮件地址

目前,当使用JavaMail时,如果我使用getFrom()将消息分解为单独的部分,则getFrom()也将显示发送者的名称。 这可能是一个简单的问题,但是如何制作它只会返回电子邮件地址。 对不起,如果这是一个简单的问题,但我似乎无法找到答案。

无法使用SMTP发送电子邮件(获取javax.mail.MessagingException:无法将套接字转换为TLS;)

我编写了以下代码,使用javamail API通过SMTP作为TLS发送电子邮件,因为不支持SSL,但我最终得到以下exception。 请参阅下面的代码。 我使用过调试模式,在代码下面你也可以找到exception。 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 SendMailTLS { public static void main(String[] args) { final String username = “———@mydomain.com”; final String password = “***********”; Properties props = new Properties(); props.put(“mail.smtp.auth”, “true”); props.put(“mail.smtp.starttls.enable”, “true”); props.put(“mail.smtp.host”, “mail.mydomain.com”); props.put(“mail.smtp.debug”, “true”); props.put(“mail.smtp.port”, “587”); […]

Javamail性能

我一直在使用javamail从IMAP服务器(目前是GMail)检索邮件。 Javamail非常快速地从服务器检索特定文件夹中的消息列表(仅ID),但是当我实际获取消息(仅包含信息甚至不包含内容)时,每条消息大约需要1到2秒。 应该使用哪些技术进行快速检索? 这是我的代码: try { IMAPStore store = null; if(store!=null&&store.isConnected())return; Properties props = System.getProperties(); Session sessionIMAP = Session.getInstance(props, null); try { store = (IMAPStore) sessionIMAP.getStore(“imaps”); store.connect(“imap.gmail.com”,993,”username@gmail.com”,”password”); } catch (Exception e) { e.printStackTrace(); } IMAPFolder folder = (IMAPFolder) store.getFolder(“INBOX”); folder.open(Folder.READ_ONLY); System.out.println(“start”); Message[] msgs = folder.getMessages(1,10); long ftime = System.currentTimeMillis(); FetchProfile fp=new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); folder.fetch(msgs, fp); […]

如何使用javamail将base64图像嵌入到电子邮件中

我正在尝试使用嵌入式base64图像从javamail发送电子邮件(img alt =’image PNG’src =’data:image / png; base64,iVBORw0KGgoAAAANSUhEUgA … AElFTkSuQmCC’“) 它使用的是小图像,但是当图像较大时,图像不会显示在莲花笔记中。 这是代码的一部分 Transport transport = mailSession.getTransport(); MimeMessage message = new MimeMessage( mailSession ); message.setSubject( subject ); message.setFrom( new InternetAddress( me) ); message.setContent( bodyWithEmbeddedBase64Image, “text/html” ); transport.connect(); transport.sendMessage( message, message.getAllRecipients() ); transport.close();` 我想用PreencodedMimeBodyPart来测试它,但我不知道如何使用它有人可以帮我吗:)?

使用JavaMail阅读阿拉伯语附件

我有一个使用java邮件下载阿拉伯语附件的问题。 文件名始终不明确。 问题是Bodypart将附件检索为非UTF-8字符。 private void getAttachments(Message temp) throws IOException, MessagingException { List attachments = new ArrayList(); Multipart multipart = (Multipart) temp.getContent(); System.out.println(multipart.getCount()); for (int i = 0; i =0) { bos.write(aByte); } fos.flush(); bos.flush(); bos.close(); bis.close(); fos.close(); } // end of try() catch (IOException exp) { System.out.println(“IOException:” + exp); } attachments.add(f); } }