javamail的java测试用例
以下是发送电子邮件的代码:javamail的java测试用例,java,email,junit4,Java,Email,Junit4,以下是发送电子邮件的代码: 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 =
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版本1.3.1
调试:getProvider()返回javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,sun Microsystems,Inc]
调试SMTP:useEhlo true,useAuth false
调试SMTP:尝试连接到主机“XXXX”,端口25
220 XXXXX ESMTP赛门铁克消息网关
调试SMTP:已连接到主机“XXXXX”,端口:25
EHLO XXXXX
250-XXXXX向XXXXX表示EHLO
250-STARTTLS
250-8比特
250号262144000
250-增强状态码
250流水线
调试SMTP:找到扩展名“STARTTLS”,arg“”
调试SMTP:找到扩展名“8BITMIME”,参数“”
调试SMTP:找到扩展名“SIZE”,arg“262144000”
调试SMTP:找到扩展名“ENHANCEDSTATUSCODES”,arg“”
调试SMTP:找到扩展名“管道”,arg“”
调试SMTP:use8bit false
邮寄地址:
250 2.0.0已接受来自的邮件
RCPT至:
250 2.0.0 RCPT待验收
调试SMTP:已验证的地址
调试SMTP:xxxxxx
资料
354 3.0.0继续。以“\r\n”结束。\r\n
消息ID:
发件人:xxxxxx
收件人:xxxxxxx
主题:xxxxxxx
Mime版本:1.0
内容类型:文本/纯文本;字符集=美国ascii码
内容传输编码:7bit
xxxxxxx
.
250 2.0.0正常91/8D-04168-0F919785
退出
“这里有一个类,它定义了从gmail收件箱中检索电子邮件的获取方法 必须添加以下两个库才能编译代码 1-java激活库,可从此处下载 2-可从此处下载的java邮件库
import java.io.BufferedOutputStream;
导入java.io.BufferedReader;
导入java.io.DataOutputStream;
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.Date;
导入java.util.Properties;
导入javax.mail.Address;
导入javax.mail.Folder;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.Multipart;
导入javax.mail.NoSuchProviderException;
导入javax.mail.Part;
导入javax.mail.Session;
导入javax.mail.Store;
公共类获取电子邮件{
publicstaticvoidfetch(字符串pop3Host、字符串storeType、字符串user、,
字符串(密码){
试一试{
//创建属性字段
属性=新属性();
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”);
会话emailSession=Session.getDefaultInstance(属性);
//emailSession.setDebug(true);
//创建POP3存储对象并连接pop服务器
Store Store=emailSession.getStore(“pop3s”);
store.connect(pop3Host、用户、密码);
//创建文件夹对象并将其打开
文件夹emailFolder=store.getFolder(“收件箱”);
emailFolder.open(文件夹只读);
//从数组中的文件夹中检索邮件并打印
Message[]messages=emailFolder.getMessages();
System.out.println(“messages.length----”+messages.length);
for(int i=0;i
现在您有了这个类-在for循环中添加代码,以您想要的方式处理每条消息。然后调用fetch方法,如下所示
fetch(“pop.gmail.com”,“pop3”,用户名,密码)代码>如果发送失败,它应该会经历一个错误,所以只需将其包装在一个try-and-catch语句中即可成功发送。我需要使用session.getStore()检索它;但如何检索smtp主机的存储对象。换句话说,您需要代码从收件箱读取电子邮件?是的,我需要代码读取电子邮件。javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,sun Microsystems,Inc]是提供程序。如何在上述代码不变的情况下连接到imap或pop3,因为我正在使用smtp主机。是否可以连接到imap或pop3存储并在INBOXOk中获取邮件,我发布了正确的代码以从Java中的gmail帐户检索邮件。我没有收到邮件。我的gmail帐户设置是否需要进一步更改?是的,您需要在gmail设置中启用pop
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
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();
}
}
}