that code can help you. You shoul send mail in a seperate
thread..:
//designed (will be transformed for the framework) for struts framework uses gmail. Username and password are included in the code.
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.Security;
import java.util.Properties;
public class MyGoogleMailSender {
private int x;
public static void main(
String args[]) throws Exception {
String SMTP_HOST_NAME = "smtp.gmail.com";
String SMTP_PORT = "465";
String emailMsgTxt = "mail body";
String emailSubjectTxt = "subject";
String emailFromAddress = "
[email protected]";
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String[] sendTo = {"
[email protected]"};
//String toBeSent="
[email protected]";
String recipients[]=new String[1];
recipients[0]="
[email protected]";
//System.out.print(recipients[0]);
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
// buraya gmail mail adresinizi ve sifrenizi girmelisiniz.
return new PasswordAuthentication("
[email protected]", "password");
}
});
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(emailFromAddress);
msg.setFrom(addressFrom);
InternetAddress[]
addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
msg.setSubject(emailSubjectTxt);
msg.setContent(emailMsgTxt, "text/plain");
//Transport.send(msg);
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
new GoogleMailSender().sendSSLMessage(
sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}//end main
}//end class
[ July 03, 2008: Message edited by: kaan h. ]