SubethaSmtp example



你能告诉我如何使用SubethaSmtp库吗?

<dependency>
<groupId>org.subethamail</groupId>
<artifactId>subethasmtp</artifactId>
<version>3.1.7</version>
<scope>test</scope>
</dependency>

我只想从我的Gmail/outlook收件箱中收听端口号上的电子邮件,并将它们显示在控制台窗口中/将它们记录在文本文件/DB中。

我研究了大部分 API 文档,但我无法将这些部分放在一起以使事情正常工作。

你能告诉我弹簧靴的一个工作示例吗?

这是我到目前为止下面的代码

package emailbox;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.subethamail.smtp.helper.SimpleMessageListenerAdapter;
import org.subethamail.smtp.server.SMTPServer;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@SuppressWarnings("restriction")
@PropertySource("/emailbox.properties")
@Service
public class SMTPServerService {
@Value("${smtpserver.enabled}")
String enabled="";
@Value("${smtpserver.hostName}")
String hostName="";
@Value("${smtpserver.port}")
String port="";
SMTPServer smtpServer;
public SMTPServerService() {
}
@PostConstruct 
public void start() { 
if(enabled.equalsIgnoreCase("true"))
{ 
SimpleMessageListenerImpl l = new SimpleMessageListenerImpl(); 
smtpServer = new SMTPServer(new SimpleMessageListenerAdapter(l)); 
smtpServer.setHostName(this.hostName); 
smtpServer.setPort(Integer.valueOf(port)); 
smtpServer.start(); 
System.out.println("****** SMTP Server is running for domain "+smtpServer.getHostName()+" on port "+smtpServer.getPort()); 
} else { 
System.out.println("****** SMTP Server NOT ENABLED by settings "); 
} 
}
@PreDestroy 
public void stop() { 
if(enabled.equalsIgnoreCase("true")){ 
System.out.println("****** Stopping SMTP Server for domain "+smtpServer.getHostName()+" on port "+smtpServer.getPort()); 
smtpServer.stop(); 
} 
}
public boolean isRunning() {
return smtpServer.isRunning();
}
}

package emailbox;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import org.subethamail.smtp.helper.SimpleMessageListener;
public class SimpleMessageListenerImpl implements SimpleMessageListener {
public SimpleMessageListenerImpl() {
}
@Override
public boolean accept(String from, String recipient) {
return true;
}
@Override 
public void deliver(String from, String recipient, InputStream data) { 
Session session = Session.getDefaultInstance(new Properties()); 
MimeMessage m = new MimeMessage(session,data); 
ReceivedEmail email=new ReceivedEmail(m); 
// ... here we go with email message ... 
}
}

Subetha SMTP 用于构建 SMTP 服务器。 为此,您应该使用 POP 或 IMAP 来阅读收件箱中的电子邮件。 请看下面的例子:你只需要java mail api。

public void receiveMail(){
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imap");
props.put("mail.imap.host", host);
props.put("mail.imap.port", port);
props.put("mail.imap.auth.login.disable", "true");
props.put("mail.imap.auth.plain.disable", "false");
//props.put("mail.imap.ssl.enable", "true");
props.put("mail.debug", "true");
props.put("mail.debug.auth", "true");

Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pwd);
}
});
store = (IMAPStore) session.getStore("imap");

store.connect(host, user, pwd);
folder = (IMAPFolder) store.getFolder("INBOX"); 
openFolder(folder);
}
public static void openFolder(IMAPFolder folder) throws MessagingException, IOException{
String subject = null;
if (!folder.isOpen())
folder.open(Folder.READ_WRITE);
Message[] messages = folder.getMessages();
System.out.println("No of Messages : " + folder.getMessageCount());
System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount());
System.out.println(messages.length);
if (messages.length > 0) {
for (int i = 0; i < 1; i++) {
System.out.println("*****************************************************************************");
System.out.println("MESSAGE " + (i + 1) + ":");
Message msg = messages[i];
System.out.println("Message Number: " + msg.getMessageNumber());
// Object String;
System.out.println("UID: " + folder.getUID(msg));
subject = msg.getSubject();
System.out.println("Subject: " + subject);
System.out.println("From: " + msg.getFrom()[0]);
System.out.println("To: " + msg.getAllRecipients()[0]);
System.out.println("Date: " + msg.getReceivedDate());
System.out.println("Size: " + msg.getSize());
System.out.println(msg.getFlags());
System.out.println("Body: n" + msg.getContent());
System.out.println(msg.getContentType());
}
}
}

使用SubEtha SMTP接收电子邮件的完整示例(如果您也使用Spring Boot(可在此处获得

相关内容

  • 没有找到相关文章

最新更新