Hallo zusammen,
ich habe folgedes Problem:
das unten stehende Programm kann mails versenden, was auch funktioniert! :-D
nun möchte ich das Programm von einer anderen Main Methode aus starten....
was muss ich abändern dass dies geht?
danke im voraus
ich habe folgedes Problem:
das unten stehende Programm kann mails versenden, was auch funktioniert! :-D
nun möchte ich das Programm von einer anderen Main Methode aus starten....
was muss ich abändern dass dies geht?
danke im voraus
Java:
//MAIN Methode
public class MyAppVorapp
{
public static void main(String[] args)
{
Mail m = new Mail();
}
}
Java:
//Eigentliches Programm
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Mail
{
public void sendMail(String smtpHost,String username,String password,String senderAddress,String recipientsAddress,String subject,String text )
{
MailAuthenticator auth = new MailAuthenticator(username, password);
Properties properties = new Properties();
properties.put("mail.smtp.host", smtpHost);
properties.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(properties, auth);
try {
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(senderAddress));
InternetAddress[] address = {new InternetAddress(recipientsAddress)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(text);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource("C:\\Dokumente und Einstellungen\\halm_a\\Desktop\\Programme\\Diplomarbeit\\Protokoll.txt");
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
// create the Multipart and add its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
// set the Date: header
msg.setSentDate(new Date());
// send the message
Transport.send(msg);
}
catch (Exception e)
{
e.printStackTrace( );
}
}
class MailAuthenticator extends Authenticator
{
private final String user;
private final String password;
public MailAuthenticator(String user, String password)
{
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(this.user, this.password);
}
}
public static void main(String[] args)
{
try
{
String emailadresse = "";
int x = 0;
FileReader fr = new FileReader("emailadresse.txt");
while ((x = fr.read()) != -1)
emailadresse = emailadresse + (char)x;
fr.close();
System.out.println(emailadresse);
String username = "user";
String password = "geheim";
String senderAddress = "test@web.de";//someone@web.de
String recipientsAddress = emailadresse; //somereceiver@web.de
String subject = "Java forum";
String smtpHost = "smtp.web.de";
String text = "Lieber Forum Besucher,";
new Mail().sendMail(smtpHost, username, password, senderAddress, recipientsAddress, subject, text);
}
catch(IOException ioex)
{
System.out.println(ioex.getMessage());
}
}
}