SubethaSmtp示例

时间:2017-07-28 19:21:28

标签: spring-boot incoming-mail

您能否告诉我如何使用 SubethaSmtp 库?

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

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

我研究了大部分的API文档,但是我无法将各个部分放在一起以使事情有效。

你能告诉我一个关于spring-boot的工作示例吗?

以下是我目前的代码

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 ... 
 }
 }

2 个答案:

答案 0 :(得分:1)

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());

              }
          }
    }

答案 1 :(得分:-1)

here

提供了使用SubEtha SMTP接收电子邮件的完整示例(如果您还使用Spring Boot,则可能可用)。