Ulf Dittmer wrote:The Java API for sending email is called JavaMail. The http://faq.javaranch.com/java/JavaEnterpriseEditionFaq has some links to introductions and other material about it. Note that you'll need an email server to use it (or use a library like Aspirin that can send email w/o a mail server).
mihira
mih ira wrote: This is the sample code for your request
Use this for sending your emails automatically
try {
SendEmail sendEmail = new SendEmail();
String[] sendEmails = this.getUsersEmails();
String subject = "This is email subject";
String description = "email body message";
String userMail = getLoggedInUser();
sendEmail.postMail(
sendEmails,
subject,
description,
userMail);
} catch (MessagingException ex) {
ex.printStackTrace();
}
//SendEmail.java class method
public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
{
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", "email server ip address");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
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);
// Optional : You can also set your custom headers in the Email if you Want
msg.addHeader("MyHeaderName", "Write here the email header");
// Setting the Subject and Content Type
msg.setSubject(subject);
/*msg.setContent(message, "text/plain");*/
// Create a message part to represent the body text
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(message);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
//msg.setContent(message, "text/plain");
Transport.send(msg);
}
mihira
mih ira wrote:It is the mail server IP.
You have to give the mail server IP address and open the ports for email send.
Don't get me started about those stupid light bulbs. |