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. 读写锁ReentrantReadWriteLock源代码浅析

    1.简介 并发中常用的ReentrantLock,是一种典型的排他锁,这类锁在同一时刻只允许一个线程进行访问,实际上将并行操作变成了串行操作.在并发量大的业务中,其整体效率.吞吐量不能满足实现的需要. ...

  2. VS2013的工程移植到VS2008

    VS2013的工程完成后,用VS2008创建一个名称一样的工程(大小写也一样). 具体过程参考http://blog.csdn.net/sz76211822/article/details/42775 ...

  3. xv6 系统调用

    1. 系统调用的实现 开发程序需所有的接口在user.h中,包含两部分system call和ulib user.h中的系统接口函数在usys.S中通过汇编实现 #define SYSCALL(nam ...

  4. request对象和response对象的作用和相关方法

    response对象(响应) 响应行 状态码 :setStatus(int a) 设置状态码 302重定向 304控制缓存 响应头 setHeader() 一个key对应一个value addHead ...

  5. Springboot整合mongodb时无法连接数据库

    由于之前没有接触过mongodb,最近在学习时遇到了一些问题.用yml配置mongodb如下: spring: application: name:xc-service-manage-cms data ...

  6. [极客大挑战 2019]EasySQL

    万能密码直接登陆得到flag admin' or 1=1 #

  7. Maven--Cargo远程部署

    参考:https://www.digitalocean.com/community/questions/how-to-access-tomcat-8-admin-gui-from-different- ...

  8. elk常用知识点

    1.让文件每次都从头读取: 当有时候需要重新抽取全日志时候,则需要先删除ES上的索引,然后在logstash上input插件配置sincedb_path => "/dev/null&q ...

  9. 条款02:尽量以const,enum,inline替换#define

    目录 1. 总结 2. 使用const常量或enum替换宏常量 class外部的常量指针 class专属常量 1. 总结 对于单纯常量,最好以const常量或enum替换#define 对于宏代码段, ...

  10. Windows Java桌面应用程序集成slf4j实现日志持久化

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 Windows上一般的应用程序也可以通过日志系统打印日志到指定文件.通过这个例子想说明,问题处理的方法是多 ...