要用到的jar包:mail.jar,activation.jar(jdk6不需要用此jar包),commons-email.jar。
1、发送时验证
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;
public class SimpleMail{
public static void main(String[] args){
Properties properties=new Properties();
properties.setProperty("mail.smtp.auth","true");
properties.setProperty("mail.transport.protocol","smtp");
Session session=Session.getInstance(properties);
//设为debug模式可在控制台看到发送邮件的全过程
session.setDebug(true);
Message message=new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setSubject("simple java mail");
message.setText("hello,superzhang!this is a simple java mail");
Transport transport=session.getTransport();
//发送邮件时进行验证
transport.connect("smtp.sina.com", 25, "superzangcao","super");
transport.sendMessage(message,InternetAddress.parse("[email protected],[email protected],[email protected]"));
}
}
2、连接时验证
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class StaticTransport {
public static void main(String[] args) throws Exception{
Properties properties=new Properties();
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.host", "smtp.sina.com");
//获取session实例时进行验证
//Authenticator是抽象类,此处有一个匿名类,该匿名类继承抽象类Authenticator,new的是匿名类,当然,你也可以单独写一个类继承Authenticator,重写它的getPasswordAuthentication()方法
Session session=Session.getInstance(properties,new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("superzangcao","super");
}
});
session.setDebug(true);
Message message=new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(RecipientType.TO,InternetAddress.parse("[email protected],[email protected],[email protected],[email protected]"));
message.setSubject("static transport");
message.setContent("this email is transported by <font color='red'>static</font> transport","text/html;charset=utf-8;");
Transport.send(message);
}
}
3、带附件的邮件
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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 javax.mail.internet.MimeUtility;
public class MultipartMail {
public static void main(String[] args)throws Exception{
Properties properties=new Properties();
properties.put("mail.smtp.auth", "auth");
properties.put("mail.transport.protocol", "smtp");
Session session=Session.getInstance(properties);
session.setDebug(true);
Message message=new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setSubject("multipart mail");
Multipart multipart=new MimeMultipart("mixed");
message.setContent(multipart,"text/html;charset=utf-8;");
//邮件正文body
MimeBodyPart body=new MimeBodyPart();
Multipart bodyMultipart=new MimeMultipart("related");
BodyPart bodyPart=new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(new FileDataSource("D:/mail.bmp")));
bodyMultipart.addBodyPart(bodyPart);
body.setContent(bodyMultipart);
body.setContent("", "text/html;charset=utf-8");
//邮件附件attachment
MimeBodyPart attachment=new MimeBodyPart();
attachment.setDataHandler(new DataHandler(new FileDataSource("D:/ax_files.xml")));
attachment.setFileName(MimeUtility.encodeText("中文")+"xml");
multipart.addBodyPart(body);
multipart.addBodyPart(attachment);
Transport transport=session.getTransport();
transport.connect("smtp.sina.com",25, "superzangcao","super");
transport.sendMessage(message,InternetAddress.parse("[email protected],[email protected]"));
}
}
4、用commons-email发送简单邮件
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import org.apache.commons.mail.SimpleEmail;
public class SimpleMail {
public static void main(String[] args) throws Exception{
SimpleEmail email=new SimpleEmail();
email.setHostName("smtp.sina.com");
email.setAuthenticator(new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("superzangcao","super");
}
});
email.setDebug(true);
email.setCharset("UTF-8");
email.setFrom("[email protected]");
email.addTo("[email protected]");
email.setSubject("你好!");
email.setMsg("hello,superzhang!很高兴认识你。");
email.send();
}
}
5、用commons-email发送带附件的邮件
import javax.mail.BodyPart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
public class MultiMail {
public static void main(String[] args) throws Exception {
MultiPartEmail email = new MultiPartEmail();
email.setHostName("smtp.sina.com");
email.setAuthentication("superzangcao", "super");
email.setCharset("UTF-8");
email.setDebug(true);
email.setFrom("[email protected]");
email.addTo("[email protected]");
email.setSubject("带附件的邮件");
MimeMultipart body=new MimeMultipart();
BodyPart bodyPart=new MimeBodyPart();
bodyPart.setContent("<font color=red>你好,中文邮件也没问题</font>", "text/html;charset=utf-8");
body.addBodyPart(bodyPart);
email.addPart(body);
EmailAttachment attachment = new EmailAttachment();
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setName(MimeUtility.encodeText("附件"));
attachment.setPath("D:\\Java邮件开发驱动包.rar");
email.attach(attachment);
email.send();
}
}