我正在尝试自动在网站上注册,但是该网站将OTP发送到电子邮件。因此我需要从邮件中提取OTP并将其打印到文本字段。
我正在使用此代码,但未显示未读消息计数和OTP验证邮件。
任何人都可以帮助我从电子邮件中提取OTP并将其提交到网站上吗?
package MAVEN.GmailIMAP;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
public class Gmail3 {
public static void ReceiveMail(String FolderName,String SubjectContent, String emailContent, int lengthOfOTP){
String hostName = "imap.gmail.com";//change it according to your mail
String username = "[email protected]";//username
String password = "test12345"; //password
int messageCount;
int unreadMsgCount;
String emailSubject;
Message emailMessage;
String searchText=null ;
Properties sysProps = System.getProperties();
sysProps.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getInstance(sysProps, null);
Store store = session.getStore();
store.connect(hostName, username, password);
Folder emailBox = store.getFolder(FolderName);
emailBox.open(Folder.READ_ONLY);
messageCount = emailBox.getMessageCount();
System.out.println("Total Message Count: " + messageCount);
unreadMsgCount = emailBox.getNewMessageCount();
System.out.println("Unread Emails count:" + unreadMsgCount);
for(int i=messageCount; i>(messageCount-unreadMsgCount); i--)
{
emailMessage = emailBox.getMessage(i);
emailSubject = emailMessage.getSubject();
if(emailSubject.contains(SubjectContent))
{
System.out.println("OTP mail found");
String line;
StringBuffer buffer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(emailMessage.getInputStream()));
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String messageContent=emailContent;
String result = buffer.toString().substring(buffer.toString().indexOf(messageContent));
searchText = result.substring(messageContent.length(), messageContent.length()+lengthOfOTP);
System.out.println("Text found : "+ searchText);
emailMessage.setFlag(Flags.Flag.SEEN, true);
break;
}
emailMessage.setFlag(Flags.Flag.SEEN, true);
}
emailBox.close(true);
store.close();
} catch (Exception mex) {
mex.printStackTrace();
System.out.println("OTP Not found ");
}
return searchText ;
}
public static void main(String[] args) {
ReceiveMail("FolderName","SubjectContent","One Time Password (OTP):",6);
}
}
下面的电子邮件正文扫描器应可用于所需的输入内容
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class OtpRegex {
public static void main(String[] args) {
// String regex = "((is|otp|password|key|code|CODE|KEY|OTP|PASSWORD|Do).[0-9]{4,8}.(is|otp|password|key|code|CODE|KEY|OTP|PASSWORD)?)|(^[0-9]{4,8}.(is|otp|password|key|code|CODE|KEY|OTP|PASSWORD))";
String text = "To auth... \n\n869256\n\n Do is your login OTP. Treat this as confidential. Sharing it with anyone gives them full access to your Paytm Wallet. Paytm never calls to verify your OTP.";
Scanner sc = new Scanner(text);
int otp = -1;
String line = null;
while (sc.hasNextLine()) {
line = sc.nextLine().trim();
try{
otp = Integer.parseInt(line);
break;
} catch (NumberFormatException nfx) {
//Ignore the exception
}
}
System.out.println(otp);
}
}