如何在Java中仅包含from和to以及主题和消息的邮件? - java

我正在使用以下代码发送邮件:

protected void mailSettings() {
    Object lookedUp = null;
    String mailSettingValues[] = null;
    try {
        InitialContext initialContext = new InitialContext();
        lookedUp = initialContext.lookup("java:/comp/env/mailsettings");
        mailSettingValues = lookedUp.toString().split(",");
        smtphost = mailSettingValues[0];
        port = mailSettingValues[1];
        username = mailSettingValues[2];
        password = mailSettingValues[3];
    } catch (NamingException e) {
        e.printStackTrace();
    }

    m_properties = new Properties();
    m_properties.put("mail.smtp.host", smtphost);
    m_properties.put("mail.smtp.auth", "true");
    m_properties.put("mail.smtp.starttls.enable", "true");
    m_properties.put("mail.smtp.port", port);

    m_Session = Session.getDefaultInstance(m_properties,
            new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

}

public void sendMail(String mail_from, String mail_to, String mail_subject,
        String m_body) {
    try {
        m_simpleMessage = new MimeMessage(m_Session);
        m_fromAddress = new InternetAddress(mail_from);
        m_toAddress = new InternetAddress(mail_to);

        m_simpleMessage.setFrom(m_fromAddress);
        m_simpleMessage.setRecipient(RecipientType.TO, m_toAddress);
        m_simpleMessage.setSubject(mail_subject);

        m_simpleMessage.setContent(m_body, "text/plain");

        Transport.send(m_simpleMessage);
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
}

从此代码开始,我们需要使用相同的地址和密码发送电子邮件,但是我想在不进行身份验证的情况下将邮件发送到to_email地址(获取用户密码)。

如何才能做到这一点?

我需要做以下事情:

从属性或上下文文件获取发件人地址
从属性或上下文文件获取收件人地址
在不验证发件人地址的情况下将邮件发送到to_address

参考方案

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SimpleSendEmail {
    public static void main(String[] args) {

        String host = "your smtp host";
        String to = "[email protected]";
        String from = "[email protected]";
        String subject = "My First Email";
        String messageText = "I am sending a message using the"
                + " JavaMail API.\n" + "Here type your message.";
        boolean sessionDebug = false;
        Properties props = System.getProperties();
        props.put("mail.host", host);
        props.put("mail.transport.protocol", "smtp");
        Session session = Session.getDefaultInstance(props, null);
        // Set debug on the Session so we can see what is going on
        // Passing false will not echo debug info, and passing true
        // will.
        session.setDebug(sessionDebug);
        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = { new InternetAddress(to) };
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(subject);
            msg.setSentDate(new Date());
            msg.setText(messageText);

            Transport.send(msg);
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}

Java Double与BigDecimal - java

我正在查看一些使用双精度变量来存储(360-359.9998779296875)结果为0.0001220703125的代码。 double变量将其存储为-1.220703125E-4。当我使用BigDecimal时,其存储为0.0001220703125。为什么将它双重存储为-1.220703125E-4? 参考方案 我不会在这里提及精度问题,而只会提及数字…

JAVA:字节码和二进制有什么区别? - java

java字节代码(已编译的语言,也称为目标代码)与机器代码(当前计算机的本机代码)之间有什么区别?我读过一些书,他们将字节码称为二进制指令,但我不知道为什么。 参考方案 字节码是独立于平台的,在Windows中运行的编译器编译的字节码仍将在linux / unix / mac中运行。机器代码是特定于平台的,如果在Windows x86中编译,则它将仅在Win…

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

Java:BigInteger,如何通过OutputStream编写它 - java

我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…