Email Messaging For Core Java
In this example, we show you how to sending an Email using Core Java , You know developer's mind is different , developer's can do anything whatever it's think, and java is a endless world of programming, it's really different for other's programming language, so best of all students, visitor's or follower's you are in the right place to learn java programming language also we provide a example code for all the developments project for my site, before you can use this codes, just click on all the advertisements ,available on this page. Now, we going to make a project.
"GoogleMailSender.java"
/*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,I am from Bhopal hello how are you";
private static final String emailSubjectTxt = "Javaaaaaaaaaa";
private static final String emailFromAddress = ""; //
At this place use your email address
private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private static final String[] sendTo = {"@gmail.com","@yahoo.com"}; //
At this place senderto email address
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("
Enter Your Email Address", "
Enter Password");
}
});
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
"MainSender.java"
/**
* @author Balwant Singh Chandel
*/
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
public class MailSender {
public static void main(String[] args) {
try {
Properties env = new Properties();
String host = ""; //hostname like // mail.google.com
//
String username = "user";
//
String password = "pwd";
String protocol = "smtp";
env.put("mail.transport.protocol", protocol);
env.put("mail.smtp.host", host);
env.put("mail.smtp.port",25);
Session session = Session.getInstance(env);
Message message = new MimeMessage(session);
//set the subject
message.setSubject("Mail sent through Java Mail");
Address address1 =new InternetAddress("test.google.com","google");
Address address2 =
new InternetAddress("abc@gmail.com","Tutorials");
//construct and set the recipient addresses
message.addRecipient(Message.RecipientType.TO, address1);
message.addRecipient(Message.RecipientType.CC, address2);
//construct and set the sender address
Address fromAddress =new InternetAddress("abc@gmail.com","balwant");
message.setFrom(fromAddress);
//set the content of the message
message.setContent("Hello, dude!", "text/plain");
//send the message
Transport.send(message);
} catch (Exception e) {
System.out.println("Exception>>>>>");
e.printStackTrace();
}
}
}
Compile Both Files :
And Run.....
If this information is important to you , and you want to , I will continue writing some more details of Android Programming Language, so click on the advertising, available on this page. This is motivate me for writing some more useful stuff. Thank's
Comments
Post a Comment