We just need mail.jar imported into our Eclipse project for this mail functionality.
package Mail;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailManager {
private static Properties properties = System.getProperties();
static {
String mailhost = "relay.company.com";
properties.put("mail.smtp.host", mailhost);
properties.put("mail.mime.charset", "utf-8");
}
public static void main(String[] args) throws Throwable {
try {
// System.out.println("Start-----------");
// String userName = "Vikas";
// String from = userName + "@gmail.com";
// String to = "manish.kr.jaiswal@gmail.com";
// String body = "This is the body";
// String subject = "TestSubject";
// sendEmail(from, to, body, subject);
// System.out.println("Start-----------2");
} catch (Throwable th) {
th.printStackTrace();
throw th;
}
}
public static void sendEmail(String from, String to, String body,
String subject) throws MessagingException, AddressException {
Session session = Session.getInstance(properties, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
to, false));
message.setSubject(subject);
message.setContent(body, "text/html; charset=utf-8");
// message.setText(body);// , "text/html;
Transport.send(message);
}
}
No comments:
Post a Comment