2012-01-11 7 views
0

以下は私のコードです。 java 私は内部ネットワークを使用してこれを実行しようとしています。これは、MVCポートレットのprocessActionメソッドで記述されます。私はLiferayのJava Webアプリケーション経由でメールを送信しようとしていますが、何も問題なく動作しているようです。

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); 
    } 

} 

答えて

7

電子メールの設定が他のポートレットとし、Liferayの自分自身と共有することができた場合は、Liferayの

が付属しています内蔵のメールサービスを使用してみてくださいは(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); 

このアプローチの完全な例:https://github.com/kastork/dharma-pm-portlet/blob/master/docroot/WEB-INF/src/com/dharma/pm/portlet/PMPortlet.java

これを実行すると、ポータル用に設定されたメールの設定が使用されdので、SMTPサーバーにアクセスするためにLiferayを設定する必要があります。 (おそらくLiferayがパスワードリマインダー、wikiページ変更通知などを送信できるように設定されていることをお勧めします)。ここでは、このタスクの研究のための1つの出発点は次のとおりです。

http://www.liferay.com/documentation/liferay-portal/6.1/user-guide/-/ai/ma-5

+1

良いアドバイス、それを必要とする各ポートレットのメールコードをやり直す必要はありませんよう!そして構成への変更はハードコーディングされません! – Jonny

+0

気高く私はそれが働いていた........ :) – lee

関連する問題