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);

    }
    }

  • 相关阅读:
    Azure PowerShell (7) 使用CSV文件批量设置Virtual Machine Endpoint
    Windows Azure Cloud Service (39) 如何将现有Web应用迁移到Azure PaaS平台
    Azure China (7) 使用WebMetrix将Web Site发布至Azure China
    Microsoft Azure News(4) Azure新D系列虚拟机上线
    Windows Azure Cloud Service (38) 微软IaaS与PaaS比较
    Windows Azure Cloud Service (37) 浅谈Cloud Service
    Azure PowerShell (6) 设置单个Virtual Machine Endpoint
    Azure PowerShell (5) 使用Azure PowerShell创建简单的Azure虚拟机和Linux虚拟机
    功能代码(1)---通过Jquery来处理复选框
    案例1.用Ajax实现用户名的校验
  • 原文地址:https://www.cnblogs.com/csk-1/p/4996617.html
Copyright © 2011-2022 走看看