Hot send mail using a java application
Sending mail from a Java application is so easy.
1.first download mail API from SUN's website (mail_api.rar)
2.Include mail.jar and activation.jar to your IDE's libraries
3.Use following code to send email(example configured for gmail)
enjoy...
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.Security;
import java.util.Properties;
public class GoogleMailSender {
private static final String
SMTP_HOST_NAME = "smtp.gmail.com";
private static final String
SMTP_PORT = "465";
private static final String
emailMsgTxt = " test mail sent";
private static final String
emailSubjectTxt = "Javaaaaaaaaaa";
private static final String
// mailin kimden gittiğini gösteren adres
emailFromAddress = "[email protected]";
private static final String
SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private static final String[]
// mail göndermek istediğimiz adresler
sendTo = {"[email protected]"};
public void sendSSLMessage(String recipients[], String subject,
String message, String from)
throws MessagingException {
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 here");
}
});
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
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(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
public static void main(String args[]) throws Exception {
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
Re: Hot send mail using a java application
why not just use Apache's commons mail?
Re: Hot send mail using a java application
Is there any third party Java Mail APIs available which doesn't use JavaMail API?
Re: Hot send mail using a java application
Quote:
Originally Posted by
chazzy
why not just use Apache's commons mail?
Yes we can use Apache Commons to send mail. Apache commons has been build around JavaMail.Apache Commons is the best third party open souce to send mails, etc......