邮件发送
package com.bao.test;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Test {
public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {
Properties props=new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.qq.com");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback","false");
props.setProperty("mail.smtp.socketFactory.port", "465");
Session session=Session.getInstance(props);
MimeMessage message=createMimeMessage(session, "发件人邮箱", "", "", "");
Transport transport = session.getTransport();
transport.connect("发件人邮箱","识别码");
transport.sendMessage(message,message.getAllRecipients());
}
public static MimeMessage createMimeMessage(Session session,String send,String receive,String cReceive,String mReceive) throws MessagingException, UnsupportedEncodingException {
MimeMessage message=new MimeMessage(session);
Address address=new InternetAddress(send,"发件人名字","utf-8");
message.setFrom(address);
message.setSubject("这是一个标题","utf-8");
message.setContent("正文内容、、、、、hello","text/html,charset=utf-8");
message.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(receive,"收件人A","utf-8"));
message.setRecipient(MimeMessage.RecipientType.CC,new InternetAddress(cReceive,"收件人B","utf-8"));
message.setRecipient(MimeMessage.RecipientType.BCC,new InternetAddress(mReceive,"收件人C","utf-8"));
message.setSentDate(new Date());
message.saveChanges();
return message;
}
}
带附件的邮件发送
package com.bao.test;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
public class Test {
public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {
Properties props=new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.qq.com");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback","false");
props.setProperty("mail.smtp.socketFactory.port", "465");
Session session=Session.getInstance(props);
MimeMessage message=createMimeMessage(session, "发件人", "", "", "");
Transport transport = session.getTransport();
transport.connect("发件人","检验码");
transport.sendMessage(message,message.getAllRecipients());
}
public static MimeMessage createMimeMessage(Session session,String send,String receive,String cReceive,String mReceive) throws MessagingException, UnsupportedEncodingException {
MimeMessage message=new MimeMessage(session);
Address address=new InternetAddress(send,"发件人名字","utf-8");
message.setFrom(address);
message.setSubject("这是一个标题","utf-8");
MimeBodyPart image=new MimeBodyPart();
DataHandler imdatahandler=new DataHandler(new FileDataSource("src/a.jpg"));
image.setDataHandler(imdatahandler);
image.setContentID("tupian");
MimeBodyPart text=new MimeBodyPart();
text.setContent("正文内容、、、、、hello,img:<img src='cid:tupian' />","text/html;charset=utf-8");
MimeMultipart mm_text_image=new MimeMultipart();
mm_text_image.addBodyPart(image);
mm_text_image.addBodyPart(text);
mm_text_image.setSubType("related");
MimeBodyPart t_m_bodypart=new MimeBodyPart();
t_m_bodypart.setContent(mm_text_image);
MimeBodyPart attachment=new MimeBodyPart();
DataHandler fileDataHandler=new DataHandler(new FileDataSource("src/a.png"));
attachment.setDataHandler(fileDataHandler);
attachment.setFileName(MimeUtility.encodeText(fileDataHandler.getName()));
MimeMultipart mm=new MimeMultipart();
mm.addBodyPart(t_m_bodypart);
mm.addBodyPart(attachment);
mm.setSubType("mixed");
message.setContent(mm,"text/html,charset=utf-8");
message.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(receive,"收件人A","utf-8"));
message.setRecipient(MimeMessage.RecipientType.CC,new InternetAddress(cReceive,"收件人B","utf-8"));
message.setRecipient(MimeMessage.RecipientType.BCC,new InternetAddress(mReceive,"收件人C","utf-8"));
message.setSentDate(new Date());
message.saveChanges();
return message;
}
}