SMTP之JAVA调用示例__SMTP接口说明_邮件推送-阿里云
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class SimpleAliDMSendMail {
private static final String ALIDM_SMTP_HOST = "smtpdm.aliyun.com";
private static final int ALIDM_SMTP_PORT = 25;
public static void main(String[] args) throws MessagingException {
// 配置发送邮件的环境属性
final Properties props = new Properties();
// 表示SMTP发送邮件,需要进行身份验证
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", ALIDM_SMTP_HOST);
props.put("mail.smtp.port", ALIDM_SMTP_PORT);
// 如果使用ssl,则去掉使用25端口的配置,进行如下配置,
// props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// props.put("mail.smtp.socketFactory.port", "465");
// props.put("mail.smtp.port", "465");
// 发件人的账号
props.put("mail.user", "***");
// 访问SMTP服务时需要提供的密码
props.put("mail.password", "***");
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);
// 设置收件人
InternetAddress to = new InternetAddress("***");
message.setRecipient(MimeMessage.RecipientType.TO, to);
// 设置邮件标题
message.setSubject("测试邮件");
// 设置邮件的内容体
message.setContent("测试的HTML邮件", "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
}
}
最后更新:2016-11-24 11:23:48
上一篇:
SMTP服务地址__SMTP接口说明_邮件推送-阿里云
下一篇:
SMTP之CSharp调用示例__SMTP接口说明_邮件推送-阿里云
功能实时性__常见问题_日志服务-阿里云
在哪里可以查到SLB的权限定义___负载均衡(SLB)授权问题_授权常见问题_访问控制-阿里云
编辑负载均衡实例名称__实例管理_用户指南_负载均衡-阿里云
添加域名__域名操作_产品使用手册_云解析-阿里云
扩容磁盘__磁盘相关接口_API 参考_云服务器 ECS-阿里云
示例项目__Java SDK_SDK参考_E-MapReduce-阿里云
步骤 4:格式化数据盘__快速入门(Windows)_云服务器 ECS-阿里云
SecurityPreference__数据类型_RAM API文档_访问控制-阿里云
Linux实例挂载数据盘报错__故障处理_云服务器 ECS-阿里云
阿里云争抢万亿物联网市场 亚马逊、微软“云”业务或遭冲击
相关内容
常见错误说明__附录_大数据计算服务-阿里云
发送短信接口__API使用手册_短信服务-阿里云
接口文档__Android_安全组件教程_移动安全-阿里云
运营商错误码(联通)__常见问题_短信服务-阿里云
设置短信模板__使用手册_短信服务-阿里云
OSS 权限问题及排查__常见错误及排除_最佳实践_对象存储 OSS-阿里云
消息通知__操作指南_批量计算-阿里云
设备端快速接入(MQTT)__快速开始_阿里云物联网套件-阿里云
查询API调用流量数据__API管理相关接口_API_API 网关-阿里云
使用STS访问__JavaScript-SDK_SDK 参考_对象存储 OSS-阿里云