Joke Collection Website - Public benefit messages - Mail sender written in java

Mail sender written in java

Just write your own interface and pass in the relevant parameters. I have written this before. Use javamail. There is a main way to test your email, and there should be no problem. I hope I can help you. Pay attention to import the javamail.jar package you need- Import java.util.properties; Import javax.mail.address; Import javax.mail.flags; Import javax.mail.folder; Import javax.mail.message; import javax . mail . messaging exception; import javax . mail . nosuchproviderexception; Import javax.mail.session; Import javax.mail.store; Import javax.mail.transport; Import javax.mail.internet.addressexception; Import javax.mail.internet.internetaddress; Import javax.mail.internet.mimemessage; public class TestMail { public static void main(String【】args){//TestMail . sendmail(); //testmail . receive mail(); testmail . delete mail(); }/* * * Send mail */public static void sendmail () {stringhost = "smtp.sina.com"; //mail server stringfrom = "xingui5624 @ sina.com"; //Sender address stringto = "I love numen @ vip.sina.com"; //Accept the address (pop3 protocol must be supported) String username = "xingui5624"; //Sender's email name Stringpwd = "* * * *"; //Sender's email password Propels = NewProperties (); Props.put ("mail.smtp.host", host); props . put(“mail . SMTP . auth“,“true“); session session = session . getdefaultinstance(props); session . set debug(true); MimeMessage msg = new MimeMessage (session); Try {msg.setfrom (new internet address (from)); Msg.addRecipient (message. RecipientType.TO,new internet address(TO)); //Send msg.setSubject ("My test. . . . . . . “); //email subject msg.setText ("Test content. . . . . . . "); //Message content msg.savechanges (); transport transport = session . get transport(“SMTP“); Transport.connect (host, user name, password); //The mail server verifies transport. sendmessage(msg, msg.getrecipients (message.recipienttype.to)); System.out.println ("Sending succeeded. . . . . . . . . . . . . . . . “); } catch(address exception e){ e . printstacktrace(); } catch(messaging exception e){ e . printstacktrace(); }}/* * receive mail */public static void receive mail () {stringhost = "pop3.sina.com"; string userName =“xingui 5624“; string passWord =“* * * * * * *“; Properties props = new Properties(); session session = session . getdefaultinstance(props); session . set debug(true); Try {system. out.println ("receive. . . . . . . . . . . . . . . . . . . . . . . . . . . . “); store store = session . getstore(“pop 3“); Store.connect (host, user name, password); //verify folderfolder = store.getfolder ("inbox"); //get the receiving folder folder. READ _ WRITE); message msg【】= folder . get messages(); System.out.println ("number of messages:"+msg.length "); for(int I = 0; I & ltmsg. Length; i++){ Message Message = msg【I】; address address【】= message . get from(); string buffer from = new string buffer(); /* * * This for loop is used for my project test */for (int j = 0; J< address. Length; j++){ if(j & gt; 0)from . append“; “); From.append (address [j]. toString()); } system . out . println(message . getmessagenumber()); system . out . println(“from:“+from . tostring()); system . out . println(“size:“+message . getsize()); system . out . println(“subject:“+message . get subject()); system . out . println(“time::“+message . getsendate()); system . out . println(“= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =“; } folder . close(true); //set to closestore.close (); System.out.println ("End of reception. . . . . . . . . . . . . . . . . . . . . . . . . . . “); } catch(NoSuchProviderException e){ e . printstacktrace(); } catch(messaging exception e){ e . printstacktrace(); } }/* * * delete mail */public static void delete mail(){ String host =“pop 3 . Sina . com“; string userName =“xingui 5624“; string passWord =“* * * * * * *“; Properties props = new Properties(); //Properties props = system . get Properties(); This method creates the above property: Session Session = Session. getDefaultInstance(props); session . set debug(true); Try {system. out.println ("begin delete. . . . . . . “); store store = session . getstore(“pop 3“); Store.connect (host, user name, password); //Verify that the mailbox folder = store.getfolder ("inbox"); Folder.open (folder. READ _ WRITE); //Set my reading and writing mode to open int count of all = folder. getmessagecount(); //get the number of messages in int readcount = folder.getunreadmessagecount (); //read number int newofcount = folder. getnewmessagecount(); //Unread number System.out.println ("Total number:"+count of all "; system . out . println(“number read:“+un read count“); System.out.println ("unread number:"+new of count "); for(int I = 1; I< = count of Alli++) {message message = folder.getmessage (i); Message.setFlag (flag. Flag.DELETED,true); //Set the deleted status to true if (message. Isset (logo. Flag. Deleted) system. Out.println ("+I+"message has been deleted. . . . . . . . . "); } folder . close(true); store . close(); System.out.println ("Delete succeeded. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ); } catch(NoSuchProviderException e){ e . printstacktrace(); } catch(messaging exception e){ e . printstacktrace(); }}/* * * reply mail */public static void reply mail () {//test}} Note: This implementation requires all mailboxes to support pop3 and smtp protocols. Now all the old Netease mailboxes are supported (registered before 2006), so Sina's qq can support it, and some Yahoo's support can be found online. = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = It's simple. First, download the JavaMail framework from Sun's website. The latest version is 1.4. 1. Then there is the JavaBeans activation framework, the latest version of which is 1. 1. 1. JavaMail needs this framework. However, if JDK is 1.6, you don't have to fall. 1.6 already contains the JavaBeans activation framework. Finally, download the latest general e-mail version 1. 1 from mons.apache.org/email/,. First, build a new Java project in Eclipse, then decompress JavaMail and Common Email, and add references to all decompressed jars in the project. Code: packagerorg.fourpane.mail; Import org.apache.commons.mail.emailexception; import org . Apache . commons . mail . html email; public class Mail { public static void main(String【】args){//simple email email = new simple email(); html email email = new html email(); email . set hostname(“SMTP . 163 . com“); //mail server email.setauthentication ("xingui5624", * * * * * "); //smtp authenticated user name and password try {email.addto ("xingui5624 @163.com"); //Recipient email.setfrom ("xingui5624 @126.com", "* * * *"; //senderemail.setsubject ("xingui5624 test email"); //Title email . setcharset(“UTF-8“); //encoded format email.setMsg ("This is the test email of xingui5624"); //content email . send(); //Send System.out.println ("Sending succeeded. . . . . . “); } catch(email exception e){ e . printstacktrace(); }}} If the sending is unsuccessful, there may be something wrong with your jar package, and the jar of javamail may conflict with the jar of j2ee above jdk 1.5. Your mailbox may not support pop3 and smtp protocols.

Seek adoption