I am using the following code to send mail:
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();
}
}
As of this code we need to have email from address and the password for the same, but I would like to send mail to to_email address without the authentication(getting user password).
How can this be done?
I need to do the following things:
- get the from address from properties or context file
- get the to address from properties or context file
- send mail to
to_addresswithout authenticating the from address
1 Answer