我正在使用以下代码发送邮件:
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
在不验证发件人地址的情况下发送邮件