首先导入附件中的mail.jar和activation.jar
具体发邮件程序如下:
package mail;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
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 SendMailTest {
public static void main(String[] args) throws AddressException, MessagingException, IOException {
//method1();// 发送不带附件的邮件
method2();// 发送带附件的邮件
}
/**
* 发送邮件(不带附件)
* @throws MessagingException
* @throws AddressException
*/
public static void method1() throws AddressException, MessagingException{
// 1,得到session
Properties pro = new Properties();
pro.setProperty("mail.host", "smtp.163.com");
pro.setProperty("mail.smtp.auth", "true");
Authenticator auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 此处设置发件人的用户名密码
return new PasswordAuthentication("w6037", "w687!");
}
};
Session session = Session.getInstance(pro, auth);
// 2,创建MimeMessage
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("[email protected]"));// 设置发件人
msg.setRecipients(RecipientType.TO, "[email protected]");// 设置收件人
msg.setRecipients(RecipientType.CC, "[email protected]");// 设置抄送人
msg.setRecipients(RecipientType.BCC, "[email protected]");// 设置密送人
msg.setSubject("来自本天才的邮件");// 设置邮件标题
msg.setContent("这是一封测试邮件,请查收", "text/html;charset=UTF-8");// 设置邮件内容
// 发送邮件
Transport.send(msg);
}
/**
* 发送邮件(带附件)
* @throws AddressException
* @throws MessagingException
* @throws IOException
*/
public static void method2() throws AddressException, MessagingException, IOException {
// 1,得到session
Properties pro = new Properties();
pro.setProperty("mail.host", "smtp.163.com");
pro.setProperty("mail.smtp.auth", "true");
Authenticator auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 设置发件人用户名密码
return new PasswordAuthentication("w6037", "w688!");
}
};
Session session = Session.getInstance(pro, auth);
// 2,创建MimeMessage
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("[email protected]"));// 设置发件人
msg.setRecipients(RecipientType.TO, "[email protected]");// 设置收件人
msg.setRecipients(RecipientType.CC, "[email protected]");// 设置抄送人
msg.setRecipients(RecipientType.BCC, "[email protected]");// 设置密送人
msg.setSubject("来自本天才的邮件");// 设置邮件标题
MimeMultipart list=new MimeMultipart();
MimeBodyPart content=new MimeBodyPart();
content.setContent("这是一封带附件的测试邮件","text/html;charset=UTF-8");//设置邮件内容
MimeBodyPart file=new MimeBodyPart();
file.attachFile("D:\\1.txt");//设置要上传附件
file.setFileName(MimeUtility.encodeText("欧巴刚那斯达.txt"));//在邮件中显示的附件名称,MimeUtility.encodeText用来处理中文乱码
list.addBodyPart(content);
list.addBodyPart(file);
msg.setContent(list);
// 发送邮件
Transport.send(msg);
}
}