Add to Favorites    Make Home Page 1447 Online  
 Language Categories  
 Our Services  

Home » Java Home » Algorithms Home » Sending mail Using JavaMail to Yahoo and Gmail accounts

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Sending mail Using JavaMail to Yahoo and Gmail accounts
Author sai prasad
Author Email sai_tangirala1 [at] yahoo.com
Description download the javamail jars from
http://java.sun.com/products/javamail/downloads/index.html and put them in
classpath and then run the program.
Category Java » Algorithms
Hits 464342
Code Select and Copy the Code
import java.io.File; import java.security.Security; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class GoogleTest { private static final String SMTP_HOST_NAME = "smtp.gmail.com"; private static final String SMTP_PORT = "465"; private static final String emailMsgTxt = "Test Message Contents"; private static final String emailSubjectTxt = "A test from gmail"; private static final String emailFromAddress = "[email protected]"; private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; private static final String[] sendTo = {"[email protected]","[email protected]"}; private static final String fileAttachment="D:\hai.txt"; public static void main(String args[]) throws Exception { Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); new GoogleTest().sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress); System.out.println("Sucessfully Sent mail to All Users"); } public void sendSSLMessage(String recipients[], String subject, String message, String from) throws MessagingException { boolean debug = true; Properties props = new Properties(); props.put("mail.smtp.host", SMTP_HOST_NAME); props.put("mail.smtp.auth", "true"); props.put("mail.debug", "true"); props.put("mail.smtp.port", SMTP_PORT); props.put("mail.smtp.socketFactory.port", SMTP_PORT); props.put("mail.smtp.socketFactory.class", SSL_FACTORY); props.put("mail.smtp.socketFactory.fallback", "false"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("[email protected]", "give password of gmail"); } }); MimeMessage message1 = new MimeMessage(session); message1.setFrom( new InternetAddress(from)); message1.addRecipient( Message.RecipientType.TO, new InternetAddress(recipients[0])); message1.addRecipient( Message.RecipientType.TO, new InternetAddress(recipients[1])); message1.setSubject( "Hello JavaMail Attachment"); // create the message part MimeBodyPart messageBodyPart = new MimeBodyPart(); //fill message messageBodyPart.setText("Hi"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(fileAttachment); messageBodyPart.setDataHandler( new DataHandler(source)); messageBodyPart.setFileName(fileAttachment); multipart.addBodyPart(messageBodyPart); // Put parts in message message1.setContent(multipart); // Send the message Transport.send( message1 ); } }

Related Source Codes

Script Name Author
Simple Program in Java to Implement Multithreading Satish.K
Simple Calculator in Java Using Remote Method Invocation Satish.K
Guest Book Application Using Servlets Satish.K
String Manipulation Using Stringification Satish.K
String Manipulation Using Stringification Satish.K
Moving Ball Application Using Java Beans Satish.K
Rapid Roll game subrahmanyeswararao
student mgm arpan
Sourav Datta
Download Manager Sagar
Address Book in Java Rahul Chouhan
address book using java database connectivity(jdbc-msaccess) shekhar bansal
sun Steganography B.Rajavel
Connecting Java with MS-Access - Inserting data in Aseem
Address Book in Java Rahul Chouhan

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=1258


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review