我正在尝试实现Java Mail servlet,第一步是连接到IMAP服务器.
我能够通过端口143(默认IMAP端口)telnet到服务器,telnet说: OK The Microsoft Exchange IMAP4 service is ready.
现在我尝试使用Java Mail API连接到服务器,如下所示:
Properties props = new Properties();
session = Session.getDefaultInstance(props, null);
store = session.getStore("imap");
store.connect("host","user","password");
Run Code Online (Sandbox Code Playgroud)
我能够使用现有的Outlook Webapp连接到同一台服务器,并使用与我在java中传递给它的相同凭据.
但运行此命令session.setDebug(true)会产生以下输出:
DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: trying to connect to host "myHost", port 143, isSSL false
* OK The Microsoft Exchange IMAP4 service is ready.
A0 CAPABILITY
* CAPABILITY …Run Code Online (Sandbox Code Playgroud) 我正在尝试在android中构建一个电子邮件客户端应用程序,现在我想配置javaMail部分.
我正在尝试与imap服务器建立连接,但我的代码有问题..这是我的代码:
package mailpackage;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
public class Connection implements Runnable
{
boolean done;
public Connection()
{
this.done=false;
}
@Override
public void run()
{
System.out.println("Hello from Connection Thread!");
while(!done)
{
String host = "myhost";// change accordingly
String mailStoreType = "imap";
String username = "myusername";// change accordingly
String password = "mypasswd";// change accordingly
check(host, mailStoreType, username, password);
}
}
public static void receiveEmail(String host, String storeType, String …Run Code Online (Sandbox Code Playgroud) 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:1764)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1523)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:453)
at javax.mail.Service.connect(Service.java:313)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at javaapplication5.SendMail.send(SendMail.java:77)
at javaapplication5.SendMailTest.main(SendMailTest.java:17)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(InputRecord.java:523)
at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:355)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:789)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1112)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:744)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:75)
at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:106)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:84)
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1742)
... 9 more
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我使用代理使用JavaMail API发送邮件吗?
我试图借助JavaMail API通过IMAP访问Gmail帐户中的电子邮件.我想知道为什么代码适用于一个电子邮件帐户但不适用于另一个.
我可以访问Inbox两个电子邮件帐户的文件夹.但对于其中一个电子邮件帐户,其他文件夹SPAM([Gmail]/Spam)无法访问,并引发FolderNotFoundException异常.有人可以解释一下出了什么问题吗?
先感谢您.
这是代码:
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.Flags.Flag;
import javax.mail.internet.*;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPMessage;
public class FolderFetchIMAP {
public static void main(String[] args) throws MessagingException, IOException {
IMAPFolder folder = null;
Store store = null;
String subject = null;
Flag flag = null;
try
{
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
store = session.getStore("imaps");
store.connect("imap.googlemail.com","myemailid@gmail.com", "password");
folder = (IMAPFolder) store.getFolder("[Gmail]/Spam"); // This doesn't work …Run Code Online (Sandbox Code Playgroud) 我必须用Java实现IMAP客户端.
使用Apache Commons Net库有哪些优势?它是否使实现更健壮,更灵活?
我如何处理返回值,它总是产生字符串.
例如:
public static void main(String[] args) throws Exception {
IMAPClient client = new IMAPClient();
client.connect(SERVER);
client.login(USERNAME, PASSWORD);
client.select("INBOX");
client.fetch("1", "body[header]");
}
Run Code Online (Sandbox Code Playgroud)
我们可以将输出定向到字符串
client.addProtocolCommandListener(new PrintCommandListener(System.out, true));
Run Code Online (Sandbox Code Playgroud)
但是,如何将文件夹列表作为文件夹实例而不是纯字符串输出?
我正在尝试在用户注册后发送确认电子邮件.我正在使用JavaMail库和Java 8 Base64 util类.
我正在以下列方式编码用户电子邮件:
byte[] encodedEmail = Base64.getUrlEncoder().encode(user.getEmail().getBytes(StandardCharsets.UTF_8));
Multipart multipart = new MimeMultipart();
InternetHeaders headers = new InternetHeaders();
headers.addHeader("Content-type", "text/html; charset=UTF-8");
String confirmLink = "Complete your registration by clicking on following"+ "\n<a href='" + confirmationURL + encodedEmail + "'>link</a>";
MimeBodyPart link = new MimeBodyPart(headers,
confirmLink.getBytes("UTF-8"));
multipart.addBodyPart(link);
Run Code Online (Sandbox Code Playgroud)
在哪里confirmationURL:
private final static String confirmationURL = "http://localhost:8080/project/controller?command=confirmRegistration&ID=";
Run Code Online (Sandbox Code Playgroud)
然后以这种方式在ConfirmRegistrationCommand中对此进行解码:
String encryptedEmail = request.getParameter("ID");
String decodedEmail = new String(Base64.getUrlDecoder().decode(encryptedEmail), StandardCharsets.UTF_8);
RepositoryFactory repositoryFactory = RepositoryFactory
.getFactoryByName(FactoryType.MYSQL_REPOSITORY_FACTORY);
UserRepository userRepository = repositoryFactory.getUserRepository();
User user = …Run Code Online (Sandbox Code Playgroud) 首先,我需要说发送1.2.0.RELEASE的电子邮件工作正常
application.properties:
spring.mail.host = smtp.gmail.com
spring.mail.username = *****@gmail.com
spring.mail.password = ****
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.socketFactory.port = 465
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = false
Run Code Online (Sandbox Code Playgroud)
pox.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.0.RELEASE</version>
<relativePath/>
</parent>
Run Code Online (Sandbox Code Playgroud)
.......
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
将父版本更改为1.2.5.RELEASE电子邮件发送无效
文档说: 如果spring.mail.host和相关库(由spring-boot-starter-mail定义)可用,则创建默认JavaMailSender(如果不存在).
所以我补充说
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
它没有帮助,然后我已经取而代之
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我也试过了
spring.mail.host = smtp.gmail.com
spring.mail.username = *****@gmail.com
spring.mail.password = ****
spring.mail.port = 465
Run Code Online (Sandbox Code Playgroud)
结果一样.
手动创建和配置@Bean不是问题.但是我想要使用Spring Boot的所有美感.
请指出我的错误.
提前致谢
我有一个类,它有一个在xml中定义的init方法
<bean id="appStarter" class="com.myapp.myClass" init-method="init" destroy-method="destroy"/>
Run Code Online (Sandbox Code Playgroud)
我的课:
public class myClass{
private Thread t;
public void init() {
t = new Thread() {
@Override
public void run() {
while (true)
try {
doStuff();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();
}
public void destroy() {
t.interrupt();
}
}
Run Code Online (Sandbox Code Playgroud)
当应用程序启动时,这些线程运行正常,一切正常,一段时间后我得到以下异常.
INFO: Illegal access: this web application instance has been stopped already. Could not load com.sun.mail.imap.IMAPStore. The eventual following stack trace is caused by an error thrown for debugging …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试使用JavaMail从IMAP服务器(Gmail和其他人)获取电子邮件.基本上,我的代码工作:我确实可以得到标题,正文内容等.我的问题如下:当处理IMAP服务器(没有SSL)时,处理消息基本上需要1-2ms.当我使用IMAPS服务器(因此使用SSL,例如Gmail)时,我的消息达到250米左右.我只测量处理消息的时间(不考虑连接,握手等).
我知道,因为这是SSL,所以数据是加密的.但是,解密的时间不应该那么重要,不是吗?
我已经尝试设置更高的ServerCacheSize值,更高的connectionpoolsize,但我的想法很严重.谁有人遇到这个问题?有人可能希望解决它吗?
我担心JavaMail API每次从IMAPS服务器获取邮件时都会使用不同的连接(涉及握手的开销......).如果是这样,有没有办法覆盖这种行为?
这是从Main()类调用的代码(虽然非常标准):
public static int connectTest(String SSL, String user, String pwd, String host) throws IOException,
ProtocolException,
GeneralSecurityException {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", SSL);
props.setProperty("mail.imaps.ssl.trust", host);
props.setProperty("mail.imaps.connectionpoolsize", "10");
try {
Session session = Session.getDefaultInstance(props, null);
// session.setDebug(true);
Store store = session.getStore(SSL);
store.connect(host, user, pwd);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
int numMess = inbox.getMessageCount();
Message[] messages = inbox.getMessages();
for (Message m : messages) {
m.getAllHeaders();
m.getContent();
}
inbox.close(false);
store.close();
return numMess;
} catch (MessagingException e) {
e.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 目前我正在编写一个将要监听目录的代码.当使用.apk文件更新目录时,我会将带有此.apk文件的邮件发送到gmail帐户.我在我的程序中使用Jnotify和JAVA Mail.
我得到的错误是,
javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; boundary="----=_Part_0_145238.1392728439484"
Run Code Online (Sandbox Code Playgroud)
我在stackoverflow中寻找了解决方案以寻求帮助,但没有一个在哪里有用.
提前致谢
public void fileCreated(int wd, String rootPath, String name) {
print("created " + rootPath + " : " + name);
if (name.contains(".apk"))
SendEmail(name);
else
System.out.println("Not the APK file");
}
void SendEmail(String strname){
String Path = "D:/POC/Email/apk folder/"+strname;
System.out.println("Path->" + Path);
Properties props = new Properties();
props.put("mail.smtp.host","173.194.78.108");
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");
System.out.println("Properties has been set properly");
Session session = …Run Code Online (Sandbox Code Playgroud) jakarta-mail ×10
java ×10
email ×4
imap ×4
ssl ×2
base64 ×1
gmail ×1
ioexception ×1
java-ee ×1
jnotify ×1
pkix ×1
registration ×1
spring ×1
spring-boot ×1
tomcat ×1