How do I send an e-mail in Java?

I need to send e-mails from a servlet running within Tomcat. I'll always send to the same recipient with the same subject, but with different contents.

What's a simple, easy way to send an e-mail in Java?

Related:

How do you send email from a Java app using GMail?


Here's my code for doing that:

import javax.mail.*;
import javax.mail.internet.*;

// Set up the SMTP server.
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", "smtp.myisp.com");
Session session = Session.getDefaultInstance(props, null);

// Construct the message
String to = "you@you.com";
String from = "me@me.com";
String subject = "Hello";
Message msg = new MimeMessage(session);
try {
    msg.setFrom(new InternetAddress(from));
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
    msg.setSubject(subject);
    msg.setText("Hi,\n\nHow are you?");

    // Send the message.
    Transport.send(msg);
} catch (MessagingException e) {
    // Error.
}

You can get the JavaMail libraries from Sun here: http://java.sun.com/products/javamail/

Java - Sending Email, This post will help you to make the first steps with Jakarta Mail (previously known as JavaMail) to send emails from Java apps. Also, we review� 1. Send Email. Send a normal email in text format. 2. Send Email in HTML format. The key is create a custom javax.activation.DataSource to handle HTML format. 3. Send Email with Attachment. 3.1 The key is creating MimeBodyPart and add it into Multipart.


JavaMail can be a bit of a pain to use. If you want a simpler, cleaner, solution then have a look at the Spring wrapper for JavaMail. The reference docs are here:

http://static.springframework.org/spring/docs/2.5.x/reference/mail.html

However, this does mean you need Spring in your application, if that isn't an option then you could look at another opensource wrapper such as simple-java-mail:

simplejavamail.org

Alternatively, you can use JavaMail directly, but the two solutions above are easier and cleaner ways to send email in Java.

Sending Emails Using Java, In this quick tutorial, we're going to look at sending an email with and without attachments – using the core Java mail library. 2. Project Setup and� Steps to send email using JavaMail API. Get the session object. Compose the message. send the message. Simple example of sending email using JavaMail API. Example of sending email using JavaMail API through the SMTP server provided by the host provider. There are various ways to send email using JavaMail API.


Yet another option that wraps the Java Mail API is Apache's commons-email.

From their User Guide.

SimpleEmail email = new SimpleEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();

Sending Emails with Java, There are various ways to send email using JavaMail API. For this purpose, you must have SMTP server that is responsible to send mails. You can use one of� After adding these files, follow the below steps and write a java program to send email: Create a new session object by calling getDefaultInstance () method and passing properties as argument to get all of the important properties like hostname of the SMTP server etc.


Example of sending email in Java, Send a normal email in text format. SendEmailSMTP.java. package com.mkyong; import com.sun� To followup on jon's reply, here's an example of sending a mail using simple-java-mail.. The idea is that you don't need to know about all the technical (nested) parts that make up an email.


I usually define my javamail session in the GlobalNamingResources section of tomcat's server.xml file so that my code does not depend on the configuration parameters:

<GlobalNamingResources>
    <Resource name="mail/Mail" auth="Container" type="javax.mail.Session"
              mail.smtp.host="localhost"/>
    ...
</GlobalNamingResources>

and I get the session via JNDI:

    Context context = new InitialContext();
    Session sess = (Session) context.lookup("java:comp/env/mail/Mail");

    MimeMessage message = new MimeMessage(sess);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    message.setSubject(subject, "UTF-8");
    message.setText(content, "UTF-8");
    Transport.send(message);

Java - How to send Email, mail . Sending a Plain Text Email. Although our example email below isn't as exciting as the well-designed emails with custom buttons and pictures,� The main method specifies the SMTP email server host to send the email to. This is the host that I specified in my hosts file and configured MailEnable to use. I specify the 'to' address, a 'cc' address, a 'from' address, the 'subject', and the 'text' (email body). An email 'Session' object is responsible for managing messages.


How to Send Emails in Java, The following code works very well with Google SMTP server. You need to supply your Google username and password. import com.sun.mail.smtp. I am trying to send an email using Java and Gmail. I have stored my files on the cloud and the stored files I want to send as an attachment to my email. It should add those files to this mail and


Send email using java, Today we will look into JavaMail Example to send email in java programs. Sending emails is� But my Outlook 2010 obviously can send these emails. I was wondering if there is a way to automate Outlook 2010 via Java code so that Outlook can send the emails ? I know stuff like "mailto" can be used pop-up the default Outlook send dialog with pre-populated info, but I am looking for a way to have the send operation happen behind the scenes.


JavaMail Example - Send Mail in Java using SMTP, Detailed steps on how to setup JavaMail in your Java Project and implement JavaMail API to build and send emails on SMTP protocol. Sending Email in Java through Gmail Server. We can send email by using the SMTP server of gmail. It is good if you are don't have any SMTP server and reliable. Here we will learn how to send email through gmail server by SSL (Secured Socket Layer). SSL is basically used for security if you are sending email through gmail server.