2012-11-10 45 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

你显然没有看到*的问题*。其中唯一提到的电子邮件是特别说明它不是*想要的。 –

相关问题