San Francisco, , United States
Core Java, Android, HTML
0 टिप्पणी करें | 27 लोगो ने देखा है | 20 सितम्बर 13  | Mohammad Naseem Ashraf
I had played around with JavaMail API last year for a competition while I was in college where my team was assigned to build a system which would send bulk mail customized by the sender to a list of recipents as per their age and gender. I built the core messaging system in fifteen minutes or so, thanks to JavaMail Though JavaMail is said to be more complicated and low-level than any most other Java APIs around. I found sending a simple email pretty easy. The entire source code which is comfortably small can be divided into specific part 1 Properties of E-Mail Service Here, I39ve setup the properties initializing them in the Properties object - java.util.Properties, provided by the Utility Class - java.util. Properties props new Properties props.putquotmail.smtp.hostquot, quotsmtp.gmail.comquot props.putquotmail.smtp.socketFactory.portquot, quot465quot props.putquotmail.smtp.socketFactory.classquot, quotjavax.net.ssl.SSLSocketFactoryquot props.putquotmail.smtp.authquot, quottruequot props.putquotmail.smtp.portquot, quot465quot 2 Starting the Session by logging in using the Authenticator from JavaMail. Session session Session.getDefaultInstanc props,new javax.mail.Authenticator protected PasswordAuthentication getPasswordAuthentication return new PasswordAuthenticationtargetemail,password Here, you39ll need to enter your EMail service provider Username with the postfix yourhost.com and the password. I used a Class attribute to keep the password, sender email ID and the recipents email address. public static String password quotmypasswordquot public static String senderemail quotiam.legend.ngmail.comquot public static String recipentemail quotmd.naseem.ashrafgmail.comquot A better way of fashioning this code is by setting up these attributes as private and keeping an Arraylist of recipent39s email address which would be fetched, altered and mainteained separately. 3 Sending the MIME Message the actual E-Mail message try Message message new MimeMessagesession message.setFromnew InternetAddresssenderemail message.setRecipientsMessage.RecipientType.TO,InternetAddress.parserecieveremail message.setSubjectquotEMail Subjectquot message.setTextquotCongratulationsquot quotnYour EMail has been successfuly recieved by this EMail client.quot Transport.sendmessage System.out.printlnquotMessage Sentquot catch MessagingException e throw new RuntimeExceptione This block of code is self-explanatory. Some specific options I39d like to talk about, required to customize our E-Mails are 1. message.addHeaderString .... Useful to add headers to E-Mails which are used by E-Mail Clients to sort, classify and identify emails. This is pretty useful now a days with GMail39s new layout where E-Mails are classified as Standard, Social Group, Mailing Lists etc and Promotions Advertisements under respective tabs. Though I39m finding it annoying in the new layout to open multiple emails simultaneously in separate tabs. Thus, I stick to basic HTML layout which has the plus point of being super fast. 2. Message.RecipientType.XX - You can set, TO To a single prime recipient, CC Carbon Copies and BCC Blind Carbon Copies in the Recipient Type. Example message.setRecipientsMessage.RecipientType.TO,InternetAddress.parserecieveremail message.setRecipientsMessage.RecipientType.CC,InternetAddress.parseanotheremail 3. message.setFromX - You can also set the sender email address specifically in the mail even though it will be identified by the EMail client. Example message.setFromnew InternetAddresssenderemail Note that the string senderemail usage with InternetAddress. CONGRATULATIONS You39ve setup your Java program to send E-Mails. Some of the ways you can put added functionalities would be by creating a good String creation method which would arrange the message text and then add to quotmessage.setText...quot method. I39ll be soon studying about recieving and processing E-mail with JavaMail. Will blog about it later. ADDENDUM Source code is added here - https//docs.google.com/file/d/0B4e1TZA7mwrNV01rTDBrQVluc3M/edituspsharing

    • इस ब्लॉग के लिए सामाजिक शेयर

पोर्फोलिओ और ब्लॉग
Mohammad Naseem Ashraf विभिन्न कंपनियों का अनुसरण करता है, ये कंपनियां और नियोक्ता Mohammad के फिर से शुरू देख सकते हैं
सबसे अच्छा नौकरी के अवसर पाने के लिए अपना फिर से शुरू करें अपलोड करें

मुफ्त रजिस्टर करें!