package com.email.send;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
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;
public class EmailSender {
String protocol ="smtp"; //服务器
String from="*******@qq.com";//寄信人,这里可以改为你的邮箱地址
String to="*******@qq.com";//收信人,改为你要寄给那人的邮箱地址 建议可以用自己的QQ邮箱试试
String subject="Java发送邮件测试!";//邮件主题
String body="qq.com" +
"";//邮件内容 这是一个HTTP网页类的邮件内容
public static void main(String []args)throws Exception{
String server ="smtp.qq.com";//QQ邮箱的服务器,例如新浪邮箱或者其他服务器可以自己去查,这里用QQ邮箱为例
String user="******@qq.com";//登录邮箱的用户,不如说你用QQ邮箱,那就写你登录QQ邮箱的帐号
String pass="*******";//登录密码
EmailSender sender=new EmailSender();
Session session= sender.createSession();
MimeMessage message=sender.createMessage(session);
System.out.println("正在发送邮件...");
Transport transport=session.getTransport();
transport.connect(server,user,pass);
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
transport.close();
System.out.println("发送成功!!!");
}
public Session createSession(){
Properties props=new Properties();
props.setProperty("mail.transport.protocol", protocol);
props.setProperty("mail.smtp.auth","true");
Session session=Session.getInstance(props);
// session.setDebug(true);
return session;
}
public MimeMessage createMessage(Session session)throws Exception{
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(subject);
MimeMultipart multipart=new MimeMultipart("re
1