JavaMail发送邮件的小程序

时间:2024-01-04 18:37:50

首先加入jar包javax.mail.jar

package com.tideway.javamail;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties; import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; public class SendMail {
//收件人地址
private String to;
//发件人地址
private String from;
//SMTP服务器地址
private String smtpServer;
//登录副武器的用户名
private String username;
//登陆服务器的密码
private String password;
//邮件主题
private String subject;
//邮件正文
private String content;
//记录所有附件文件的集合,本人没写附件
List<String> list=new ArrayList<String>(); public SendMail(){} /**
* 初始化各属性构造器
* @param to 收件人地址
* @param from 发件人地址
* @param smtpServer SMTP服务器地址
* @param username 服务器登录用户名
* @param password 密码
* @param subject 标题
* @param content 内容
*/
public SendMail(String to,String from,String smtpServer,String username,
String password,String subject,String content){
this.to=to;
this.from=from;
this.smtpServer=smtpServer;
this.username=username;
this.password=password;
this.subject=subject;
this.content=content;
} public void setTo(String to) {
this.to = to;
} public void setFrom(String from) {
this.from = from;
} public void setSmtpServer(String smtpServer) {
this.smtpServer = smtpServer;
} public void setUsername(String username) {
this.username = username;
} public void setPassword(String password) {
this.password = password;
}
public void setSubject(String subject) {
this.subject = subject;
} public void setContent(String content) {
this.content = content;
}
//将附件文件名添加到List集合
public void attachfile(String filename){ list.add(filename);
} public boolean send(){
//创建Session需要的Properties对象
Properties pro=new Properties();
pro.put("mail.smtp.host", smtpServer);
pro.put("mail.smtp.auth", "true");
//创建Session对象
Session session=Session.getDefaultInstance(pro,new Authenticator() {
public javax.mail.PasswordAuthentication getPasswordAuthentication(){
return new javax.mail.PasswordAuthentication(username, password); }
});
try {
//构造MimeMessage并设置相关属性
MimeMessage msg=new MimeMessage(session);
//设置发件人地址
msg.setFrom(new InternetAddress(from));
//设置收件人
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
//设置邮件主题
msg.setSubject(subject);
//构造Multipart
Multipart mp=new MimeMultipart();
//向mp添加正文
MimeBodyPart mbpContent=new MimeBodyPart();
mbpContent.setText(content);
//将MimeBodyPart添加到Multipart容器中
mp.addBodyPart(mbpContent);
//附件附件附件附
//没写附件 //添加
msg.setContent(mp);
//设置发送时间
msg.setSentDate(new Date());
//发送
Transport.send(msg); } catch (MessagingException e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args) {
SendMail sendmail=new SendMail();
sendmail.setSmtpServer("smtp.163.com");
//设置登录服务器用户名
sendmail.setUsername("xxxxxxx");
//163邮箱开授权码的写授权码,没开的写邮箱密码
sendmail.setPassword("自己163邮箱授权码");
//给谁发
sendmail.setTo("xxxx@qq.com"); sendmail.setFrom("xxxxxxx@163.com"); sendmail.setSubject("你是一个不讲诚信的人"); sendmail.setContent("我的海底捞,我的海底捞,我的海底捞。"); if (sendmail.send()) {
System.out.println("success!");
}
}
}

注意:上面程序中创建的Session实例,使用了Autherenticator的匿名内部类,通过该类完成了SMTP的认证,少数邮件服务器在使用SMTP服务时不需要安全认证,则直接如下书写即可。

//创建Session需要的Properties对象
Properties pro=new Properties();
pro.put("mail.smtp.host", smtpServer);
pro.put("mail.smtp.auth", "false");//改成false.................
//创建Session对象
Session session=Session.getDefaultInstance(pro);