Java教程

java: javamail 1.6.2 Create Receive Email using jdk 19

本文主要是介绍java: javamail 1.6.2 Create Receive Email using jdk 19,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

 

	    /**
	     * 接收邮件
	     * 中文是乱码 未解决
	     * @param pop3Host   pop.163.com
	     * @param storeType pop3
	     * @param user  geovindu@163.com
	     * @param password  geovindu
	     * @author geovindu
	     * @serialData@
	     * 
	     * 
	     * **/
	    public static void DuReceiveEmail(String pop3Host,String storeType, String user, String password){
	    	Properties props = new Properties();
	    	props.put("mail.pop3.host", pop3Host);
	    	props.put("mail.pop3.port", "110");
	    	props.put("mail.pop3.starttls.enable", "false");
	    	props.put("mail.store.protocol", "pop3");
	     
	    Session session = Session.getInstance(props);	
	    try {  
	    	Store mailStore = session.getStore(storeType);
	    	mailStore.connect(pop3Host, user, password);
	     
	    	Folder folder = mailStore.getFolder("INBOX");
	    	folder.open(Folder.READ_ONLY);
	     
	    	Message[] emailMessages = folder.getMessages();
	    	System.out.println("Total Message - " 
	    			+ emailMessages.length);
	     
	       //Iterate the messages
	      for (int i = 0; i < emailMessages.length; i++) {
	       Message message = emailMessages[i];
	       Address[] toAddress = 
	       message.getRecipients(Message.RecipientType.TO);
	       System.out.println();  
	       System.out.println("Email " + (i+1) + "-");  
	       System.out.println("Subject - " + message.getSubject());  
	       System.out.println("From - " + message.getFrom()[0]); 
	     
	       System.out.println("To - "); 
	       for(int j = 0; j < toAddress.length; j++){
	    	   System.out.println(toAddress[j].toString());
	       }
	       System.out.println("Text - " + 
	    		   message.getContent().toString());  
	      }
	     
	       folder.close(false);
	       mailStore.close();
	    } catch (Exception e) {
	        e.printStackTrace();
	        System.err.println("Error in receiving email.");
	        }        
	    }
    
    /**
     * 接收邮件
     * 中文是乱码 未解决
     * @param pop3Host   pop.163.com
     * @param storeType pop3 imap
     * @param user  geovindu@163.com
     * @param password  
     * @author geovindu
     * 
     * 
     * **/
    public static void receiveEmail(String pop3Host, String storeType, String user, String password) {  
    		  try {  
    		   //1) get the session object  
    		   Properties properties = new Properties();  
    		   properties.put("mail.pop3.host", pop3Host);  
    		   Session emailSession = Session.getDefaultInstance(properties);  
    		     
    		   //2) create the POP3 store object and connect with the pop server  
    		   POP3Store emailStore = (POP3Store) emailSession.getStore(storeType);  
    		   emailStore.connect(user, password);  
    		  
    		   //3) create the folder object and open it  
    		   Folder emailFolder = emailStore.getFolder("INBOX");  
    		   emailFolder.open(Folder.READ_ONLY);  
    		   //获取邮箱基本信息
   	       //   Map<String, Integer> map = DuMailHelper.emailInfo(emailFolder);
   	            //  System.out.println(map);
    		   //4) retrieve the messages from the folder in an array and print it  
    		   Message[] messages = emailFolder.getMessages();  
    		  // parseMessage(messages);
    		   
    		   for (int i = 0; i < messages.length; i++) {  
    		    Message message = messages[i];  
    		    System.out.println("---------------------------------");  
    		    System.out.println("Email Number " + (i + 1));  
    		    System.out.println("Subject: " + message.getSubject());  
    		    System.out.println("From: " + message.getFrom()[0]);  
    		    System.out.println("Text: " + message.getContent().toString());  
    		   }  
    		  
    		   //5) close the store and folder objects  
    		   emailFolder.close(false);  
    		   emailStore.close();  
    		  
    		  } catch (NoSuchProviderException e) {e.printStackTrace();}   
    		  catch (MessagingException e) {e.printStackTrace();}  
    		  catch (IOException e) {e.printStackTrace();}  
    	}  

  

 

https://javaee.github.io/javamail/FAQ#commonmistakes

https://www.geeksforgeeks.org/sending-email-java-ssltls-authentication/

https://stackoverflow.com/questions/13980922/java-mail-api-encoding-problems

https://www.go4expert.com/articles/send-receive-mails-java-javamail-t3649/

https://community.oracle.com/tech/developers/discussion/1590874/send-an-email-encoded-in-utf-8-with-javamail-character-problems

 

 

QQ邮箱的收取邮件支持POP/IMAP两种协议,发送邮件采用SMTP协议,收件和发件均使用SSL协议来进行加密传输,采用SSL协议需要单独对帐户进行设置。采用SSL协议和非SSL协议时端口号有所区别,参照下表的一些常见配置组合:

类型

服务器名称

服务器地址

非SSL协议端口号

SSL协议端口号

发件服务器

SMTP

smtp.qq.com

25

465/587

收件服务器

POP

pop.qq.com

110

995

收件服务器

IMAP

imap.qq.com

143

993

 

 

 

 

 

163邮箱的收取邮件支持POP/IMAP两种协议,发送邮件采用SMTP协议,收件和发件均使用SSL协议来进行加密传输,采用SSL协议需要单独对帐户进行设置。采用SSL协议和非SSL协议时端口号有所区别,参照下表的一些常见配置组合:

 

类型 服务器名称 服务器地址 SSL协议端口号 非SSL协议端口号
收件服务器 POP pop.163.com 995 110
收件服务器 IMAP imap.163.com 993 143
发件服务器 SMTP smtp.163.com 465/994 25

这篇关于java: javamail 1.6.2 Create Receive Email using jdk 19的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!