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. adfs环境安装

    安装文档参考: https://docs.microsoft.com/zh-cn/windows-server/identity/ad-fs/deployment/set-up-the-lab-env ...

  2. Tomcat跨域

    先下载 cors-filter-2.6.jar  2.java-property-utils-1.9.1.jar,这两个文件有些在csdn上积分太高,有些要百度网盘,还要下载百度网盘客户端,太麻烦,直 ...

  3. Python __name__="__main__"的作用

    该语句加在模块的最后,可以让这个模块,即可以被别人import,又可以直接运行. fibo.py文件: def fibo(): pass # fibo函数的内容 if __name__==" ...

  4. SpringMVC_执行原理

    什么是SpringMVC 概述 Spring MVC是Spring Framework的一部分,是基于Java实现MVC的轻量级Web框架. 查看官方文档:https://docs.spring.io ...

  5. with和上下文管理器

    with和上下文管理器 如果你有时间阅读源码的习惯,可能会看到一些优秀的代码会出现带有with关键字的语句. 对于系统资源如文件,数据库连接,socket而言,应用程序打开这些资源并执行完业务逻辑之后 ...

  6. {转}理解HTTP/304响应

    源文(英):http://www.telerik.com/blogs/understanding-http-304-responses 中文译文:http://www.cnblogs.com/ziyu ...

  7. nodejs(11)Express 中进行数据库操作

    配置 MySql 数据库环境 mysql 第三方模块的介绍和基本配置 要安装操作数据库的第三方包npm i mysql -S 导入 包 const mysql = require('mysql') 创 ...

  8. 纯CSS导航栏下划线跟随效果

    参考文章 <ul> <li>111</li> <li>2222</li> <li>3333333</li> < ...

  9. i春秋-web- 爆破2

    题目:flag不在变量中. 打开链接: <?php include "flag.php"; $a = @$_REQUEST['hello']; eval( "var ...

  10. centos7.4 测试CPU压力--命令搞定

    直接输入命令CPU消耗增加: cat /dev/urandom | gzip - > /dev/null 停止: 直接Ctrl+c结束