Hallo,
habe bereits knapp 1,5 Jahre Java in der Schule (Oberstufe, momentan 13. KL) und somit schon diverse Basics drauf (OOP, GUI, Datenbank).
Doch hänge ich an folgendem Code fest:
Hauptklasse
[Java]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* Beschreibung
*
* @version 1.0 vom 23.04.2011
* @author
*/
public class emailHaupt extends JFrame {
// Anfang Attribute
private JLabel lblUeberschrift = new JLabel();
private JTextField txtEmail = new JTextField();
private JLabel lblEmailText = new JLabel();
private JLabel lblAnzahl = new JLabel();
private JTextField txtAnzahl = new JTextField();
private JButton cmdSpamgo = new JButton();
private JButton cmdEnde = new JButton();
// Ende Attribute
public emailHaupt(String title) {
// Frame-Initialisierung
super(title);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 490;
int frameHeight = 304;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
lblUeberschrift.setBounds(168, 8, 143, 24);
lblUeberschrift.setText("Email Sender");
lblUeberschrift.setFont(new Font("MS Sans Serif", Font.BOLD, 19));
cp.add(lblUeberschrift);
txtEmail.setBounds(160, 56, 161, 32);
txtEmail.setText("");
cp.add(txtEmail);
lblEmailText.setBounds(16, 64, 44, 16);
lblEmailText.setText("Email:");
lblEmailText.setFont(new Font("MS Sans Serif", Font.BOLD, 13));
cp.add(lblEmailText);
lblAnzahl.setBounds(16, 120, 119, 16);
lblAnzahl.setText("Anzahl der Mails:");
lblAnzahl.setFont(new Font("MS Sans Serif", Font.BOLD, 13));
cp.add(lblAnzahl);
txtAnzahl.setBounds(160, 112, 161, 32);
txtAnzahl.setText("");
cp.add(txtAnzahl);
cmdSpamgo.setBounds(80, 168, 131, 49);
cmdSpamgo.setText("Mails verschicken");
cmdSpamgo.setMargin(new Insets(2, 2, 2, 2));
cmdSpamgo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
cmdSpamgo_ActionPerformed(evt);
}
});
cp.add(cmdSpamgo);
cmdEnde.setBounds(272, 168, 115, 49);
cmdEnde.setText("Ende");
cmdEnde.setMargin(new Insets(2, 2, 2, 2));
cmdEnde.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
cmdEnde_ActionPerformed(evt);
}
});
cp.add(cmdEnde);
// Ende Komponenten
}
// Anfang Methoden
public void cmdSpamgo_ActionPerformed(ActionEvent evt)
{
try
{
sendeEmail();
System.out.println("Test");
}
catch(Exception e)
{
System.out.println("Fehlgeschlagen..");
System.exit(0);
}
}
public void cmdEnde_ActionPerformed(ActionEvent evt)
{
System.exit(0);
}
public void sendeEmail() throws Exception
{
String recipient, subject, message, from;
int Anzahl;
Anzahl = Integer.parseInt(txtAnzahl.getText());
recipient = txtEmail.getText();
subject = "Hundenachricht";
message = "SPAM";
from = "GeorgeBusch@american-gov.com";
SendeMailExample neu = new SendeMailExample();
do
{
neu.postMail(recipient, subject, message, from);
Anzahl = Anzahl -1;
}while(Anzahl >0);
}
// Ende Methoden
}
[/Java]
mailExample Klasse:
[Java]
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendeMailExample
{
public static void postMail( String recipient,
String subject,
String message, String from )
throws MessagingException
{
Properties props = new Properties();
props.put( "mail.smtp.host", "smtp.mail.yahoo.de" );
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", "xxxxxx");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "25");
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/plain" );
Transport.send( msg );
}
}
[/Java]
Ich hänge beim Catch in der Hauptklasse fest..
Wo liegt der (Denk)Fehler?
Vielen Dank im vorraus!
habe bereits knapp 1,5 Jahre Java in der Schule (Oberstufe, momentan 13. KL) und somit schon diverse Basics drauf (OOP, GUI, Datenbank).
Doch hänge ich an folgendem Code fest:
Hauptklasse
[Java]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* Beschreibung
*
* @version 1.0 vom 23.04.2011
* @author
*/
public class emailHaupt extends JFrame {
// Anfang Attribute
private JLabel lblUeberschrift = new JLabel();
private JTextField txtEmail = new JTextField();
private JLabel lblEmailText = new JLabel();
private JLabel lblAnzahl = new JLabel();
private JTextField txtAnzahl = new JTextField();
private JButton cmdSpamgo = new JButton();
private JButton cmdEnde = new JButton();
// Ende Attribute
public emailHaupt(String title) {
// Frame-Initialisierung
super(title);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 490;
int frameHeight = 304;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
lblUeberschrift.setBounds(168, 8, 143, 24);
lblUeberschrift.setText("Email Sender");
lblUeberschrift.setFont(new Font("MS Sans Serif", Font.BOLD, 19));
cp.add(lblUeberschrift);
txtEmail.setBounds(160, 56, 161, 32);
txtEmail.setText("");
cp.add(txtEmail);
lblEmailText.setBounds(16, 64, 44, 16);
lblEmailText.setText("Email:");
lblEmailText.setFont(new Font("MS Sans Serif", Font.BOLD, 13));
cp.add(lblEmailText);
lblAnzahl.setBounds(16, 120, 119, 16);
lblAnzahl.setText("Anzahl der Mails:");
lblAnzahl.setFont(new Font("MS Sans Serif", Font.BOLD, 13));
cp.add(lblAnzahl);
txtAnzahl.setBounds(160, 112, 161, 32);
txtAnzahl.setText("");
cp.add(txtAnzahl);
cmdSpamgo.setBounds(80, 168, 131, 49);
cmdSpamgo.setText("Mails verschicken");
cmdSpamgo.setMargin(new Insets(2, 2, 2, 2));
cmdSpamgo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
cmdSpamgo_ActionPerformed(evt);
}
});
cp.add(cmdSpamgo);
cmdEnde.setBounds(272, 168, 115, 49);
cmdEnde.setText("Ende");
cmdEnde.setMargin(new Insets(2, 2, 2, 2));
cmdEnde.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
cmdEnde_ActionPerformed(evt);
}
});
cp.add(cmdEnde);
// Ende Komponenten
}
// Anfang Methoden
public void cmdSpamgo_ActionPerformed(ActionEvent evt)
{
try
{
sendeEmail();
System.out.println("Test");
}
catch(Exception e)
{
System.out.println("Fehlgeschlagen..");
System.exit(0);
}
}
public void cmdEnde_ActionPerformed(ActionEvent evt)
{
System.exit(0);
}
public void sendeEmail() throws Exception
{
String recipient, subject, message, from;
int Anzahl;
Anzahl = Integer.parseInt(txtAnzahl.getText());
recipient = txtEmail.getText();
subject = "Hundenachricht";
message = "SPAM";
from = "GeorgeBusch@american-gov.com";
SendeMailExample neu = new SendeMailExample();
do
{
neu.postMail(recipient, subject, message, from);
Anzahl = Anzahl -1;
}while(Anzahl >0);
}
// Ende Methoden
}
[/Java]
mailExample Klasse:
[Java]
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendeMailExample
{
public static void postMail( String recipient,
String subject,
String message, String from )
throws MessagingException
{
Properties props = new Properties();
props.put( "mail.smtp.host", "smtp.mail.yahoo.de" );
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", "xxxxxx");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "25");
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/plain" );
Transport.send( msg );
}
}
[/Java]
Ich hänge beim Catch in der Hauptklasse fest..
Wo liegt der (Denk)Fehler?
Vielen Dank im vorraus!
Zuletzt bearbeitet: