邮件收发要用到的两个jar包:链接: http://pan.baidu.com/s/1c16bzhu 密码: 5r7p
eclipse导入jar包教程:http://jingyan.baidu.com/article/ca41422fc76c4a1eae99ed9f.html
简易版本邮件发送:
package whut.mailsender;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
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;
import whut.mailreceiver.MailAuthenticator;
public class SimpleSMTPSender {//邮件发送
//简单邮件传输协议(simple mail transferprotocol,SMTP)
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Properties props = new Properties();
//传递一个发送邮件的服务器名 例如: smtp.163.com
props.put("mail.smtp.host", "smtp.163.com");
//props.put("mail.smtp.auth", true);
//对于发送邮件, 只要保证发送人所在的邮件服务器正确打开就可以
//受信任的邮箱可以是任意地址
//创建一个程序与邮件服务器之间的通信会话(Session)
Session mailConnection = Session.getInstance(props, null);
Message msg = new MimeMessage(mailConnection);
//设置发送人和接收人
Address sender = new InternetAddress("..."); //发件人邮箱
Address receiver = new InternetAddress("..."); // 收件人邮箱
msg.setFrom(sender);//设置邮件来源, 用发送人sender初始化
msg.setRecipient(Message.RecipientType.TO, receiver); //设置邮件发送去向, 用收件人receiver初始化
msg.setSubject("......................."); //设置邮件主题
//设置邮件正文
String sss = "..................";
msg.setContent(sss,"text/html;charset = gb2312");
//先进行邮件存储
msg.saveChanges();
Transport trans = mailConnection.getTransport("smtp");
String username = "................."; //发信人地址
String pw = ".........."; //发件地址对应的密码
//邮件服务器名, 用户名, 密码
trans.connect("smtp.163.com", username, pw);
trans.sendMessage(msg, msg.getAllRecipients()); //发送的信息
}catch(Exception e)
{
System.err.println(e);
}
finally{
System.exit(0);
}
}
}
带附件发送,以及群发邮件写法:
package whut.mailsender;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
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;
import whut.mailreceiver.MailAuthenticator;
public class SimpleSMTPSender {//邮件发送
//简单邮件传输协议(simple mail transferprotocol,SMTP)
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Properties props = new Properties();
//传递一个发送邮件的服务器名 例如: smtp.163.com
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.auth", "true");
//对于发送邮件, 只要保证发送人所在的邮件服务器正确打开就可以
//受信任的邮箱可以是任意地址
//创建一个程序与邮件服务器之间的通信会话(Session)
Session mailConnection = Session.getInstance(props, null);
Message msg = new MimeMessage(mailConnection);
//设置发送人和接收人
Address sender = new InternetAddress("................."); //发件人邮箱
//群发邮件
StringBuffer buffer = new StringBuffer();
buffer.append("...............,");
buffer.append("...............,");
buffer.append("。。。。。。。。。。。,");
buffer.append("。。。。。。。。。。");
Address[] alld = InternetAddress.parse(buffer.toString());
msg.setFrom(sender);//设置邮件来源, 用发送人sender初始化
//注意下面的方法变成了 setRecipients 不再是单人发送时用的 setRecipient
msg.setRecipients(Message.RecipientType.TO, alld); //设置邮件发送去向, 用收件人receiver初始化
msg.setSubject("这个是一个测试邮件"); //设置邮件主题
//新建一个MimeMultpart对象来存放多个BodyPart对象
Multipart mtp = new MimeMultipart();
//BodyPart对象可以存储文字信息,也可以存储文件信息
BodyPart mdp_info = new MimeBodyPart();
String sss = "晚安!!";//设置邮件正文
mdp_info.setContent(sss, "text/html;charset=gb2312");
mtp.addBodyPart(mdp_info);
//将含有文字内容的BodyPart对象加入到MimeMultiPart对象中
BodyPart mdp_file = new MimeBodyPart();
FileDataSource fds = new FileDataSource("......路径。。\\12.jpg");
DataHandler dh = new DataHandler(fds);
mdp_file.setFileName("12.jpg");
mdp_file.setDataHandler(dh);
mtp.addBodyPart(mdp_file);
//将含有文件(附件)内容的BodyPart对象加入到MimeMultiPart对象中
msg.setContent(mtp);
//把mtp作为消息对象的内容
//先进行邮件存储
msg.saveChanges();
Transport trans = mailConnection.getTransport("smtp");
String username = "。。。。。。。。。。"; //发信人地址
String pw = "。。。。。。。。。。。"; //发件地址对应的密码
//邮件服务器名, 用户名, 密码
trans.connect("smtp.163.com", username, pw);
trans.sendMessage(msg, msg.getAllRecipients()); //发送的信息
trans.close();
System.out.println("发信成功!!");
}catch(Exception e)
{
System.err.println(e);
}
finally{
System.exit(0);
}
}
}