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. 每天一点点之vue框架开发 - 数据渲染-for循环中动态设置页面背景色

    实现方式很简单,在属性前加:,表示绑定 :style="{'background':item.bgColor} 代码样例: <li v-for="item in laber_ ...

  2. 51nod 1438:方阵与完全平方数

    1438 方阵与完全平方数 题目来源: mostleg 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 如果一个由正整数组成的n*n的方阵,满足以下 ...

  3. SpringBoot项目 org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Jetty servlet container报错

    SpringBoot项目启动报错 ERROR 2172 --- [ main] o.s.boot.SpringApplication : Application startup failed org. ...

  4. mysql时间类型总结及常用时间函数

    日期时间和类型 常用日期和时间类型 字节 year                1       表示年份                   值范围:(1901----2155) date     ...

  5. 微信小程序返回页面传值

    一.通过url传递参数,但由于navigateTo无法跳转到导航页,所以无法往导航页传递参数 wx.navigateTo({ url: 'test?id=1' }) 二.通过wx.navigateBa ...

  6. NABCD模型——星遇

    我们项目是个面向希望有新奇体验的用户的社交软件,致力于打造不一样的有趣的社交. 发表后一周预计用户量:1000人 N:(Need,需求) 目前主流社交软件由于时间原因体量越来越大,各种繁琐而不必要的功 ...

  7. 2018.11.16javascript课上随笔(DOM)

    <li> <a href = "“#”>-</a> </li> <li>子节点:文本节点(回车),元素节点,文本节点. 不同节点树 ...

  8. Git 报错:fatal: refusing to merge unrelated histories

    背景:[接上篇git push 出错的随笔]当 pull 远端仓库到本地的时候,出现以下错误: 错误情况: 出错原因:主要原因还是在于本地仓库和远程仓库实际上是独立的两个仓库,假如我之前是直接以 cl ...

  9. JavaWeb开发中遇到问题汇总

    一.基础问题 1.${pageContext.request.contextPath}获取相对路径无效 问题:在jsp中使用${pageContext.request.contextPath}获取相对 ...

  10. IOS下的safari不支持localStorage?

    同事在统计日志的时候,想用localStorag去记载一些什么,但是在各大浏览器都运行的良好的基础上,唯独IOS下的safari一直静静无声,没有任何反应.打印localStorage都是Object ...