我是初学者,这个问题已经捆饶我2天了,不得已麻烦大家的时间,非常感谢您的时间:
代码如下
jButton1_actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="Send")
{
sm.send ("[email protected]","[email protected]","subject","content");
}
}
sm 是CLASS SENDER 的一个实例
class Sender
{
String host="";
String user="";
String password="";public void setHost(String host)
{
this.host=host;
}public void setAccount(String user,String password)
{
this.user=user;
this.password=password;
}public void send(String from,String to,String subject,String content)
{
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
try
{
Session mailSession = Session.getDefaultInstance(props); mailSession.setDebug(true); Message message=new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));//from
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));//to
message.setSubject(subject);//subject
message.setText(content);//content
message.saveChanges(); Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}catch(Exception e)
{
System.out.println(e);
}}
}
host, user, password 都已经设定,在单独SENDER CLASS里面可以发送并且在JB9.0里可以传输到LISNER里面,但是LISTENER却不能执行 SM.SEND();
请问是怎么回事?小弟谢过了
代码如下
jButton1_actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="Send")
{
sm.send ("[email protected]","[email protected]","subject","content");
}
}
sm 是CLASS SENDER 的一个实例
class Sender
{
String host="";
String user="";
String password="";public void setHost(String host)
{
this.host=host;
}public void setAccount(String user,String password)
{
this.user=user;
this.password=password;
}public void send(String from,String to,String subject,String content)
{
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
try
{
Session mailSession = Session.getDefaultInstance(props); mailSession.setDebug(true); Message message=new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));//from
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));//to
message.setSubject(subject);//subject
message.setText(content);//content
message.saveChanges(); Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}catch(Exception e)
{
System.out.println(e);
}}
}
host, user, password 都已经设定,在单独SENDER CLASS里面可以发送并且在JB9.0里可以传输到LISNER里面,但是LISTENER却不能执行 SM.SEND();
请问是怎么回事?小弟谢过了
我决定应该是...
JButton _send_btn = new JButton("...");
...
if( e.getSource() == _send_btn)
...
我觉得应该是...
JButton _send_btn = new JButton("...");
...
if( e.getSource() == _send_btn)
...
...
JButton _send_btn = new JButton("...");
...
if( e.getSource() == _send_btn)
...
这样好。
要是以后做个国际化
e.getActionCommand()变成其他的什么(比如"发送"),还是会有问题的。:P
jLabel1.setText(sm.host);
jTextField1.setText("");
}
如果改成这样的话,为什么别的方法可以执行而惟独SM.SEND不能呢?
谢谢楼上们的回答
就像JBUILDER 平常创建App一样,一个APPLICATION 一个FRAME
LISTENER应该是在FRAME里面吧
你可以在
public void send(String from,String to,String subject,String content)
方法里
加一些调试信息,看看具体问题。
......
Session mailSession = Session.getDefaultInstance(props);
//比如这里加上
System.out.println("mailSession is created");
mailSession.setDebug(true);Message message=new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));//from
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));//to
message.setSubject(subject);//subject
message.setText(content);//content
message.saveChanges();Transport transport = mailSession.getTransport("smtp");
//这里
System.out.println("transport is created and ready to connect host");
transport.connect(host, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
......
在JB里运行看看
真的很感谢
我知道没发送是因为两个信箱都是我的,如果成功运行的话我会收到 主题:SUBJECT 内容:CONTENT的邮件...
但是运行N次,实验了N种方法都不行
但是在这个LISTENER里 输入
SM.SETACCOUNT();
SM.SETHOST();
都是可以的,值都可以传到Label.settext();里面
我现在正研究是不是线程的问题....我是初学者,请多多指教
我看不会是线程的问题,是网络连接问题,可能是一个premisson问题也说不定。
你可以去看看别人的例子
http://java.sun.com/developer/EJTechTips/2002/tt1015.html#1这里有些例子下
但是新问题又出现了...MASSAGE的CONTENT只能是中文才能发过去...
英文就不行
SUBJECT中英就都可以.....