邮件发送:存在问题,不知道什么原因,使用itcastjar包发送可以成功,自己写的两个函数发送失败,提示邮件内容错误,退信。mmp遗留的问题待解决。
package com.ayit.mail;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
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;
import org.junit.Test;
import cn.itcast.mail.Mail;
import cn.itcast.mail.MailUtils;
public class SendMail {
@Test
public void send() throws Exception{
//表示会话,即客户端与邮件服务器之间的会话!想获得会话需要给出账户和密码,当然还要给出服务器名称。
//在邮件服务中的Session对象,就相当于连接数据库时的Connection对象。
Properties p = new Properties();
p.setProperty("mail.host", "smtp.163.com");
p.setProperty("mail.smtp.auth", "true");
//获取session对象
Session session = Session.getInstance(p,new Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("ay_xyj","xxxxxx");//授权码
}
});
//示邮件类,它是Message的子类。它包含邮件的主题(标题)、内容,收件人地址、发件人地址,还可以
//设置抄送和暗送,甚至还可以设置附件。
MimeMessage msg = new MimeMessage(session);
//设置发件人
msg.setFrom(new InternetAddress("ay_xyj@163.com"));
//设置收件人
msg.addRecipients(RecipientType.TO, "1525467920@qq.com");
msg.addRecipients(RecipientType.CC, "aysyl@sohu.com,17630531853@163.com");//设置多个抄送
msg.addRecipients(RecipientType.BCC, "87089043@qq.com");//设置暗送
msg.setSubject("123213");//设置主题(标题)
msg.setContent("157289839838", "text/html;charset=utf-8");//设置正文
//Transport:用来发送邮件。它是发送器!
Transport.send(msg);
}
@Test
public void send1() throws Exception{
Properties p = new Properties();
//设置服务器主机
p.setProperty("mail.host", "smtp.163.com");
//设置是否需要认证
p.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(p, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("ay_xyj","xxxxxx");
}
});
//获取MimeMessage
MimeMessage msg = new MimeMessage(session);
//发件人
msg.setFrom(new InternetAddress("ay_xyj@163.com"));
//收件人
msg.addRecipients(RecipientType.TO, "87089043@qq.com");
//---------------内容加附件---------------------
//获取MimeMultipart
MimeMultipart list = new MimeMultipart();
MimeBodyPart part = new MimeBodyPart();
part.setContent("hello","text/html;charset=utf-8");
list.addBodyPart(part);
// MimeBodyPart part2 = new MimeBodyPart();
// part2.attachFile(new File("D:\\mail.txt"));
// part2.setFileName("syl.txt");
// list.addBodyPart(part2);
//-------------------------------------------
msg.setContent(list);
//获取Transport
Transport.send(msg);
}
@Test
public void send2() throws MessagingException, IOException{
//得到session
Session session = MailUtils.createSession("smtp.163.com", "17630531853", "xxxxxx");
//创建邮件对象
Mail mail = new Mail("17630531853@163.com","1779733833@qq.com,ay_xyj@163.com","你好,jack","Angelbaby");
//发送
MailUtils.send(session, mail);
}
}
有遇到和我一样的问题的解决的,希望能留言提醒一下,感谢。(xxxxxx是网易邮箱授权码,并不是登录密码)