javaMail邮件发送

1、下载javaMail所需要的jar包

地址如下:

http://www.oracle.com/technetwork/java/javamail/index-138643.html

2、编写工具类



import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class JavaMail {


private static Properties props = new Properties();


private static Session session = Session.getInstance(props);


public static void setProps(String host){

// Props中配置的参数同发送邮件有关(仅创建邮件时可以不设置)
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", host);
props.setProperty("mail.smtp.auth", "true");// 请求认证
}



public static MimeMessage createMessage(String subject,String sendMailAccount,String recMailAccount) {
// 创建MimeMessage对象:代表一封邮件
MimeMessage message = new MimeMessage(session);
// 设置邮件头
try {
message.setFrom(new InternetAddress(sendMailAccount));
message.setRecipients(Message.RecipientType.TO, recMailAccount);
message.setSubject(subject);
return message;
} catch (Exception e) {
throw new RuntimeException(e);
}
}


public static void sendMail(Message message,String SENDMAIL_ACCOUNT,String SENDMAIL_PASSWD){
       try {
           Transport ts = session.getTransport();
           ts.connect(SENDMAIL_ACCOUNT,SENDMAIL_PASSWD);
           ts.sendMessage(message, message.getAllRecipients());
       } catch (Exception e) {
           throw new RuntimeException(e);
       }
   }


public static String readValue(String filePath,String key) {
 Properties props = new Properties();
       try {
        InputStream in = new BufferedInputStream (new FileInputStream(filePath));
        props.load(in);
        String value = props.getProperty (key);
           System.out.println(key+value);
           return value;
       } catch (Exception e) {
        e.printStackTrace();
        return null;
       }
}


/**
 * 调用方式  文本发送
 *
public static void main(String[] args) throws Exception {
    //创建message对象(已设置好邮件头)
    MimeMessage message = MailUtil.createMessage("a mail just contains text.");
    //设置正文(邮件体)
    message.setContent("<h1>just contains text</h1>", "text/html");


    //保存并生成最终的邮件内容
    message.saveChanges();
    //发送邮件
    MailUtil.sendMail(message);
    //顺便将邮件写到本地磁盘(只是看看效果,非必须)
    message.writeTo(new FileOutputStream("C:/Users/flyne/Desktop/1.eml"));
}
 */


/***
 * 文本中内嵌图片和附件的邮件
 *
 * public static void main(String[] args) throws Exception{
    MimeMessage message = MailUtil.createMessage("a mail contains text&image&file");


    //设置正文①:文本部分
    MimeBodyPart textPart = new MimeBodyPart();
    textPart.setContent("美女图片 <img src='cid:mm'/> text", "text/html;charset=UTF-8");


    //设置正文②:图片部分
    MimeBodyPart imagePart = new MimeBodyPart();
    imagePart.setContentID("mm");
    DataHandler dh = new DataHandler(new FileDataSource("src/0.jpg"));
    imagePart.setDataHandler(dh);


    //设置正文③:关联文本和图片
    MimeMultipart mp = new MimeMultipart();
    mp.addBodyPart(textPart);
    mp.addBodyPart(imagePart);
    mp.setSubType("related");//数据之间的关系:multipart/related


    //设置正文④:封装到BodyPart
    MimeBodyPart textImagePart = new MimeBodyPart();
    textImagePart.setContent(mp);


    //设置正文⑤:附件部分
    MimeBodyPart attachmentPart = new MimeBodyPart();
    dh = new DataHandler(new FileDataSource("src/Account.zip"));
    attachmentPart.setDataHandler(dh);
    attachmentPart.setFileName(MimeUtility.encodeText("账户表.zip"));//设置附件名


    //设置正文⑥:创建最终的邮件版本
    MimeMultipart finalMp = new MimeMultipart();
    finalMp.addBodyPart(textImagePart);
    finalMp.addBodyPart(attachmentPart);
    finalMp.setSubType("mixed");


    //设置正文⑦
    message.setContent(finalMp);


    message.saveChanges();
    //发送邮件
    MailUtil.sendMail(message);
    message.writeTo(new FileOutputStream("C:/Users/flyne/Desktop/3.eml"));
}
 */




}


/***

调用的具体方法

*/

// 设置正文⑦
try {


// 创建message对象(已设置好邮件头)
//JavaMail.setHost(mail_account.getSEND_ADDRESS());
JavaMail.setProps(mail_account.getSEND_ADDRESS());
MimeMessage message = JavaMail.createMessage("XXXXXXXXXX", mail_account.getSENDMAIL_ACCOUNT(),
mail_account.getREC_ACCOUNT());
// 设置正文⑤:附件部分
MimeMultipart mp = new MimeMultipart("alternative");
Enumeration efile = vector.elements();
String filename = "";
while (efile.hasMoreElements()) {


MimeBodyPart mbpFile = new MimeBodyPart();
filename = efile.nextElement().toString();
FileDataSource fds = new FileDataSource(filename);
mbpFile.setDataHandler(new DataHandler(fds));
filename = new String(fds.getName().getBytes(), "gbk");
mbpFile.setFileName(MimeUtility.encodeText(filename, "gbk", "B"));
// 向MimeMessage添加(Multipart代表附件)
mp.setSubType("mixed");
mp.addBodyPart(mbpFile);

}

message.setContent(mp);
message.setSentDate(new Date());
message.saveChanges();
// 发送邮件
JavaMail.sendMail(message, mail_account.getSENDMAIL_ACCOUNT(), mail_account.getSENDMAIL_PASSWD());
} catch (MessagingException e1) {
e1.printStackTrace();
}




你可能感兴趣的:(javaMail邮件发送)