import java.io.File;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
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.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
public class demo2 {
public static void main(String[] args) throws Exception {
//创建一个登陆连接服务器的文件
Properties props = new Properties();
props.setProperty("mail.host", "smtp.sina.com");
props.setProperty("mail.smtp.auth", "true");
//登陆的用户名和密码
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication("eric@163.com","123456");
}
};
Session session = Session.getDefaultInstance(props , authenticator );
MimeMessage mail = new MimeMessage(session);
mail.setFrom(new InternetAddress("ericxu12345@sina.com"));
mail.setRecipient(RecipientType.TO, new InternetAddress("jackyxu@163.com"));
mail.setSubject("这是一个主题");
MimeBodyPart body1 = new MimeBodyPart();
body1.attachFile(new File("c:/images.jpg"));
MimeBodyPart body2 = new MimeBodyPart();
body2.attachFile(new File("c:/images.jpg"));
MimeBodyPart body3 = new MimeBodyPart();
body3.setContent("youyisi", "text/html;charset=utf-8");
MimeMultipart mmp = new MimeMultipart();
mmp.addBodyPart(body3, 0);
mmp.addBodyPart(body2, 1);
mmp.addBodyPart(body1, 2);
mail.setContent(mmp);
Transport.send(mail);
}
}
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class demo1 {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.setProperty("mail.host", "smtp.sina.com"); //登陆的邮箱的主机
props.setProperty("mail.smtp.auth", "true"); //是否验证加密
//创建一个Authenticator的子类对象,对用户名和密码进行加密
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("eric@163.com", "123456");
}
};
//创建连接并登陆服务器,
//参数一为properties文件表示登陆的主机地址和登陆配置,参数二为指定用户名和密码(需要验证登录的(加密的))
Session session = Session.getDefaultInstance(props, authenticator);
session.setDebug(true);
//创建邮件
MimeMessage mail = new MimeMessage(session);
//2.1 指定发件人(和登录用户一致)
mail.setFrom(new InternetAddress("ericxu12345@sina.com"));
//2.2 指定收件人
//参数一:收件类型 TO:收件人 CC:抄送人 BCC:密送人
//例如: A -> B(TO) -> C(CC) -> D(BCC)
mail.setRecipient(RecipientType.TO, new InternetAddress("jackyxu12345@sina.com"));
//2.3 主题
mail.setSubject("这是一封javamail发送的测试邮件");
//2.4 内容
mail.setContent("测试邮件内容,七夕快乐!", "text/plain;charset=utf-8");
//3)发送邮件
Transport.send(mail);
}
}
JavaMail邮件发送功能
<span style="white-space:pre"> </span>通过使用SMTP协议进行邮件的发送
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>1)创建并登陆服务器,并对用户名和密码进行登陆加密
<span style="white-space:pre"> </span>2)创建一份邮件,包括发件人、收件人、主题、内容
<span style="white-space:pre"> </span>3)发送邮件
其实mail邮件这个功能也是在特定的需求中才会被使用到