All pastes #403431 Raw Edit

Java Mail Problem

public text v1 · immutable
·published 2007-03-20 11:20 UTC
rendered paste body
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	
///
/// Send Message
///
	String to = "toAdress";
	String from = "myName";
	String host = "myHost";
	Logger log = Logger.getLogger(TestMail2.class);
	//boolean debug = Boolean.valueOf(args[3]).booleanValue();

	// create some properties and get the default Session
	Properties props = new Properties();
	props.put("mail.smtp.host", host);
	
	Session session = Session.getDefaultInstance(props, null);
//	session.setDebug(debug);
	
	try {
	    // create a message
	    MimeMessage msg = new MimeMessage(session);
	    msg.setFrom(new InternetAddress(from));
	    InternetAddress[] address = {new InternetAddress(to)};
	    msg.setRecipients(Message.RecipientType.TO, address);
	    msg.setSubject("JavaMail APIs Multipart Test");
	    msg.setSentDate(new Date());
	
	    // create and fill the first message part
	    MimeBodyPart mbp1 = new MimeBodyPart();
	    mbp1.setText("Your mail");
	
	    // create and fill the second message part
	    MimeBodyPart mbp2 = new MimeBodyPart();
	    // Use setText(text, charset), to show it off !
	    File file = new File("F:\\pen-data\\image-out\\DB_Notizblock_V1_0-ALEX2.1825.pdf");
	    if(file.exists())
    	{
    	log.info("Atachment exists");
    	}
	    
	    FileDataSource fds =  new FileDataSource(file);
   	    //log.info("FileDataSource: contentType: "+fds.getContentType()+"Name "+fds.getName());
  	           	    
   	    DataHandler x = new DataHandler(fds);
   	    //log.info("DataHandler: contentType"+x.getContentType()+"Name"+x.getName());
    	mbp2.setDataHandler(x);
    	mbp2.setFileName(fds.getName());
    	   	  
 	    
	    // create the Multipart and its parts to it
	    Multipart mp = new MimeMultipart();
	    mp.addBodyPart(mbp1);
	    mp.addBodyPart(mbp2);
	
	    // add the Multipart to the message
	    msg.setContent(mp);
	    
	    // send the message
	    Transport.send(msg);
	} catch (MessagingException mex) {
	    mex.printStackTrace();
	    Exception ex = null;
	    if ((ex = mex.getNextException()) != null) {
	   ex.printStackTrace();
	    }
		}
		}
}