Witam,
Zaczynam przygode z Java i probuje sobie napisac prosty programik, ktory bedzie wysylal maile.
Idea jest taka ze bedzie on wywolywany z poziomu bazy danych (SQL Server) z parametrami (email, tytul, tresc).
Napisalem cos takiego:
klasa Main
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import java.util.Properties;
public class Main {
private static final String SMTP_HOST_NAME = "xxx";
private static final String SMTP_AUTH_USER = "yyy";
private static final String SMTP_AUTH_PWD = "ppp";
public static void main(String[] args) throws Exception{
new Main().test();
Argumenty a;
a = new Argumenty();
a.email = args[1];
a.tresc = args[2];
a.tytul = args[3];
}
public void test() throws Exception{
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
// uncomment for debugging infos to stdout
// mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setContent(a.tresc, "text/plain");
message.setFrom(new InternetAddress("[email protected]"));
message.setSubject(a.tytul);
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(a.email));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}
oraz klasa Argumenty:
public class Argumenty
{
public String email, tytul, tresc;
}
Wiem ze w klasie Main przekombinowalem efekt jaki chce uzyskac to wywolanie programu z trzema parametrami:
email, tytul, tresc i przkeazanie ich do 'public void test() throws Exception'.
Umieszczenie zmiennych w odpowiednich miejsach.
Samo wysylanie maili idzie bez problemu mam problem z przekazaniem tych argumentow gdyz nie wiem jak to zrobic.
Mozecie podpowiedziec jak to zrobic?