我做了一个自动发邮件,发出的主题全是null,怎样解决?

时间:2021-12-18 17:48:00
就是我做了一个自动发送邮件到指定服务器的程序,然后发送的标题是另外3个类的输出结果,但是发出的邮件标题全是null,问题出在哪了,求教

5 个解决方案

#1


可能获得标题的代码有些错误吧?
没见你的代码,无法确定
或许打错一个字母什么的?

#2


import java.io.File;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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 javax.mail.internet.MimeUtility;
public class SendMailTest {

    public int sendMail(String host, String from, String username,
            String password, String to, String subject, String content,
            Vector appendFiles, boolean author) {
        try {
            Properties p = System.getProperties();

            p.put("mail.smtp.host", host);
            p.put("mail.smtp.auth", author + "");
            p.put("mail.smtp.localhost", "forceview");

            Session session = Session.getDefaultInstance(p,
                    new PopupAuthenticator(username, password));

            MimeMessage msg = new MimeMessage(session);

            msg.setFrom(new InternetAddress(from));// 设置发送帐号

            String[] tos = to.split(";");
            InternetAddress[] sendTo = new InternetAddress[tos.length];
            for (int i = 0; i < tos.length; i++) {
                sendTo[i] = new InternetAddress(tos[i]);
            }
            msg.setRecipients(javax.mail.internet.MimeMessage.RecipientType.TO,
                    sendTo);// 设置接收邮件的地址

            msg.setSubject(subject, "utf-8");// 设置主题

            msg.setSentDate(new Date());// 设置发送时间

            Multipart mp = new MimeMultipart();

            MimeBodyPart bp = new MimeBodyPart();
            bp.setContent(
                    "<meta http-equiv=Content-Type content=text/html; charset=utf-8>"
                            + content, "text/html;charset=UTF-8");

            mp.addBodyPart(bp);

            MimeBodyPart fp;
            String appendStrs = "";
            if (appendFiles != null) {
                for (int i = 0; i < appendFiles.size(); i++) {
                    fp = new MimeBodyPart();
                    appendStrs = appendFiles.get(i).toString();
                    String[] filenames = appendStrs.split("\\|");

                    if (new File(filenames[0]).exists()) {
                        FileDataSource fds = new FileDataSource(filenames[0]);
                        fp.setDataHandler(new DataHandler(fds));
                        fp.setFileName(MimeUtility.encodeText(filenames[1]
                                + fds.getName(), "utf-8", "K"));
                        mp.addBodyPart(fp);
                    }
                }
            }
            msg.setContent(mp);
            msg.saveChanges();

            Transport transport = session.getTransport("smtp");// 设置发送端口
            transport.connect(host, username, password);// 连接邮件服务器
            transport.sendMessage(msg, msg.getAllRecipients());

            transport.close();
        } catch (Exception e) {
            e.printStackTrace();

            return -1;
        }

        return 0;
    }

    class PopupAuthenticator extends Authenticator {
        private String username, password;

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

        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(this.username, this.password);
        }
    }

}
我的代码如上,麻烦帮忙看看,弄了很久了,就是不知道问题出在哪?

#3


debug,看看你setsubject里面是不是空的

#4


subject 没有赋值,你在调用方法的时候传过来,我在做邮件发送的时候主题都是写死的,因为不需要改变的

#5


subject 你调试看下 肯定是没有赋值才是Null

#1


可能获得标题的代码有些错误吧?
没见你的代码,无法确定
或许打错一个字母什么的?

#2


import java.io.File;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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 javax.mail.internet.MimeUtility;
public class SendMailTest {

    public int sendMail(String host, String from, String username,
            String password, String to, String subject, String content,
            Vector appendFiles, boolean author) {
        try {
            Properties p = System.getProperties();

            p.put("mail.smtp.host", host);
            p.put("mail.smtp.auth", author + "");
            p.put("mail.smtp.localhost", "forceview");

            Session session = Session.getDefaultInstance(p,
                    new PopupAuthenticator(username, password));

            MimeMessage msg = new MimeMessage(session);

            msg.setFrom(new InternetAddress(from));// 设置发送帐号

            String[] tos = to.split(";");
            InternetAddress[] sendTo = new InternetAddress[tos.length];
            for (int i = 0; i < tos.length; i++) {
                sendTo[i] = new InternetAddress(tos[i]);
            }
            msg.setRecipients(javax.mail.internet.MimeMessage.RecipientType.TO,
                    sendTo);// 设置接收邮件的地址

            msg.setSubject(subject, "utf-8");// 设置主题

            msg.setSentDate(new Date());// 设置发送时间

            Multipart mp = new MimeMultipart();

            MimeBodyPart bp = new MimeBodyPart();
            bp.setContent(
                    "<meta http-equiv=Content-Type content=text/html; charset=utf-8>"
                            + content, "text/html;charset=UTF-8");

            mp.addBodyPart(bp);

            MimeBodyPart fp;
            String appendStrs = "";
            if (appendFiles != null) {
                for (int i = 0; i < appendFiles.size(); i++) {
                    fp = new MimeBodyPart();
                    appendStrs = appendFiles.get(i).toString();
                    String[] filenames = appendStrs.split("\\|");

                    if (new File(filenames[0]).exists()) {
                        FileDataSource fds = new FileDataSource(filenames[0]);
                        fp.setDataHandler(new DataHandler(fds));
                        fp.setFileName(MimeUtility.encodeText(filenames[1]
                                + fds.getName(), "utf-8", "K"));
                        mp.addBodyPart(fp);
                    }
                }
            }
            msg.setContent(mp);
            msg.saveChanges();

            Transport transport = session.getTransport("smtp");// 设置发送端口
            transport.connect(host, username, password);// 连接邮件服务器
            transport.sendMessage(msg, msg.getAllRecipients());

            transport.close();
        } catch (Exception e) {
            e.printStackTrace();

            return -1;
        }

        return 0;
    }

    class PopupAuthenticator extends Authenticator {
        private String username, password;

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

        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(this.username, this.password);
        }
    }

}
我的代码如上,麻烦帮忙看看,弄了很久了,就是不知道问题出在哪?

#3


debug,看看你setsubject里面是不是空的

#4


subject 没有赋值,你在调用方法的时候传过来,我在做邮件发送的时候主题都是写死的,因为不需要改变的

#5


subject 你调试看下 肯定是没有赋值才是Null