Sending email using java code
is there an easy way to send email from java code 开发者_StackOverflow?
You can use JavaMail or CommonsEmail (which is built on top of JavaMail)
Here is a simple example with Commons Mail, taken from this page :
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();
If you want a bare to-the-point mailing API and/or want to be able to reach POP3 as well instead of only SMTP, have a look at JavaMail API. How to use it is covered in its excellent FAQ.
If you want a less bloated and more convenient API to send mails, head to Apache Commons Email which in turn is built on top of JavaMail API. How to use it is covered in its User Guide.
There are several ways to do that, best way is to use java mail. Look in this example for more info: http://www.javacommerce.com/displaypage.jsp?name=javamail.sql&id=18274
Try the standard JavaMail API! There seems to be a quickstart
There's a good short course about it: "jGuru: Fundamentals of the JavaMail API" - try it out.
it contains code snippets as well as explanations of them and of the Mail protocols (in case the dev. doesn't know how each one of them behaves).
There is Ogham library. The code to send email is easy to write and you can even use a template to write the content of your email. It is easier to use than other libraries because you don't need to handle technical concerns (like inlining images and styles in the HTML, it is done automatically). You can even test your code locally with a SMTP server to check the result of your email before sending it through a SMTP provider. The email can be sent either using SMTP protocol or through providers API (like SendGrid).
package fr.sii.ogham.sample.standard.email;
import java.util.Properties;
import fr.sii.ogham.core.builder.MessagingBuilder;
import fr.sii.ogham.core.exception.MessagingException;
import fr.sii.ogham.core.service.MessagingService;
import fr.sii.ogham.email.message.Email;
public class BasicSample {
public static void main(String[] args) throws MessagingException {
// [PREPARATION] Just do it once at startup of your application
// configure properties (could be stored in a properties file or defined
// in System properties)
Properties properties = new Properties();
properties.put("mail.smtp.host", "<your server host>");
properties.put("mail.smtp.port", "<your server port>");
properties.put("ogham.email.from.default-value", "<email address to display for the sender user>");
// Instantiate the messaging service using default behavior and
// provided properties
MessagingService service = MessagingBuilder.standard() // <1>
.environment()
.properties(properties) // <2>
.and()
.build(); // <3>
// [/PREPARATION]
// [SEND AN EMAIL]
// send the email using fluent API
service.send(new Email() // <4>
.subject("BasicSample")
.body().string("email content")
.to("ogham-test@yopmail.com"));
// [/SEND AN EMAIL]
}
}
There are many other features and samples / spring samples.
精彩评论