简体   繁体   English

使用java代码发送电子邮件

[英]Sending email using java code

有没有一种简单的方法可以从 Java 代码发送电子邮件?

You can use JavaMail or CommonsEmail (which is built on top of JavaMail)您可以使用JavaMailCommonsEmail (建立在 JavaMail 之上)

Here is a simple example with Commons Mail, taken from this page :这是 Commons Mail 的一个简单示例,取自此页面

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 .如果您想要一个裸露的点对点邮件 API 和/或希望能够访问 POP3 而不仅仅是 SMTP,请查看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.如果您想要一个不那么臃肿且更方便的 API 来发送邮件,请前往Apache Commons Email ,它又建立在 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.有几种方法可以做到这一点,最好的方法是使用java邮件。 Look in this example for more info: http://www.javacommerce.com/displaypage.jsp?name=javamail.sql&id=18274查看此示例以获取更多信息: http : //www.javacommerce.com/displaypage.jsp?name= javamail.sql& id=18274

Try the standard JavaMail API !试试标准的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.有关于它的一个很好的短期课程:“ jGuru:JavaMail API 的基础知识” - 试试看。

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).它比其他库更易于使用,因为您不需要处理技术问题(例如在 HTML 中内联图像和样式,它是自动完成的)。 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.您甚至可以使用 SMTP 服务器在本地测试您的代码,以在通过 SMTP 提供商发送电子邮件之前检查您的电子邮件的结果。 The email can be sent either using SMTP protocol or through providers API (like SendGrid).可以使用 SMTP 协议或通过提供程序 API(如 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 .还有许多其他功能示例/ 弹簧示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM