Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, January 31, 2016

How do I send an e-mail in Java?

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?

Answer by Jherico for How do I send an e-mail in Java?


use the Java Mail library

import javax.mail.*    ...    Session mSession = Session.getDefaultInstance(new Properties());  Transport mTransport = null;  mTransport = mSession.getTransport("smtp");  mTransport.connect(cServer, cUser, cPass);  MimeMessage mMessage = new MimeMessage(mSession);  mTransport.sendMessage(mMessage,  mMessage.getAllRecipients());  mTransport.close();  

This is a truncated version of the code I use to have an application send emails. Obviously, putting a body and recipients in the message before sending it is probably going to suit you better.

The maven repository location is artifactId: javax.mail, groupId: mail.

Answer by RichieHindle for How do I send an e-mail in Java?


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/

Answer by Jon for How do I send an e-mail in Java?


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:

https://github.com/bbottema/simple-java-mail

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

Answer by Steve K for How do I send an e-mail in Java?


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();  

Answer by Yishai for How do I send an e-mail in Java?


JavaMail is great if you can rely on an outside SMTP server. If, however, you have to be your own SMTP server, then take a look at Asprin.

Answer by Maurice Perry for How do I send an e-mail in Java?


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:

            ...    

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);  

Answer by user109771 for How do I send an e-mail in Java?


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. In that sense it's a lot like Apache's commons-email, except that Simple Java Mail is a little bit more straightforward than Apache's mailing API when dealing with attachments and embedded images. Spring's mailing facility works as well but is a bit awkward in use (for example it requires an anonymous innerclass) and ofcourse you need to a dependency on Spring which gets you much more than just a simple mailing library, since it its base it was designed to be an IOC solution.

Simple Java Mail btw is a wrapper around the JavaMail API.

final Email email = new Email();    email.setFromAddress("lollypop", "lolly.pop@somemail.com");   email.setSubject("hey");  email.addRecipient("C. Cane", "candycane@candyshop.org", RecipientType.TO);  email.addRecipient("C. Bo", "chocobo@candyshop.org", RecipientType.BCC);   email.setText("We should meet up! ;)");   email.setTextHTML("We should meet up!");    // embed images and include downloadable attachments   email.addEmbeddedImage("wink1", imageByteArray, "image/png");  email.addEmbeddedImage("wink2", imageDatesource);   email.addAttachment("invitation", pdfByteArray, "application/pdf");  email.addAttachment("dresscode", odfDatasource);    new Mailer("smtp.host.com", 25, "username", "password").sendMail(email);  // or alternatively, pass in your own traditional MailSession object.  new Mailer(preconfiguredMailSession).sendMail(email);  

Answer by SuperSaiyan for How do I send an e-mail in Java?


Here is the simple Solution

Download these jars: 1. Javamail 2. smtp 3. Java.mail

Copy and paste the below code from [http://javapapers.com/core-java/java-email/][1]

Edit the ToEmail, Username and Password (Gmail User ID and Pwd)


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.