• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javamail error

gilberto silva
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code of sending mail. However, it shows error on InternetAddress[] address = {new InternetAddress(to)}; I tried to set the variable to ="bcdq1234@gmail.com" , it works. Can I set the To variable getting from a session which passed from the previous page?

<title>JSP JavaMail Example </title>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>

String username = (String)session.getAttribute("username");
String to = (String)session.getAttribute("email");
String password = (String)session.getAttribute("password");

String host = "smtp.umac.mo";
String from = "JSP";
String subject = "Password Recovery Letter";
String messagebegin = "Dear, "+ username +"\n\n";
String messageContent = "The password is shown below: \n" + "Username" + username + "\nPassword: " + password + "\n";
String messageEnd = "\nIf you have any question, please let us know and we will provide our best service to you. Thanks for support us!\n\n Best regards,";
String messageText = messagebegin + messageContent + messageEnd;

// Create some properties and obtain a session.

Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.setProperty("mail.smtp.ssl.trust", host);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");

Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("abcd1234@pclan.umac.mo", "********");} });

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSentDate(new Date());

Transport tr = mailSession.getTransport("smtp");
tr.connect(host, "abcd1234@umac.mo", "*******");
tr.sendMessage(msg, msg.getAllRecipients());
out.println("The Mail was sent.");

Tim Moores
Posts: 3004
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct syntax should be something like

InternetAddress[] address = new InternetAddress[] {new InternetAddress(to)};

Sure you can use a value from the session; it looks as if your code does that already.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic