Reputation: 246
Below is my code,please point out what am i doin wrong?? java I am trying to do this by using our internal network. This is written in processAction method in MVC portlet.
String name=actionRequest.getParameter("name");
String email=actionRequest.getParameter("email");
String myMessage=actionRequest.getParameter("message");
String host = "smtp.xyz.com";
int port = 25;
String username = "xxx";
String password = "yyy";
Properties props = new Properties();
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.xyz.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.user", username);
props.put("mail.smtp.password", password);
Session session = Session.getInstance(props);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(email));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("xxx"));
message.setSubject("Testing Subject");
message.setText("From " + name + "," + myMessage);
Transport transport = session.getTransport("smtp");
transport.connect(host, port, username, password);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
Upvotes: 0
Views: 1375
Reputation: 1188
If the email configuration can be shared with other portlets and with Liferay itself, try using the in-built mail service that comes with Liferay
(relies on com.liferay.mail.service.MailServiceUtil)
String fromEmail = "[email protected]";
String fromName = "Site Administrator";
String subject = "Hello from example.com";
String body = "text of message";
InternetAddress from = new InternetAddress(fromEmail, fromName);
InternetAddress to = InternetAddress.parse("xxx");
MailMessage emailMessage = new MailMessage(from, to, subject, body, false);
MailServiceUtil.sendEmail(emailMessage);
A full example of this approach: https://github.com/kastork/dharma-pm-portlet/blob/master/docroot/WEB-INF/src/com/dharma/pm/portlet/PMPortlet.java
When doing this, the mail configuration set up for the Portal is used, so you need to configure Liferay to access your SMTP server. (You probably want that configured anyway so the Liferay can do things like send password reminders, wiki page change notifications and so on). Here is one starting point for research into this task:
http://www.liferay.com/documentation/liferay-portal/6.1/user-guide/-/ai/ma-5
Upvotes: 7