2012-11-10 43 views
1

我以爲我在之前的文章中已經解決了這個問題,但是我仍然爲此付出了努力。 在我的Rails應用程序中,我試圖讓用戶能夠在應用程序中發送消息(不通過電子郵件)。我使用的鐵軌3.2.8和紅寶石1.9.3p194從一個用戶發送消息到另一個

消息belongs_to的:用戶

用戶的has_many:消息

這是我在我的消息模型

def self.send_message(from, recipient) 
    recipient do |recipient| 
     msg = self.clone 
     msg.sent = false 
     msg.user_id = recipient 
     msg.save 
    end 
    self.update_attributes :user_id => from.id, :sent => true 
    end 
發送消息的方法

我試圖做測試這種在軌控制檯: Message.send_message(U2,U)

,其中U2 = User.find(1)& U = User.find(2)

我不斷收到錯誤NoMethodError:未定義的方法`收件人爲#

我在做什麼錯?如何正確地向用戶發送來自其他用戶的消息。

如果有人有更好的方法,我願意接受任何策略。

+0

幾個問題1)如果你做'Message.send_message(u2,u)'所以我認爲'recepient'是'在那裏2)msg = self.clone msg.sent在一個類方法將給類msg.sent msg.user_id和msg.save對類對象ie味精我覺得它應該響應類的實例方法不知道這樣做糾正我,如果我錯了 – Viren

+0

@Viren我不知道我完全理解你的問題,我是相當新的編碼。我一直在回答這個問題http://stackoverflow.com/questions/5141564/model-users-message-in-rails-3希望澄清一些事情。 – DaveG

+1

你需要像[this]這樣的結構(http://pastie.org/5355281)請忽略一些'TYPO'錯誤 – Viren

回答

-4
package com.bizibrush.action.login; 
import java.security.Security; 
import java.util.Properties; 

import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class GoogleTest { 

private static final String SMTP_HOST_NAME = "smtp.gmail.com"; 
private static final String SMTP_PORT = "465"; 
private static final String emailMsgTxt = "Test Message Contents"; 
private static final String emailSubjectTxt = "A test from gmail"; 
private static final String emailFromAddress = "mailID"; 
private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 
private static final String sendTo = "mailID"; 

/*public static void main(String args[]) throws Exception { 

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 

new GoogleTest().sendSSLMessage(sendTo, emailSubjectTxt,emailMsgTxt, emailFromAddress); 
System.out.println("Sucessfully Sent mail to All Users"); 
}*/ 

public void sendSSLMessage(String recipients, String subject, 
String message, String from) throws MessagingException { 
boolean debug = true; 

Properties props = new Properties(); 
props.put("mail.smtp.host", SMTP_HOST_NAME); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.debug", "true"); 
props.put("mail.smtp.port", SMTP_PORT); 
props.put("mail.smtp.socketFactory.port", SMTP_PORT); 
props.put("mail.smtp.socketFactory.class", SSL_FACTORY); 
props.put("mail.smtp.socketFactory.fallback", "false"); 

Session session = Session.getDefaultInstance(props, 
new javax.mail.Authenticator() { 

protected PasswordAuthentication getPasswordAuthentication() { 
return new PasswordAuthentication("mailID", "password"); 
} 
}); 

session.setDebug(debug); 

Message msg = new MimeMessage(session); 
InternetAddress addressFrom = new InternetAddress(from); 
msg.setFrom(addressFrom); 

InternetAddress addressTo = new InternetAddress(); 
addressTo = new InternetAddress(recipients); 
msg.setRecipient(Message.RecipientType.TO, addressTo); 

// Setting the Subject and Content Type 
msg.setSubject(subject); 
msg.setContent(message, "text/plain"); 
Transport.send(msg); 
} 
} 

通過此代碼,您可以通過gmail發送郵件。你應該添加lib文件

+0

你顯然沒有看到*的問題*。其中唯一提到的電子郵件是特別說明它不是*想要的。 –

相關問題