File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes How to send mail form java application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "How to send mail form java application" Watch "How to send mail form java application" New topic
Author

How to send mail form java application

kaan h.
Greenhorn

Joined: Apr 06, 2008
Posts: 5
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
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8915
    
    8

Welcome to the JavaRanch.
When you are looking to answer questions, we ask that you don't revive old threads as you have in your other two posts. It is very unlikely that those folks who solved their problems months ago will come back looking for more input.
If you have some code and would like to share, it may make more sense to contribute to our JavaRanch FAQ. For example, our Enterprise Edition FAQ has a number of resources on JavaMail.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Howdy "kaan h.",

Welcome to JavaRanch.

There are not many strict rules except for the names every member has. JavaRanch insists the users to have their *real* names as their first and last names separated by a space. It is well explained in Ranch's Naming Policy

You may have to update your display name accordingly. You can easily update your name by editing your profile/


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
GeeCON Prague 2014
 
subject: How to send mail form java application