Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java mail.

807599Apr 3 2007 — edited Apr 3 2007
Use this code
 


              String host = "";// give ur mail server name or IP I hope u will be having local mail server
                               // or configure gmail. Through that I think u can send mail.   
	
	
//			   Get system properties
			Properties props = System.getProperties();

//			   Setup mail server
			props.put("mail.smtp.host", host);
			props.put("mail.smtp.auth","true");
			//props.setProperty("mail.smtp.dsn.notify",
			//					"SUCCESS,FAILURE ORCPT=rfc822;sachincr@gmail.com");
			props.setProperty("mail.smtp.dsn.ret", "FULL");

//			   Get session
			Session session = Session.getDefaultInstance(props,  new Authenticator(){
			protected PasswordAuthentication getPassWordAuthentication(){
			return new PasswordAuthentication("userName","passwd"); // give ur username and passwd				}
			});
			session.setDebug(true);
		
		String[] mailingList = {"hjj@gmail.com","hjk.jk@rediffmail.com"};
		
			
			
			
			
//			   Define message
			MimeMessage message = new MimeMessage(session);
			try {
				message.setFrom(new InternetAddress("sachincr@gmail.com"));
	
	
				for (int i = 0; i < mailingList.length; i++) {

				message.addRecipient(
					Message.RecipientType.TO,
					new InternetAddress(mailingList));
}


message.setSubject("This is my firts mail");
//message.setText("Welcome to JavaMail");

BodyPart messageBodyPart = new MimeBodyPart();



// Fill the message
//messageBodyPart.setText("Pardon Ideas");

String data ="HTML content of the file " // read the HTML file and store that into data variable
messageBodyPart.setContent(data,"text/html");



Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);


message.setContent(multipart);


// Send message

message.saveChanges(); // implicit with send()
Transport transport = session.getTransport("smtp");
transport.connect(host, "username", "passwd");// give ur username and passwd
transport.sendMessage(message, message.getAllRecipients());
transport.close();

} catch (MessagingException messagingException) {
messagingException.printStackTrace();
//System.exit(9); //
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 1 2007
Added on Apr 3 2007
4 comments
285 views