Results 1 to 4 of 4

Thread: java & smtp

  1. #1

    java & smtp

    how can i log in via POP first and then try to send email using smtp in java. any sample codes will be highly appreciated!!
    Thanx

  2. #2
    Golthar,2uantuM & stdunbar, i believe u can help

  3. #3
    Join Date
    Aug 2002
    Location
    Superior, CO, USA
    Posts
    633
    Take a look at the JavaMail API - http://java.sun.com/products/javamai...ocs/index.html

    Also, mousetrap mail (http://mousetrap.sourceforge.net/) is a full pop3 compatible mail client that can also talk to an smtp server. It is available in source form.

  4. #4
    Join Date
    Dec 2002
    Location
    NY, NY
    Posts
    3,975
    sample code..
    Code:
    <%@ page import="java.net.*"%>
    <%@ page import="javax.mail.internet.*"%>
    <%@ page import="javax.mail.*"%>
    <%@ page import="javax.activation.*"%>
    <%@ page import="java.util.*"%>
    <%@ page import="java.io.*"%>
    
    <%
    StringBuffer hw = new StringBuffer();
    	String error = "";
    	try {
    	 boolean debug = false;
    
    	 //Set the host smtp address
    	 Properties props = new Properties();
    	 props.put("mail.transport.protocol", "SMTP");
    	 props.put("mail.smtp.host", "localhost");
    
    	// create some properties and get the default Session
    	Session mailSession = Session.getDefaultInstance(props, null);
    	mailSession.setDebug(debug);
    
    	// create a message
    	Message msg = new MimeMessage(mailSession);
    
    	// set the from and to address
    	InternetAddress addressFrom = new InternetAddress("FROM EMAIL HERE");
    	msg.setFrom(addressFrom);
    
    	InternetAddress[] addressTo = new InternetAddress[1]; 
    	addressTo[0] = new InternetAddress("TO EMAIL #1 HERE");
    	msg.setRecipients(Message.RecipientType.TO, addressTo);
       
    	// Optional : You can also set your custom headers in the Email if you Want
    	msg.addHeader("MyHeaderName", "myHeaderValue");
    
    	// Setting the Subject and Content Type
    	msg.setSubject("TEST");
    	msg.setContent("Email Content - body", "text/plain");
    
    hw.append("TEXT BLAH BLAH")
    .append("TEXT GOES HERE...");
    
    	msg.setText(hw.toString());
    	Transport.send(msg);
    	} catch(Throwable t) {
    		error = t.getMessage();
    	}	
    %>
    
    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    <BR>
    <B>
    <P><FONT face="Arial" color="#003399" size="4">It was sent successfully!!!!!!!!!!!</FONT>
    Error = <%= error %><br><br><br><br>
    
    MSG WAS :<br><%= hw.toString() %>
    </B>
    </BODY>
    </HTML>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •