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开发学习:发送邮件的更多相关文章

  1. 吴裕雄--天生自然 JAVA开发学习:变量类型

    public class Variable{ static int allClicks=0; // 类变量 String str="hello world"; // 实例变量 pu ...

  2. 吴裕雄--天生自然 JAVA开发学习:java使用Eclipel连接数据库

    1:jar可到Mysql官网下载:地址Mysql 连接jar包.:https://dev.mysql.com/downloads/connector/j/如图,在下拉列表框中选择Platform In ...

  3. 吴裕雄--天生自然 JAVA开发学习:解决java.sql.SQLException: The server time zone value报错

    这个异常是时区的错误,因此只你需要设置为你当前系统时区即可,解决方案如下: import java.sql.Connection ; import java.sql.DriverManager ; i ...

  4. 吴裕雄--天生自然 JAVA开发学习:基础语法

    package test; public class temp { /* 第一个Java程序 * 它将打印字符串 Hello World */ public static void main(Stri ...

  5. 吴裕雄--天生自然 JAVA开发学习:Scanner 类

    import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanne ...

  6. 吴裕雄--天生自然 JAVA开发学习:流(Stream)、文件(File)和IO

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //使用 BufferedReader 在控制台读取 ...

  7. 吴裕雄--天生自然 JAVA开发学习:方法

    /** 返回两个整型变量数据的较大值 */ public static int max(int num1, int num2) { int result; if (num1 > num2) re ...

  8. 吴裕雄--天生自然 JAVA开发学习:正则表达式

    import java.util.regex.*; class RegexExample1{ public static void main(String args[]){ String conten ...

  9. 吴裕雄--天生自然 JAVA开发学习:日期时间

    import java.util.Date; public class DateDemo { public static void main(String args[]) { // 初始化 Date ...

随机推荐

  1. Sequence Models Week 2 Operations on word vectors

    Operations on word vectors Welcome to your first assignment of this week! Because word embeddings ar ...

  2. STM32速度---网页讲解

    1. ① USART串口,若最大波特率只需115.2k,那用2M的速度就够了,既省电也噪声小. ② I2C接口,若使用400k波特率,若想把余量留大些,可以选用10M的GPIO引脚速度. ③ SPI接 ...

  3. pytorch 自动求梯度

    自动求梯度 在深度学习中,我们经常需要对函数求梯度(gradient).PyTorch提供的autograd包能够根据输入和前向传播过程自动构建计算图,并执行反向传播.本节将介绍如何使用autogra ...

  4. JAVA初学者——Hello,World!

    大家好,我是浩宇大熊猫 我本科专业学的是GIS(Geographical Information System),大学期间也学习了很多的编程语言,有C/C++/JAVA等 之前给我们授课的是韩冰老师, ...

  5. oracle 向表中插入BLOB类型数据

    提示: 待插入图片必须保存到oracle主机路径上. 步骤: 1.SYSDBA权限用户创建图片所在目录 CREATE OR REPLACE DIRECTORY TEST_DIR AS 'C:\Pict ...

  6. JavaScript 之 原型及原型链

    对象[回顾] 通过字面量创建对象 //通过字面量创建对象 var obj1 = { name:'Jack', age: 18 } 通过系统自带的构造函数构造对象 // 通过系统自带的构造函数构造对象 ...

  7. js对象属性名和属性值生成新数组时都作为属性值

    const obj = { id:1, name:'zhangsan', age:18 } const arr = []; Object.getOwnPropertyNames(obj).forEac ...

  8. 康冕峰IT技术总结博客CSDN索引

    计算1-x内的质数, 结果保存在mysql中. Java 程序员面试笔试宝典 4.1基础知识https://blog.csdn.net/qq_40993412/article/details/1040 ...

  9. vue安装插件指定版本

    安装插件指定版本 npm install 插件名称@2.9.6 --save 查看需要安装插件的版本记录 npm view 插件名称 versions --json

  10. python版本,执行

    01. 第一个 HelloPython 程序 1.1 Python 源程序的基本概念 Python 源程序就是一个特殊格式的文本文件,可以使用任意文本编辑软件做 Python 的开发 Python 程 ...