以下是发送电子邮件的代码:
Properties properties = new Properties();
String host = "XXXXXX";
String from = "XXXXXX";
String to = "XXXXXX";
String content = "xxxxxxxx";
String subject = "xxxxxxxx";
properties.setProperty("mail.smtp.host", host);
Session session = Session.getInstance(properties, null);
Mailbox mailbox = Mailbox.get(to);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(content);
session.setDebug(true);
Transport.send(message);
现在我想知道如何从Gmail帐户中检索电子邮件并能够阅读它们?我还想知道如何测试我的电子邮件是否成功发送?
以下是调试内容: “
DEBUG: setDebug: JavaMail version 1.3.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "XXXX", port 25
220 XXXXX ESMTP Symantec Messaging Gateway
DEBUG SMTP: connected to host "XXXXX", port: 25
EHLO XXXXX
250-XXXXX says EHLO to XXXXX
250-STARTTLS
250-8BITMIME
250-SIZE 262144000
250-ENHANCEDSTATUSCODES
250 PIPELINING
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "262144000"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<xxxxxx>
250 2.0.0 MAIL FROM accepted
RCPT TO:<xxxxxxx>
250 2.0.0 RCPT TO accepted
DEBUG SMTP: Verified Addresses
DEBUG SMTP: xxxxxx
DATA
354 3.0.0 continue. finished with "\r\n.\r\n"
Message-ID: <942986815.1484331504128.JavaMail.XXXXXX>
From: xxxxxx
To: xxxxxxx
Subject: xxxxxxx
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
xxxxxxx
.
250 2.0.0 OK 91/8D-04168-0F919785
QUIT
答案 0 :(得分:0)
这是一个定义了从gmail收件箱中检索电子邮件的获取方法的类。
必须添加以下两个库才能编译代码。
1- java激活库,可以从这里下载http://www.oracle.com/technetwork/java/javase/downloads/index-135046.html
2-可从此处下载的https://java.net/projects/javamail/pages/Home
的java邮件库 import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
public class FetchingEmail {
public static void fetch(String pop3Host, String storeType, String user,
String password) {
try {
// create properties field
Properties properties = new Properties();
properties.put("mail.store.protocol", "pop3");
properties.put("mail.pop3.host", pop3Host);
properties.put("mail.pop3.port", "995");
properties.put("mail.pop3.starttls.enable", "true");
Session emailSession = Session.getDefaultInstance(properties);
// emailSession.setDebug(true);
// create the POP3 store object and connect with the pop server
Store store = emailSession.getStore("pop3s");
store.connect(pop3Host, user, password);
// create the folder object and open it
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
// retrieve the messages from the folder in an array and print it
Message[] messages = emailFolder.getMessages();
System.out.println("messages.length---" + messages.length);
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
//now over here you can process the message object in any way you like
}
// close the store and folder objects
emailFolder.close(false);
store.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
现在你有了这个类 - 在for循环中添加代码,以你想要的方式处理每个消息。然后按以下方式调用fetch方法fetch("pop.gmail.com", "pop3", username, password);