使用java代码发送电子邮件

使用java代码发送电子邮件,java,email,Java,Email,有没有一种简单的方法可以从java代码发送电子邮件?有几种方法可以做到这一点,最好的方法是使用java mail。 有关更多信息,请参见本示例: 您可以使用或(构建在JavaMail之上) 以下是Commons Mail的一个简单示例,摘自: 如果您想要一个直接到点的邮件API和/或希望能够访问POP3,而不仅仅是SMTP,请查看。如何使用它是涵盖在它的优秀 如果您想要一个不那么臃肿、更方便的API来发送邮件,那么就在JavaMail API的基础上构建头部。its中介绍了如何使用它。试试这个标

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

有几种方法可以做到这一点,最好的方法是使用java mail。 有关更多信息,请参见本示例:

您可以使用或(构建在JavaMail之上)

以下是Commons Mail的一个简单示例,摘自:


如果您想要一个直接到点的邮件API和/或希望能够访问POP3,而不仅仅是SMTP,请查看。如何使用它是涵盖在它的优秀


如果您想要一个不那么臃肿、更方便的API来发送邮件,那么就在JavaMail API的基础上构建头部。its中介绍了如何使用它。

试试这个标准!似乎有一个

关于它有一个很好的短期课程:“-试试看

它包含代码片段以及它们和邮件协议的解释(以防开发人员不知道它们各自的行为)。

有一个库。发送电子邮件的代码很容易编写,您甚至可以使用模板来编写电子邮件的内容。它比其他库更容易使用,因为您不需要处理技术问题(如HTML中的内联图像和样式,它是自动完成的)。 您甚至可以使用SMTP服务器在本地测试代码,以在通过SMTP提供商发送电子邮件之前检查电子邮件的结果。 可以使用SMTP协议或通过提供程序API(如SendGrid)发送电子邮件

包fr.sii.ogham.sample.standard.email;
导入java.util.Properties;
导入fr.sii.ogham.core.builder.MessagingBuilder;
导入fr.sii.ogham.core.exception.MessaginException;
导入fr.sii.ogham.core.service.MessagingService;
导入fr.sii.ogham.email.message.email;
公共类基本示例{
公共静态void main(字符串[]args)抛出MessaginException{
//[准备]只需在应用程序启动时执行一次即可
//配置属性(可以存储在属性文件中或定义
//系统内属性)
属性=新属性();
properties.put(“mail.smtp.host”,“”);
properties.put(“mail.smtp.port”,”);
properties.put(“ogham.email.from.default值”,”;
//使用默认行为和
//提供的属性
MessagingService=MessagingBuilder.standard()//
.环境()
.物业(物业)//
.及()
.build();//
//[/准备]
//[发送电子邮件]
//使用fluentapi发送电子邮件
service.send(新电子邮件()//
.主题(“基本样本”)
.body().string(“电子邮件内容”)
.to(“ogham-test@yopmail.com"));
//[/发送电子邮件]
}
}

还有很多其他的和/

你对通过谷歌找到的一个教程有问题吗?->这是一个重复做谷歌搜索,你会发现一些样本
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();
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]
    }
}