- commons-email-1.2.jar
- commons-logging-1.1.1.jar
- httpclient-4.1.1.jar
- httpcore-4.1.jar
- mail.jar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package com.as400samplecode; import java.io.IOException; import org.apache.commons.mail.DefaultAuthenticator; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.ParseException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; public class SendHTMLEmail { public static void main(String[] args) { try { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(httpPost); HttpEntity resEntity = response.getEntity(); String responseBody = "" ; if (resEntity != null ) { responseBody = EntityUtils.toString(resEntity); } HtmlEmail htmlEmail = new HtmlEmail(); htmlEmail.setHostName( "smtp.gmail.com" ); htmlEmail.setSmtpPort( 587 ); htmlEmail.setDebug( true ); htmlEmail.setAuthenticator( new DefaultAuthenticator( "userId" , "password" )); htmlEmail.setTLS( true ); htmlEmail.addTo( "recipient@gmail.com" , "recipient" ); htmlEmail.setFrom( "sender@gmail.com" , "sender" ); htmlEmail.setSubject( "Send HTML email with body content from URI" ); htmlEmail.setHtmlMsg(responseBody); htmlEmail.send(); } catch (EmailException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.