用Java实现最简单的邮件发送代码

1. 
import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

/*
* 在真正使用创建的过程中,往往会让我们验证密码,这是我们要写一个密码验证类。javax.mail.Authenticator是一个抽象类,
* 我们要写MyAuthenticator的密码验证类,该类继承Authenticator实现:
* */
//用户名密码验证,需要实现抽象类Authenticator的抽象方法PasswordAuthentication
public class MyAuthenricator extends Authenticator {
String u = null;
String p = null;
public MyAuthenricator(String u, String p){
this.u=u;
this.p=p;
}

@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(u,p);
}
}




2.
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

public class MailExample {
public static void main(String[] args) throws Exception {
/**
* 1.配置发件人邮箱信息以及创建一个Java 配置类存放SMTP服务器地址等参数。
*/
String sendEmailAccount = "[email protected]"; // 发件人邮箱
String sendEmailPassword = "zxc751953."; // 发件人密码
String sendEmailSMTPHost = "smtp.163.com"; // 发件人邮箱的 SMTP 服务器地址, 此处为Outlook邮箱的SMTP服务器
String receiveMailAccount = "[email protected]"; // 收件人邮箱
Properties props = new Properties(); // 使用Java配置类进行配置
props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
props.setProperty("mail.smtp.host", sendEmailSMTPHost); // 发件人的邮箱的 SMTP 服务器地址
props.setProperty("mail.smtp.auth", "true"); // 需要请求认证
final String smtpPort = "25"; // 默认端口号设置为587,也可以设置为465,具体取决于SMTP服务器要求的端口号
props.setProperty("mail.smtp.port",smtpPort );
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.starttls.enable", "true");
props.setProperty("mail.smtp.socketFactory.port", smtpPort );

/**
* 2.创建一个同邮件服务器交互的session
*/
Session session = Session.getDefaultInstance(props, new MyAuthenricator("d9_felix", "Gg9999999"));
session.setDebug(true);
MimeMessage message = new MimeMessage(session); // 1. 创建一封邮件
message.setFrom(new InternetAddress(sendEmailAccount, "ExampleFrom", "UTF-8")); // 2. From: 发件人
message.setRecipient(MimeMessage.RecipientType.TO,
new InternetAddress(receiveMailAccount, "ExampleUser", "UTF-8")); // 3. To: 收件人
message.setSubject("", "UTF-8"); // 4. Subject: 邮件主题(标题有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改标题)
message.setContent("<h3>This is a test email.</h3>", "text/html;charset=UTF-8"); // 5. Content: 邮件正文
message.setSentDate(new Date()); // 6. 设置邮件发件时间
message.saveChanges(); // 7. 保存设置

/**
* 3.创建一封格式化的邮件
*/
Transport transport = session.getTransport(); // 1. 根据 Session 获取邮件传输对象
transport.connect(sendEmailAccount, sendEmailPassword); // 2. 使用 邮箱账号 和 密码 连接邮件服务器
transport.sendMessage(message, message.getAllRecipients()); // 3. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人,
transport.close(); // 4. 关闭连接
}
}

因为这是很早之前写的记录 当时记录比较粗糙 没有过细 等以后有时间了再过来细化吧

猜你喜欢

转载自www.cnblogs.com/YinXuanZhiZhi9/p/12719170.html