import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendMailSSL {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("sender@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("receiver@gmail.com"));
message.setSubject("Testing Subject");
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("This is message body");
// Create a multipar message
Multipart multipart = new MimeMultipart();
String filename = "./sample.png"; // file name and location
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
分享到:
相关推荐
NULL 博文链接:https://tgwall.iteye.com/blog/1735704
SendmailViaSMTP 是一个在终端中通过 SMTP 帐号来发送邮件的工具。目前主要功能:命令行下通过smtp帐号发送邮件可以接收管道信息为邮件正文tls支持;gmail邮件帐号支持。批量任务首选,服务器端配合脚本使用效果更佳...
封装了一个java mail的jar包,只需设置邮件的基本信息 就可发邮件: //这个类主要是设置邮件 MailSenderInfo mailInfo = new MailSenderInfo(); mailInfo.setMailServerHost("smtp.163.com"); mailInfo....
Delphi Send Email with Gmail 2021
NULL 博文链接:https://javaeedevelop.iteye.com/blog/1726261
功能描述: 1、发送简单文本邮件 2、发送带附件邮件,本地图片 3、发送带附件邮件,网络图片 4、发送带附件邮件,网络图片2 5、发送带HTML文本邮件
java mail 邮件发送,可发送附件;多个附件以,号分开;可抄送、密送等多个抄送、密送以,号分开。详细发送DEMO参考SendMailTest.java类: SendMailInfo mailInfo = new SendMailInfo("mail.china1168.com", true, ...
java SMS for send and receive
java mail, java send mail form with javax mail sender, build with Gradle , servlet 采用 gradle 管理依赖, junit单元测试, servlet , jsp等技术; 邮件发送账号在统一 的 properties文件中配置,方便集成管理...
基于Maven的javax.mail发送邮件例子程序 pom.xml添加依赖jar: <groupId>javax.mail <artifactId>mail ${javamail.version} </dependency> <javamail.version>1.4.1 /* 功能:加入了参考文章中没有...
JAVA UDP SendString TEST OK
Java课程设计 Java实现邮件的加密发送
NULL 博文链接:https://wendli2008.iteye.com/blog/1131998
Send Mail2000(绝对源代码)
在您的Gmail帐户中通过电子邮件发送标签.. 通过Gmail发送,您可以发送当前选项卡的屏幕截图。因为通过Gmail发送通过OAuth安全地集成到您在Chrome浏览器中登录的用户,整合非常简单。使用此扩展程序与朋友或同事共享...
一个较好的基于Socket发送邮件类,内部屏蔽初始化和清除网络代码,需在工程内做此动作才可用该类
included with the 1.2.1 version of the Java 2 Platform, Enterprise Edition (J2EE), so it is still commonly used. The version of the JavaMail API you want to use affects what you download and install...
在unix自己的目录下自动记录emailaddress。自动发送标题邮件#######
JAVA中socket的使用,本界面程序用于传输浮点数据,当收到一个"开始”命令后,开始产生随机数,并将其打包为502个字节的帧,然后发送出去,并每隔一定的时间将发送一次。
- name : Send mail uses : dawidd6/action-send-mail@v3 with : # Required mail server address: server_address : smtp.gmail.com # Required mail server port: server_port : 465 # Required mail ...