thelordoftherings
Programmer
Hello,
I am using this method to send an email:
public boolean sendMail(String from, String to, String subject, String body)
{
try
{
InitialContext ctx = new InitialContext();
Session sess = (Session) javax.rmi.PortableRemoteObject.narrow(ctx.lookup("java:comp/env/mail/MailSession"), Session.class);
MimeMessage msg = new MimeMessage(sess);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, extractRecipientAddresses(to));
msg.setSubject(subject, "UTF-8");
msg.setContent(body, "text/plain;charset=UTF-8");
msg.saveChanges();
Transport tr = sess.getTransport("smtp");
tr.connect(HOST, "", "");
Address[] address = msg.getAllRecipients();
tr.sendMessage(msg, address);
tr.close();
return true;
}
catch (Exception e)
{
System.err.println(e.getMessage());
return false;
}
}
The HOST is our exchange hostname inside the organization.
The problem is that this method only sends mails to email addresses inside our organization. If, for example, I would like to use it to send an email to an account outside the organization, such as Hotmail, the sending doesn't succeed. Any ideas why?
I am using this method to send an email:
public boolean sendMail(String from, String to, String subject, String body)
{
try
{
InitialContext ctx = new InitialContext();
Session sess = (Session) javax.rmi.PortableRemoteObject.narrow(ctx.lookup("java:comp/env/mail/MailSession"), Session.class);
MimeMessage msg = new MimeMessage(sess);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, extractRecipientAddresses(to));
msg.setSubject(subject, "UTF-8");
msg.setContent(body, "text/plain;charset=UTF-8");
msg.saveChanges();
Transport tr = sess.getTransport("smtp");
tr.connect(HOST, "", "");
Address[] address = msg.getAllRecipients();
tr.sendMessage(msg, address);
tr.close();
return true;
}
catch (Exception e)
{
System.err.println(e.getMessage());
return false;
}
}
The HOST is our exchange hostname inside the organization.
The problem is that this method only sends mails to email addresses inside our organization. If, for example, I would like to use it to send an email to an account outside the organization, such as Hotmail, the sending doesn't succeed. Any ideas why?