Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

javamail error  RSS feed

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 ="" , 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 = "";
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("", host);
props.setProperty("", 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("", "********");} });

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, "", "*******");
tr.sendMessage(msg, msg.getAllRecipients());
out.println("The Mail was sent.");

Tim Moores
Saloon Keeper
Posts: 3824
  • 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
Boost this thread!