javaMail单发与群发(带附件)

import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.internet.MimeBodyPart;
import javax.activation.FileDataSource;
import javax.mail.Multipart;
import javax.mail.internet.MimeMultipart;
import javax.activation.DataHandler;

public class MsgsendService {
    private String host = "";
    private String user = "";
    private String password = "";

    public void setHost(String host) {
        this.host = host;
    }

    public void setAccount(String user, String password) {
        this.user = user;
        this.password = password;
    }

    public void Msgmultisend(String from, String to, String subject,
                             String content) {
        Properties props = new Properties();
        props.put("mail.smtp.host", host); //指定SMTP服务器
        props.put("mail.smtp.auth", "true"); //指定是否需要SMTP验证
        try {

            Session mailSession = Session.getInstance(props, null);
            mailSession.setDebug(true); //是否在控制台显示debug信息

            MimeMessage message = new MimeMessage(mailSession);
            message.setFrom(new InternetAddress(from)); //发件人

            message.setRecipients(Message.RecipientType.TO,
                                  InternetAddress.parse(to));
            message.setSubject(subject); //邮件主题
            message.setText(content); //邮件内容
            message.saveChanges();

            Transport transport = mailSession.getTransport("smtp");
            transport.connect(host, user, password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public void Msgmultisendfile(String from, String to, String subject,
                                 String content, String filename) {
        Properties props = new Properties();
        props.put("mail.smtp.host", host); //指定SMTP服务器
        props.put("mail.smtp.auth", "true"); //指定是否需要SMTP验证
        try {
            Session mailSession = Session.getInstance(props, null);

            mailSession.setDebug(true); //是否在控制台显示debug信息

            MimeMessage message = new MimeMessage(mailSession);

            MimeBodyPart textBodyPart = new MimeBodyPart();
            textBodyPart.setText(content);

            MimeBodyPart fileBodyPart = new MimeBodyPart();
            FileDataSource fds = new FileDataSource(filename);
            fileBodyPart.setDataHandler(new DataHandler(fds));
            fileBodyPart.setFileName(fds.getName());

            Multipart container = new MimeMultipart();
            container.addBodyPart(textBodyPart);
            container.addBodyPart(fileBodyPart);

            message.setSubject(subject);
            message.setContent(container);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO,
                                  InternetAddress.parse(to));
            message.saveChanges();
            Transport transport = mailSession.getTransport("smtp");
            transport.connect(host, user, password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }


    public void Msgsend(String from, String to, String subject, String content) {
        Properties props = new Properties();
        props.put("mail.smtp.host", host); //指定SMTP服务器
        props.put("mail.smtp.auth", "true"); //指定是否需要SMTP验证
        try {
            Session mailSession = Session.getDefaultInstance(props);

            mailSession.setDebug(true); //是否在控制台显示debug信息

            Message message = new MimeMessage(mailSession);
            message.setFrom(new InternetAddress(from)); //发件人
            message.addRecipient(Message.RecipientType.TO,
                                 new InternetAddress(to)); //收件人

            message.setSubject(subject); //邮件主题
            message.setText(content); //邮件内容
            message.saveChanges();

            Transport transport = mailSession.getTransport("smtp");
            transport.connect(host, user, password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            System.out.println(e);
        }

    }


    public void Msgsendfile(String from, String to, String subject,
                            String content, String filename) {
        Properties props = new Properties();
        props.put("mail.smtp.host", host); //指定SMTP服务器
        props.put("mail.smtp.auth", "true"); //指定是否需要SMTP验证
        try {
            Session mailSession = Session.getDefaultInstance(props);

            mailSession.setDebug(true); //是否在控制台显示debug信息

            MimeMessage message = new MimeMessage(mailSession);

            MimeBodyPart textBodyPart = new MimeBodyPart();
            textBodyPart.setText(content);

            MimeBodyPart fileBodyPart = new MimeBodyPart();
            FileDataSource fds = new FileDataSource(filename);
            fileBodyPart.setDataHandler(new DataHandler(fds));
            fileBodyPart.setFileName(fds.getName());

            Multipart container = new MimeMultipart();
            container.addBodyPart(textBodyPart);
            container.addBodyPart(fileBodyPart);

            message.setSubject(subject);
            message.setContent(container);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO,
                                 new InternetAddress(to));
            message.saveChanges();
            Transport transport = mailSession.getTransport("smtp");
            transport.connect(host, user, password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }


    public static void main(String args[]) {
        MsgsendService sm = new MsgsendService();

        sm.setHost("smtp.163.com"); //指定要使用的邮件服务器
        sm.setAccount("txy_821", "*********"); //指定帐号和密码

        sm.Msgmultisendfile("txy_821@163.com",
                            "txy_821@163.com,396668361@qq.com",
                            "mose测试ing", "无语!",
                            "C:\\Documents and Settings\\mos\\桌面\\1233.jpg");
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答关于 JavaMail 发送邮件附件的问题。 在 JavaMail 中发送附件的邮件,可以通过以下步骤实现: 1. 创建一个 Session 对象,用于与邮件服务器进行通信。 ``` Properties props = new Properties(); props.setProperty("mail.smtp.host", "smtp.example.com"); props.setProperty("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("your_username", "your_password"); } }); ``` 2. 创建一个 MimeMessage 对象,设置邮件的基本信息,如发件人、收件人、主题等。 ``` MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("sender@example.com")); message.addRecipient(Message.RecipientType.TO, new InternetAddress("recipient@example.com")); message.setSubject("Test email with attachment"); ``` 3. 创建一个 Multipart 对象,用于包含邮件正文和附件。 ``` Multipart multipart = new MimeMultipart(); ``` 4. 创建一个 MimeBodyPart 对象,用于包含邮件正文,并将其添加到 Multipart 对象中。 ``` MimeBodyPart bodyPart = new MimeBodyPart(); bodyPart.setText("This is the email body"); multipart.addBodyPart(bodyPart); ``` 5. 创建一个 MimeBodyPart 对象,用于包含附件,并将其添加到 Multipart 对象中。 ``` MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.attachFile(new File("path/to/attachment")); attachmentPart.setFileName("attachment.txt"); multipart.addBodyPart(attachmentPart); ``` 6. 将 Multipart 对象设置为 MimeMessage 对象的内容,并发送邮件。 ``` message.setContent(multipart); Transport.send(message); ``` 以上是 JavaMail 发送附件邮件的基本步骤,你可以根据实际需求进行修改和扩展。希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值