-
April 9th, 2008, 05:57 AM
#1
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 = "from@address.com";
private static final String
SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private static final String[]
// mail göndermek istediğimiz adresler
sendTo = {"to@address.com"};
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("username@gmail.com", "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
-
April 11th, 2008, 03:07 PM
#2
Re: Hot send mail using a java application
why not just use Apache's commons mail?
-
June 10th, 2009, 05:49 AM
#3
Re: Hot send mail using a java application
Is there any third party Java Mail APIs available which doesn't use JavaMail API?
Last edited by ranbahadur; June 10th, 2009 at 05:53 AM.
-
June 10th, 2009, 05:52 AM
#4
Re: Hot send mail using a java application
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......
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|