关闭

Android发送邮件mark

138人阅读 评论(0) 收藏 举报
分类:
public class MailSender {

    private static final String LOG_TAG = MailSender.class.getSimpleName();
    private static final int PORT = 25;
    /**
     * 以文本格式发送邮件
     * @param sender 发件人邮箱
     * @param encryptPassword 邮箱密码
     * @param smtp 服务器地址
     * @param subject 主题、标题
     * @param content 内容
     * @param file 附件路径
     * @param maillists 收件人邮箱列表
     */
    public static boolean sendTextMail(String sender, String encryptPassword, String smtp, String subject, String content, String file,
            String[] maillists) {
        if (maillists == null || maillists.length == 0 || ("".equals(maillists[0].trim()))) {
            return false;
        } else {
            // Get system properties
            Properties props = new Properties();

            // Setup mail server
            props.put("mail.smtp.host", smtp);
            props.put("mail.smtp.port", PORT);
            // Get session
            props.put("mail.smtp.auth", "true"); // 如果需要密码验证,把这里的false改成true

            // 判断是否需要身份认证
            CustomizedAuthenticator authenticator = null;
            if (true) {
                // 如果需要身份认证,则创建一个密码验证器
                authenticator = new CustomizedAuthenticator(sender, encryptPassword);
            }
            // 根据邮件会话属性和密码验证器构造一个发送邮件的session
            Session sendMailSession = Session.getInstance(props, authenticator);
            try {
                // 根据session创建一个邮件消息
                Message mailMessage = new MimeMessage(sendMailSession);
                // 创建邮件发送者地址
                Address from = new InternetAddress(sender);
                // 设置邮件消息的发送者
                mailMessage.setFrom(from);
                // 创建邮件的接收者地址,并设置到邮件消息中
                for (int i = 0; i < maillists.length; i++) {
                    // Message.RecipientType.TO属性表示接收者的类型为TO
                    mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(maillists[i]));
                }

                // 设置邮件消息的主题
                mailMessage.setSubject(subject);
                // 设置邮件消息发送的时间
                mailMessage.setSentDate(new Date());

                Multipart multipart = new MimeMultipart();
                BodyPart bodyPart = new MimeBodyPart();
                bodyPart.setText(content);
                multipart.addBodyPart(bodyPart);

                File attach = new File(file);
                if (attach.exists()) {
                    MimeBodyPart attachPart = new MimeBodyPart();
                    DataSource source = new FileDataSource(attach);
                    attachPart.setDataHandler(new DataHandler(source));
                    attachPart.setFileName(attach.getName());

                    multipart.addBodyPart(attachPart);
                }
                mailMessage.setContent(multipart);
                MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
                mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
                mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
                mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
                mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
                mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
                CommandMap.setDefaultCommandMap(mc);
                Transport.send(mailMessage);
                return true;
            } catch (MessagingException ex) {
                ex.printStackTrace();
            }
        }
        return false;
    }
}

class CustomizedAuthenticator extends Authenticator {
    String userName = null;
    String password = null;

    public CustomizedAuthenticator() {
    }

    public CustomizedAuthenticator(String username, String password) {
        this.userName = username;
        this.password = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userName, password);
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2051938次
    • 积分:10748
    • 等级:
    • 排名:第1497名
    • 原创:125篇
    • 转载:30篇
    • 译文:1篇
    • 评论:52条
    联系方式

    QQ群:194067225

    微博:brian512

    邮箱:huamm512@163.com

    博客专栏