java mail 发送邮件,支…

本文介绍了一种使用Java实现邮件发送的方法,包括设置SMTP服务器、邮件认证、邮件内容及附件处理等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天对Java发送邮件的稍微搜索整理了下,记下来沉淀一下。


package com.datadriver.fosun.sendemail.action;

 

import java.io.UnsupportedEncodingException;

import java.util.List;

import java.util.Properties;

 

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.mail.*;

import javax.mail.internet.*;

 

import com.datadriver.common.util.log.Logger;

 

public class SendMailUtil {

 

    public static String send(String host, String mail, String password,

            String from, List to, String title, String content,

            String[] fileNames) {

        String result = "邮件发送成功";

        // Get system properties

        Properties props = System.getProperties();

 

        // Setup mail server

        props.put("mail.smtp.host", host);

 

        // Get session

        props.put("mail.smtp.auth", "true"); // 这样才能通过验证

 

        MyAuthenticator myauth = new MyAuthenticator(mail, password);

        Session session = Session.getDefaultInstance(props, myauth);

        //session.setDebug(true);

 

        for (int i = 0; i < to.size(); i++) {

            // Set the to address

 

            // Define message

            MimeMessage message = new MimeMessage(session);

            // Set the from address

            try {

                message.setFrom(new InternetAddress(from));

            } catch (AddressException e) {

                // TODO Auto-generated catch block

                // e.printStackTrace();

                Logger.debug(e);

            } catch (MessagingException e) {

                // TODO Auto-generated catch block

                // e.printStackTrace();

                Logger.debug(e);

            }

 

            // Set the subject

            try {

                message.setSubject(title);

            } catch (MessagingException e) {

                // TODO Auto-generated catch block

                // e.printStackTrace();

                Logger.debug(e);

                result = "设置邮件主题错误";

            }

 

            // Set the content

            try {

                if (fileNames != null && fileNames.length > 0) {

                    BodyPart mdp = new MimeBodyPart();// 新建一个存放信件内容的BodyPart对象

                    mdp.setContent(content, "text/html;charset=GB2312");// 给BodyPart对象设置内容和格式/编码方式

                    Multipart mm = new MimeMultipart();// 新建一个MimeMultipart对象用来存放BodyPart对

                    // 象(事实上可以存放多个)

                    mm.addBodyPart(mdp);// 将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)

                    // 把mm作为消息对象的内容

                    MimeBodyPart filePart;

                    FileDataSource filedatasource;

                    // 逐个加入附件

                    for (int j = 0; j < fileNames.length; j++) {

                        filePart = new MimeBodyPart();

                        filedatasource = new FileDataSource(fileNames[j]);

                        filePart

                                .setDataHandler(new DataHandler(filedatasource));

                        try {

                            filePart.setFileName(MimeUtility.encodeText(filedatasource.getName()));

                        } catch (UnsupportedEncodingException e) {

                            e.printStackTrace();

                        }   

 

                        mm.addBodyPart(filePart);

                    }

                    message.setContent(mm);

                } else {

                    message.setText(content);

                }

 

            } catch (MessagingException e) {

                // TODO Auto-generated catch block

                // e.printStackTrace();

                Logger.debug(e);

                result = "设置邮件内容错误";

            }

            try {

                message.addRecipient(Message.RecipientType.TO,

                        new InternetAddress(to.get(i)));

            } catch (AddressException e) {

                // TODO Auto-generated catch block

                // e.printStackTrace();

                Logger.debug(e);

                result = "邮件地址解析失败";

            } catch (MessagingException e) {

                // TODO Auto-generated catch block

                result = "邮件信息错误";

            }

            try {

                message.saveChanges();

            } catch (MessagingException e) {

                // TODO Auto-generated catch block

                // e.printStackTrace();

                Logger.debug(e);

                result = "邮件发送失败";

            }

            try {

                Transport.send(message);

            } catch (MessagingException e) {

                // TODO Auto-generated catch block

                // e.printStackTrace();

                Logger.debug(e);

                result = "邮件发送失败";

            }

        }

 

        return result;

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值