java 发送邮件
下面的代码将展示如何用java来发送一封带附件的邮件,sina新浪邮箱发给QQ邮箱。
email项目(java priject)目录结构如下:
项目中使用的jar包可自行在网上下载,下载后加入到classpath就可以了。邮箱的授权码可以在电脑端邮箱的【帮助】中获取。
package www.egges.email;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
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 SendMail {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String sendFrom = "xxx@sina.com"; //发件人
String userName = "xxx@sina.com"; //发件人用户名
String password = "xxxxxx"; //发件人邮箱密码:使用邮箱的授权码
String sendTo = "xxx@qq.com"; //收件人
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true"); //认证设置:需要认证
props.setProperty("mail.transport.protocol", "smtp"); //传输协议:smtp
props.setProperty("mail.host", "smtp.sina.com"); //设置端口
Session session = Session.getInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
session.setDebug(true);
/*MimeMessage:邮件(MimeMessage),是一个mixed关系的mime复杂体。
*----------|mailAttachment:附件(MimeBodyPart)
*----------|mailContent:内容(MimeMultipart),是一个related关系的mime复杂体。
*---------------------|mailContentHtmlMimeBodyPart:超文本(MimeBodyPart)
*---------------------|mailContentImgMimeBodyPart:连接图片(MimeBodyPart)
*/
MimeMessage mimeMessage = new MimeMessage(session); //创建MIME样式消息
mimeMessage.setFrom(new InternetAddress(sendFrom)); //设置发件人
mimeMessage.setSubject("MIME格式邮件");
mimeMessage.setRecipient(RecipientType.TO, new InternetAddress(sendTo));
/*
* 备注:mixed:有附件的复杂体;related:有内嵌资源的复杂体;alternative:只有文本或超文本的复杂体
*/
MimeMultipart mailMimeMultipart = new MimeMultipart("mixed"); //新建"mixed"组合关系和复杂体
mimeMessage.setContent(mailMimeMultipart);
MimeBodyPart mailAttachment = new MimeBodyPart(); //新建邮件附件的BodyPart对象
MimeBodyPart mailContent = new MimeBodyPart(); //新建邮件内容的BodyPart对象
mailMimeMultipart.addBodyPart(mailAttachment); //将邮件附件的BodyPart对象添加到复杂体上
mailMimeMultipart.addBodyPart(mailContent); //将邮件内容的BodyPart对象添加到复杂体上
DataSource ds = new FileDataSource("F:\\jarPackage\\mail.jar");
DataHandler dh = new DataHandler(ds );
mailAttachment.setDataHandler(dh); //添加附件
mailAttachment.setFileName("mail.jar"); //设置附件名称
//新建related关系的邮件内容的复杂体对象
MimeMultipart mailContentMimeMultipart = new MimeMultipart("related");
mailContent.setContent(mailContentMimeMultipart);
//新建邮件内容的复杂体对象的BodyPart
MimeBodyPart mailContentHtmlMimeBodyPart = new MimeBodyPart();
MimeBodyPart mailContentImgMimeBodyPart = new MimeBodyPart();
//将BodyPart添加到MimeMultipart上
mailContentMimeMultipart.addBodyPart(mailContentHtmlMimeBodyPart);
mailContentMimeMultipart.addBodyPart(mailContentImgMimeBodyPart);
mailContentHtmlMimeBodyPart.setContent(
"<span style='color: red;background-color:blue;'>MIME格式符复杂体邮件邮件<span>"
+ "<img src='D:\\xxx\\xxx.xxx'/>", "text/html;charset=GBK");
DataSource mailContentImgMimeBodyPartDhDs = new FileDataSource("D:\\xxx\\coin\\mail.png");
DataHandler mailContentImgMimeBodyPartDh = new DataHandler(mailContentImgMimeBodyPartDhDs );
mailContentImgMimeBodyPart.setDataHandler(mailContentImgMimeBodyPartDh );
mailContentImgMimeBodyPart.setHeader("Content-location", "D:\\xxx\\coin\\mail.png");
mimeMessage.saveChanges();
OutputStream os = new FileOutputStream("D:\\Myeclipes\\email\\temp\\mail.msg");
mimeMessage.writeTo(os);
os.close();
Transport.send(mimeMessage);
}
}
在使用eclipse开发时,在新建activation.jar中的DataHanler对象时,会导致eclipse报错,不知道怎么解决,只好使用myeclipse,郁闷!!!
注意:尽量不要在邮件中出现测试等字眼。不要频繁发送,频繁发送会导致QQ后台将你使用的新浪邮箱标记为垃圾邮箱,会拒收你发送的邮件。