Main Menu
Most Popular Articles
Browse Knowledgebase
Billing/Helpdesk Login

Knowledgebase Query
Ask a question using natural language. Try to include as much information as possible:


Contact Us
Billing/Helpdesk Login
support@interadvantage.com
210-659-2532 (M-F, 10-5 CST)

Knowledgebase
Using JavaMail to send E-mail
Top : Tomcat (JSP & Servlet Container)

Article ID: 000005
Rating: 4.4 / 5.0 (7 votes)
Views: 5121

Problem
How do I use JavaMail to send E-mail from a Java web application?

Solution
JavaMail is the recommended way to send e-mail from Java code on our servers.

We keep the latest versions of JavaMail and the JavaBeans Activation Framework in your classpath, so you do not need to install these into your site.

The following sample JSP demonstrates the correct approach for sending e-mail from your Java code:

<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>

<html>
<head><title>JavaMail Test</title></head>
<body>
<%
   Properties props = new Properties(); // Properties for this mail session
   Session    mailSession;              // Current mail session
   Transport  transport;                // Mail transport protocol object

   // Initialize props, mailSession, transport
   props.put("mail.smtp.host", "localhost");
   mailSession = Session.getInstance(props, null);
   transport   = mailSession.getTransport("smtp");

   String fromAddress = "from@yourdomain.com";
   String toAddress   = "to@somedomain.com";
   String subject     = "Test message subject";
   String body        = "Test message body";

   // Construct and send the e-mail message
   MimeMessage msg = new MimeMessage(mailSession);
   msg.setFrom(new InternetAddress(fromAddress));
   msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
   msg.setSentDate(new Date());
   msg.setSubject(subject);
   msg.setText(body);

   // Send the message
   transport.send(msg);
%>
If you don't see an exception report, then the message was sent successfully.
</body>
</html>

Rating
Did you find this article helpful?

Related Articles
Script Security Considerations - Please Read!
Finding and Installing Scripts
Spam and Virus Protection
Creating an E-mail Account
Creating Hosting Packages and Client Accounts

Home | Virtual Hosting | Reseller Hosting | Domain Names | Support | Our Company | Contact Us | Legal Notices