Java实现发送邮件
1、授权码+邮箱账号
a、添加maven依赖
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
b、测试邮件发送
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* java实现邮箱发送邮件功能
*
*
* @author WQ
*
*/
public class JavaMail {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.put("mail.smtp.host","smtp.163.com");// smtp服务器地址
Session session = Session.getInstance(props);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setSubject("这是一个测试程序....");
msg.setText("你好!这是我的第一个javamail程序---WQ");
msg.setFrom(new InternetAddress("183****1265@163.com"));//发件人邮箱(我的163邮箱)
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress("183****1265@163.com")); //收件人邮箱(我的QQ邮箱)
msg.saveChanges();
Transport transport = session.getTransport();
transport.connect("183****1265@163.com","d****56");//发件人邮箱,授权码(可以在邮箱设置中获取到授权码的信息)
transport.sendMessage(msg, msg.getAllRecipients());
System.out.println("邮件发送成功...");
transport.close();
}
}
163邮箱授权码位置
接收邮件
1、邮箱账号+邮箱密码
a、添加maven依赖
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
b、发送邮件测试
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 SendEmail {
public static String myEmailAccount = "****";//邮箱账号
public static String myEmailPassword = "****";//邮箱密码
public static String myEmailSMTPHost = "smtp.ym.163.com";//发送邮箱服务器地址(这个地址是网易企业邮箱的地址)
public static void sendEmail(String text,String receiveEmail) throws Exception {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", myEmailSMTPHost);
props.setProperty("mail.smtp.auth", "true");
final String smtpPort = "994";
props.setProperty("mail.smtp.port", smtpPort);
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.port", smtpPort);
Session session = Session.getDefaultInstance(props);
session.setDebug(true); // 设置为debug模式, 可以查看详细的发送 log
MimeMessage message = createMimeMessage(session, myEmailAccount, receiveEmail,text);
Transport transport = session.getTransport();
transport.connect(myEmailAccount, myEmailPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail,String text) throws Exception {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sendMail, "*****", "UTF-8"));
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail,"", "UTF-8"));
message.setSubject("邮箱验证", "UTF-8");
message.setContent(text, "text/html;charset=UTF-8");
message.setSentDate(new Date());
message.saveChanges();
return message;
}
public static void main(String[] args) throws Exception {
sendEmail("这是内容,验证码45845","183****1265@163.com");
}
}
接收邮件
注:1、QQ邮箱有时候会将邮件当成垃圾邮件,导致QQ邮箱不能正常获取邮件
2、不同的邮箱每天有限制的最高发送邮件数量,网易企业邮箱免费版是100条
3、如果超出了最高发送量,则会报错
DEBUG SMTP: MessagingException while sending, THROW:
com.sun.mail.smtp.SMTPSendFailedException: 554 5.5.0 MI:STC