Go Back   Wiki NewForum | Latest Entertainment News > Career Forum & Tips > Tech Forum & Tutorial > Java Forum & Tutorial


How To Send Email Using Java Mail


Reply
Views: 2076  
Thread Tools Rate Thread
  #1  
Old 05-25-2009, 04:52 PM
bholus7
Guest
 
Posts: n/a
Default How To Send Email Using Java Mail

How To Send Email Using Java Mail


Can someone please help with the following code
If (a===b)
//then send email using java mail API

How do I code above?


Look thru java mail API's there are methods like
public void sendMail() etc.

You need to follow a few steps as given below:

//Create an instance of properties
Properties props = new Properties();
//Setup mail server
Authenticator auth = new PopupAuthenticator();
//Set the Servername and hostname of email Server
props.put(
ConfigHelper.getProperty(HOST_NAME),
ConfigHelper.getProperty(SERVER_NAME));
props.put("mail.smtp.auth", "true");

//Get session
Session session = Session.getInstance(props, auth);
//Create Message instance for email content
Message msg = new MimeMessage(session);
//senders mail id
Address loggerId = new InternetAddress(sender, sender);
//Set the from email id
msg.setFrom(loggerId);
multipleEmailsSplit(receiver, msg, Message.RecipientType.TO);
if ((receiverCC != null) && !receiverCC.equals("null")) {
multipleEmailsSplit(receiverCC, msg, Message.RecipientType.CC);
}
if ((receiverBCC != null) && !receiverBCC.equals("null")) {
multipleEmailsSplit(receiverBCC, msg, Message.RecipientType.BCC);
}
msg.setSubject(subject);
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(emailContent, "text/html");
// create the Multipart and add its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp);
//attach files to e-mail
for (int i = 0; i < fileList.size(); i++) {
String file = (String) fileList.get(i);
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(file);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
mp.addBodyPart(mbp2);
}
// add the Multipart to the message
msg.setContent(mp);
//Send the mail
Transport.send(msg);

or

- Create the mail session object (or get/lookup the session from your app server using JNDI)
- Compose message, set/add recipient(s)
- Send using Transport's static send method

Properties props = new Properties();
props.put("mail.smtp.host", "mailserver.com");
Session s = Session.getInstance(props,null);

InternetAddress from = new InternetAddress("mail@...");
InternetAddress to = new InternetAddress(recepeint@...");

MimeMessage message = new MimeMessage(s);
message.setFrom(from);
message.addRecipient(Message.RecipientType.TO, to);

message.setSubject("Your subject");
message.setText("Your text");
Transport.send(message);

If you lookup up the session using JNDI you configure mail server (smtp host) in your app server configuration

Reply With Quote
Reply

New topics in Java Forum & Tutorial





Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
WikiNewForum)