zoukankan      html  css  js  c++  java
  • java发送邮件

    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.Properties;

    import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
    import javax.mail.Address;
    import javax.mail.BodyPart;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    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;

    import org.apache.log4j.Logger;

    public class MailSend {
    /**
    * 以文本格式发送邮件
    *
    * @param mailInfo
    * 待发送的邮件的信息
    */
    private static Logger logger = Logger.getLogger(MailSend.class);

    public boolean sendTextMail(MailUserInfo mailInfo) {
    MailAuth authenticator = null;
    Properties pro = mailInfo.getProperties();
    if (mailInfo.isValidate()) {
    authenticator = new MailAuth(mailInfo.getUserName(),
    mailInfo.getPassword());
    }
    Session sendMailSession = Session
    .getDefaultInstance(pro, authenticator);
    try {
    Message mailMessage = new MimeMessage(sendMailSession);
    Address from = new InternetAddress(mailInfo.getFromAddress());
    mailMessage.setFrom(from);
    Address[] to = mailInfo.getToAddress();
    mailMessage.addRecipients(Message.RecipientType.TO, to);
    mailMessage.setSubject(mailInfo.getSubject());
    mailMessage.setSentDate(new Date());
    String mailContent = mailInfo.getContent();
    mailMessage.setText(mailContent);
    Transport.send(mailMessage);
    return true;
    } catch (MessagingException ex) {
    logger.error(ex.getMessage(), ex);
    }
    return false;
    }

    /**
    * 以HTML格式发送邮件
    *
    * @param mailInfo
    * 待发送的邮件信息
    */
    @SuppressWarnings("rawtypes")
    public boolean sendHtmlMail(MailUserInfo mailInfo) {
    MailAuth authenticator = null;
    Properties pro = mailInfo.getProperties();
    if (mailInfo.isValidate()) {
    authenticator = new MailAuth(mailInfo.getUserName(),
    mailInfo.getPassword());
    }
    Session sendMailSession = Session
    .getDefaultInstance(pro, authenticator);
    try {
    Message mailMessage = new MimeMessage(sendMailSession);
    Address from = new InternetAddress(mailInfo.getFromAddress());
    mailMessage.setFrom(from);
    Address[] to = mailInfo.getToAddress();
    mailMessage.setRecipients(Message.RecipientType.TO, to);
    mailMessage.setSubject(mailInfo.getSubject());
    mailMessage.setSentDate(new Date());
    Multipart mainPart = new MimeMultipart();
    BodyPart html = new MimeBodyPart();
    html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
    mainPart.addBodyPart(html);
    mailMessage.setContent(mainPart);
    ArrayList files = new ArrayList();
    files = mailInfo.getFilesName();
    for (int i = 0; i < files.size(); i++) {
    html = new MimeBodyPart();
    String filename = (String) files.get(i);
    FileDataSource fds = new FileDataSource(filename);
    html.setDataHandler(new DataHandler(fds)); // 得到附件本身并至入BodyPart
    html.setFileName(fds.getName()); // 得到文件名同样至入BodyPart
    mainPart.addBodyPart(html);
    }
    // 发送邮件
    Transport.send(mailMessage);
    return true;
    } catch (MessagingException ex) {
    logger.error(ex.getMessage(), ex);
    }
    return false;
    }

    public static void send(String content, String subject, Object files,
    List<String> toAddress) {
    // 这个类主要是设置邮件
    MailUserInfo mailInfo = new MailUserInfo();

    mailInfo.setMailServerHost((String) PropertyUtil
    .getProperty("mail_host"));
    mailInfo.setMailServerPort((String) PropertyUtil
    .getProperty("mail_port"));
    mailInfo.setValidate(Boolean.getBoolean((String) PropertyUtil
    .getProperty("mail_validate")));
    mailInfo.setUserName((String) PropertyUtil.getProperty("mail_username"));
    mailInfo.setPassword((String) PropertyUtil.getProperty("mail_password"));
    mailInfo.setFromAddress((String) PropertyUtil.getProperty("mail_from"));
    if (files != null) {
    mailInfo.setFilesName(files);
    }

    mailInfo.setToAddress(toAddress);
    mailInfo.setSubject(subject);
    mailInfo.setContent(content);
    MailSend sms = new MailSend();
    sms.sendHtmlMail(mailInfo);

    }
    }

  • 相关阅读:
    学习JNA,Jnative
    JNative用法注意事项
    使用JNA替代JNI调用本地方法
    傅盛读书笔记:下一个Moonshot是什么?
    华为内部狂转好文:有关大数据,看这一篇就够了
    ws2_32.dll的妙用与删除 (禁网)
    保护颈椎重点按这三大穴位(图)
    在java中调用python方法
    在Windows中实现Java调用DLL(转载)
    java程序员,英语那点事
  • 原文地址:https://www.cnblogs.com/csk-1/p/4996617.html
Copyright © 2011-2022 走看看