DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2005
    Posts
    55

    how to send mail

    hi all
    how can i send mail by java useing smtp server (SSL)?
    thax
    Rafeek

  2. #2
    Join Date
    Jul 2007
    Posts
    1
    Try this

    Code:
    public static synchronized void sendMail() {	
    		
    		
    		//		 SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
    		String to = SelectedEmail;
    
    		String from = "fromAddress";
    		// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
    		String host = "Host IP";
    
    		// Create properties, get Session
    		Properties props = new Properties();
    
    		// If using static Transport.send(),
    		// need to specify which host to send it to
    		props.put("mail.smtp.host", host);
    		// To see what is going on behind the scene
    		props.put("mail.debug", "true");
    		Session session = Session.getInstance(props);
    
    		try {
    			// Instantiatee a message
    			Message msg = new MimeMessage(session);
    			
    			//Set message attributes
    			msg.setFrom(new InternetAddress(from));
    			InternetAddress[] address = { new InternetAddress(to) };
    			msg.setRecipients(Message.RecipientType.TO, address);
    			msg.setSubject("Your Subject");
    			msg.setSentDate(new Date());
    
    			//for attachment
    
    			BodyPart messageBodyPart = new MimeBodyPart();
    			String body = "Body part here";
    			
    			messageBodyPart.setText("any body text here");
    			
    			Multipart multipart = new MimeMultipart();
    			multipart.addBodyPart(messageBodyPart);
    			messageBodyPart = new MimeBodyPart();
    			DataSource source = new FileDataSource(new File(path, fileName));
    			messageBodyPart.setDataHandler(new DataHandler(source));
    			messageBodyPart.setFileName(source.getName());
    			multipart.addBodyPart(messageBodyPart);
    			msg.setContent(multipart);
    			
    			//Send the message
    			
    			Transport.send(msg);
    			
    
    		} catch (MessagingException mex) {
    			// Prints all nested (chained) exceptions as well
    			mex.printStackTrace();
    		}
    
    	}

Similar Threads

  1. Replies: 0
    Last Post: 04-11-2006, 01:26 AM
  2. Need Code to send mail from Excel File
    By vks_online in forum Java
    Replies: 0
    Last Post: 03-20-2006, 10:29 AM
  3. How to write VB code to send mail
    By saradhi in forum Enterprise
    Replies: 13
    Last Post: 12-07-2005, 06:51 AM
  4. Replies: 1
    Last Post: 05-28-2002, 11:04 PM
  5. Send advertise mail question?
    By TQ in forum Enterprise
    Replies: 0
    Last Post: 08-07-2001, 02:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links