JavaMail

Status
Nicht offen für weitere Antworten.

hunter1977

Mitglied
Hallo Leute,

ich habe mir folgenden Code aus dem SUn Tutorial geschnappt und wollte den compilieren.
Code:
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class MailExample {
  public static void main (String args[]) throws Exception {
    String host = args[0];
    String from = args[1];
    String to = args[2];

    // Get system properties
    Properties props = System.getProperties();

    // Setup mail server
    props.put("mail.smtp.host", host);

    // Get session
    Session session = Session.getDefaultInstance(props, null);

    // Define message
    MimeMessage message = new MimeMessage(session);

    // Set the from address
    message.setFrom(new InternetAddress(from));

    // Set the to address
    message.addRecipient(Message.RecipientType.TO, 
      new InternetAddress(to));

    // Set the subject
    message.setSubject("Hello JavaMail");

    // Set the content
    message.setText("Welcome to JavaMail");

    // Send message
    Transport.send(message);
  }
}

Leider bekomme ich immer java.lang.ArrayIndexOutOfBoundsException: 0
at MailExample.main(MailExample.java:20)
Exception in thread "main"

Kann mir jemand weiterhelfen?

Danke Hunter
 
überprüf ob beim starten auch 3 argumente übergeben werden und wenn nicht dann brich das programm ab also so:
Code:
  public static void main (String args[]) throws Exception { 
    if(args.length != 3) {
      System.out.println("Excepted: java MailExample <host> <from> <to>");
      System.exit(0);
    }  
    //...
  }
 
Ich habe das Programm anders aufgebaut.

Code:
public class SendJavaMail
{
  public static void postMail( String recipient,
                               String subject,
                               String message, String from )
    throws MessagingException
  {
    Properties props = new Properties();
    props.put( "mail.smtp.host", "MAIL" );

    Session session = Session.getDefaultInstance( props );

    Message msg = new MimeMessage( session );
    

    InternetAddress addressFrom = new InternetAddress( from );
    msg.setFrom( addressFrom );
    

    InternetAddress addressTo = new InternetAddress( recipient );
    msg.setRecipient( Message.RecipientType.TO, addressTo );
    
 
    msg.setSubject( subject );

    //msg.setContent( message, "text/text" );

    

    MimeBodyPart mbp1 = new MimeBodyPart();
    mbp1.setText(message);


    MimeBodyPart mbp2 = new MimeBodyPart();


	//FileDataSource fds = new FileDataSource("");
    //mbp2.setDataHandler(new DataHandler(fds));
    //mbp2.setFileName(fds.getName());

   
    Multipart mp = new MimeMultipart();
    //mp.addBodyPart(mbp1);
    //mp.addBodyPart(mbp2);


    msg.setContent(mp);
    msg.setSentDate(new Date());
    
    Transport.send( msg );
  }
  
  public static String Uhrzeit(){
    String Zeit = "";
    Date currentDate = new Date(System.currentTimeMillis());
    Zeit = currentDate.toString();
    return Zeit;

}

  public static void main( String args[] ) throws Exception
  {
    postMail( );
  }
}

Ich habe auch einen neuen Thread aufgemacht. Ich habe noch ein Problem die mail.jar und activation.jar in mein Mail.jar zu integrieren.
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben