吴裕雄--天生自然 JAVA开发学习:发送邮件
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*; public class SendEmail
{
public static void main(String [] args)
{
// 收件人电子邮箱
String to = "abcd@gmail.com"; // 发件人电子邮箱
String from = "web@gmail.com"; // 指定发送邮件的主机为 localhost
String host = "localhost"; // 获取系统属性
Properties properties = System.getProperties(); // 设置邮件服务器
properties.setProperty("mail.smtp.host", host); // 获取默认session对象
Session session = Session.getDefaultInstance(properties); try{
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session); // Set From: 头部头字段
message.setFrom(new InternetAddress(from)); // Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to)); // Set Subject: 头部头字段
message.setSubject("This is the Subject Line!"); // 设置消息体
message.setText("This is actual message"); // 发送消息
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*; public class SendHTMLEmail
{
public static void main(String [] args)
{ // 收件人电子邮箱
String to = "abcd@gmail.com"; // 发件人电子邮箱
String from = "web@gmail.com"; // 指定发送邮件的主机为 localhost
String host = "localhost"; // 获取系统属性
Properties properties = System.getProperties(); // 设置邮件服务器
properties.setProperty("mail.smtp.host", host); // 获取默认的 Session 对象。
Session session = Session.getDefaultInstance(properties); try{
// 创建默认的 MimeMessage 对象。
MimeMessage message = new MimeMessage(session); // Set From: 头部头字段
message.setFrom(new InternetAddress(from)); // Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to)); // Set Subject: 头字段
message.setSubject("This is the Subject Line!"); // 发送 HTML 消息, 可以插入html标签
message.setContent("<h1>This is actual message</h1>",
"text/html" ); // 发送消息
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*; public class SendFileEmail
{
public static void main(String [] args)
{ // 收件人电子邮箱
String to = "abcd@gmail.com"; // 发件人电子邮箱
String from = "web@gmail.com"; // 指定发送邮件的主机为 localhost
String host = "localhost"; // 获取系统属性
Properties properties = System.getProperties(); // 设置邮件服务器
properties.setProperty("mail.smtp.host", host); // 获取默认的 Session 对象。
Session session = Session.getDefaultInstance(properties); try{
// 创建默认的 MimeMessage 对象。
MimeMessage message = new MimeMessage(session); // Set From: 头部头字段
message.setFrom(new InternetAddress(from)); // Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to)); // Set Subject: 头字段
message.setSubject("This is the Subject Line!"); // 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart(); // 消息
messageBodyPart.setText("This is message body"); // 创建多重消息
Multipart multipart = new MimeMultipart(); // 设置文本消息部分
multipart.addBodyPart(messageBodyPart); // 附件部分
messageBodyPart = new MimeBodyPart();
String filename = "file.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart); // 发送完整消息
message.setContent(multipart ); // 发送消息
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
props.put("mail.smtp.auth", "true");
props.setProperty("mail.user", "myuser");
props.setProperty("mail.password", "mypwd");
import java.util.Properties; import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; public class SendEmail2
{
public static void main(String [] args)
{
// 收件人电子邮箱
String to = "xxx@qq.com"; // 发件人电子邮箱
String from = "xxx@qq.com"; // 指定发送邮件的主机为 smtp.qq.com
String host = "smtp.qq.com"; //QQ 邮件服务器 // 获取系统属性
Properties properties = System.getProperties(); // 设置邮件服务器
properties.setProperty("mail.smtp.host", host); properties.put("mail.smtp.auth", "true");
// 获取默认session对象
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("xxx@qq.com", "qq邮箱授权码"); //发件人邮件用户名、授权码
}
}); try{
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session); // Set From: 头部头字段
message.setFrom(new InternetAddress(from)); // Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to)); // Set Subject: 头部头字段
message.setSubject("This is the Subject Line!"); // 设置消息体
message.setText("This is actual message"); // 发送消息
Transport.send(message);
System.out.println("Sent message successfully....from runoob.com");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
吴裕雄--天生自然 JAVA开发学习:发送邮件的更多相关文章
- 吴裕雄--天生自然 JAVA开发学习:变量类型
public class Variable{ static int allClicks=0; // 类变量 String str="hello world"; // 实例变量 pu ...
- 吴裕雄--天生自然 JAVA开发学习:java使用Eclipel连接数据库
1:jar可到Mysql官网下载:地址Mysql 连接jar包.:https://dev.mysql.com/downloads/connector/j/如图,在下拉列表框中选择Platform In ...
- 吴裕雄--天生自然 JAVA开发学习:解决java.sql.SQLException: The server time zone value报错
这个异常是时区的错误,因此只你需要设置为你当前系统时区即可,解决方案如下: import java.sql.Connection ; import java.sql.DriverManager ; i ...
- 吴裕雄--天生自然 JAVA开发学习:基础语法
package test; public class temp { /* 第一个Java程序 * 它将打印字符串 Hello World */ public static void main(Stri ...
- 吴裕雄--天生自然 JAVA开发学习:Scanner 类
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanne ...
- 吴裕雄--天生自然 JAVA开发学习:流(Stream)、文件(File)和IO
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //使用 BufferedReader 在控制台读取 ...
- 吴裕雄--天生自然 JAVA开发学习:方法
/** 返回两个整型变量数据的较大值 */ public static int max(int num1, int num2) { int result; if (num1 > num2) re ...
- 吴裕雄--天生自然 JAVA开发学习:正则表达式
import java.util.regex.*; class RegexExample1{ public static void main(String args[]){ String conten ...
- 吴裕雄--天生自然 JAVA开发学习:日期时间
import java.util.Date; public class DateDemo { public static void main(String args[]) { // 初始化 Date ...
随机推荐
- 每天一点点之vue框架开发 - vue坑-This relative module was not found
94% asset optimization ERROR Failed to compile with 1 errors This relative module was not found: * . ...
- SSH限制与更改端口、限制ROOT方式登录
ssh中如何配置只允许某个IP以某个账号登录服务器 只要在ssh的配置文件:sshd_config中添加如下一行即可Allowusers username@192.168.1.100上述只允许IP地址 ...
- 【每日Scrum】第七天冲刺
一.计划会议内容 界面ui制作,主界面进度 二.任务看板 三.scrum讨论照片 四.产品的状态 无 五.任务燃尽图
- jstl中遍历Map
在jstl中遍历Map和遍历List与数组一样,都是使用forEach标签. 例子: <%@ page import="java.util.Map" %> <%@ ...
- 【系统安装】如何在VMware软件中安装ghost格式的系统
一.安装准备 1.系统虚拟环境软件:VMware软件(下载链接) 2.系统镜像制作软件:UltraISO[软碟通是光盘映像文件制作/编辑/转换工具](下载链接) 3.系统安装辅助软件:Win7PE(下 ...
- 基于Struts2开发公司职工管理系统 Java源码
开发环境: Windows操作系统 开发工具: MyEclipse+Jdk+Tomcat+MySql数据库 职工管理系统作为一种管理软件正在各公司中得到越来越广泛的应用,且已达到了良好效果. 运行效果 ...
- ASP.NET ZERO 学习 JTable的使用
View信息: @using Abp.Web.Mvc.Extensions @using MedicalSystem.Authorization @using MedicalSystem.Web.Na ...
- CodeForces 1287B Hyperset
N^2遍历所有得(i,j)然后可以根据(i,j)字符串构造出来第三个T字符串,然后查找一下是否有这个T存在即可,注意最后答案要/3因为会重复出现. #include <stdio.h> # ...
- [Algo] 118. Array Deduplication IV
Given an unsorted integer array, remove adjacent duplicate elements repeatedly, from left to right. ...
- 17.3.12----math模块
1----math模块提供很多的数学运算功能: math.pi 圆周率 math.e 那个自然常熟就是e^x,的这个e math.ceil(i) 对i向上取整 math.floor(i) ...